要将按钮放置在图像下,并在窗口大小改变时保持固定,可以使用HTML和CSS来实现。
首先,需要创建一个包含图像和按钮的HTML页面。可以使用以下代码作为示例:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
display: inline-block;
}
.image {
width: 100%;
height: auto;
}
.button {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
padding: 10px 20px;
background-color: #f44336;
color: white;
font-size: 16px;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<img class="image" src="your-image.jpg" alt="Your Image">
<button class="button">Click Me</button>
</div>
</body>
</html>
在上述代码中,.container
类定义了一个相对定位的容器,.image
类定义了图像的样式,.button
类定义了按钮的样式。按钮使用绝对定位,并通过 bottom
、left
和 transform
属性来将其放置在图像下方并居中对齐。
接下来,需要使用CSS媒体查询来在窗口大小改变时保持按钮的固定位置。可以使用以下代码作为示例:
@media screen and (max-width: 768px) {
.button {
position: static;
transform: none;
margin-top: 10px;
}
}
在上述代码中,使用 @media
媒体查询来指定窗口宽度小于等于768px时的样式。在这种情况下,将按钮的定位属性设置为 static
,取消绝对定位,并通过 margin-top
属性来保持按钮与图像的间距。
通过以上的HTML和CSS代码,可以将按钮放置在图像下,并在窗口大小改变时保持固定。
领取专属 10元无门槛券
手把手带您无忧上云