如何做 - 主角圖片
瞭解如何使用 CSS 建立主角圖片。
主角圖片是帶有文字的大圖片,通常放在網頁頂部
如何建立主角圖片
步驟 1) 新增 HTML
示例
<div class="hero-image">
<div class="hero-text">
<h1>我是張三</h1>
<p>我是一名攝影師</p>
<button>僱用我</button>
</div>
</div>
步驟 2) 新增 CSS
示例
body, html {
height: 100%;
}
/* 主角圖片 */
.hero-image {
/* 使用 "linear-gradient" 為圖片 (photographer.jpg) 新增變暗的背景效果。這將使文字更易於閱讀 */
background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("photographer.jpg");
/* 設定特定高度 */
height: 50%;
/* 定位並居中圖片,以便在所有螢幕上都能很好地縮放 */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
/* 將文字放在圖片中間 */
.hero-text {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
}
自己動手試一試 »