如何操作 - 图片上的按钮
了解如何使用 CSS 在图片上添加按钮。
图片上的按钮

如何在图片上添加按钮
步骤 1) 添加 HTML
示例
<div class="container">
<img src="img_snow.jpg" alt="雪">
<button class="btn">按钮</button>
</div>
步骤 2) 添加 CSS
示例
/* 定位按钮所需的容器。根据需要调整宽度 */
.container {
position: relative;
width: 50%;
}
/* 使图像具有响应性 */
.container img {
width: 100%;
height: auto;
}
/* 设置按钮样式并将其放置在容器/图像的中间 */
.container .btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
}
.container .btn:hover {
background-color: black;
}
自己动手试一试 »