如何創建:圖片上的按鈕
學習如何使用 CSS 將按鈕添加到圖片上。
圖片上的按鈕

如何在圖像上添加按鈕
第一步 - 添加 HTML:
<div class="container"> <img src="img_snow.jpg" alt="Snow"> <button class="btn">Button</button> </div>
第二步 - 添加 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; }