如何操作 - 圖片上的按鈕
瞭解如何使用 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;
}
自己動手試一試 »