CSS 滿版區塊 (Full Screen Section)
滿版區塊是指佔據整個視窗高度的區塊,常用於首頁 Hero 區域、Landing Page 的各個區段。
基本滿版
.fullscreen {
height: 100vh;
}
100vh 表示視窗高度的 100%。
滿版 + 置中內容
.hero {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
完整範例
Hero 區塊
.hero {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
padding: 24px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.hero h1 {
font-size: clamp(2rem, 5vw, 4rem);
margin: 0 0 16px;
}
.hero p {
font-size: clamp(1rem, 2vw, 1.25rem);
max-width: 600px;
margin: 0 0 32px;
opacity: 0.9;
}
.hero-buttons {
display: flex;
gap: 16px;
flex-wrap: wrap;
justify-content: center;
}
背景圖片 Hero
.hero {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
color: white;
}
.hero-background {
position: absolute;
inset: 0;
z-index: -1;
}
.hero-background img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* 暗化覆蓋層 */
.hero-background::after {
content: '';
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.5);
}
.hero-content {
text-align: center;
padding: 24px;
max-width: 800px;
}
影片背景
.hero {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
overflow: hidden;
}
.hero-video {
position: absolute;
inset: 0;
z-index: -1;
}
.hero-video video {
width: 100%;
height: 100%;
object-fit: cover;
}
.hero-video::after {
content: '';
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.4);
}
<section class="hero">
<div class="hero-video">
<video autoplay muted loop playsinline>
<source src="video.mp4" type="video/mp4">
</video>
</div>
<div class="hero-content">
<h1>標題</h1>
</div>
</section>
分割畫面
.split-screen {
min-height: 100vh;
display: flex;
}
.split-left {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
padding: 60px;
background: #f5f5f5;
}
.split-right {
flex: 1;
background: url('image.jpg') center/cover;
}
@media (max-width: 768px) {
.split-screen {
flex-direction: column;
}
.split-left,
.split-right {
min-height: 50vh;
}
}
多個滿版區段
.section {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 60px 24px;
}
.section:nth-child(odd) {
background: white;
}
.section:nth-child(even) {
background: #f5f5f5;
}
.section-content {
max-width: 1200px;
width: 100%;
}
捲動指示
告訴使用者可以往下捲動:
.scroll-indicator {
position: absolute;
bottom: 40px;
left: 50%;
transform: translateX(-50%);
animation: bounce 2s infinite;
}
@keyframes bounce {
0%, 100% {
transform: translateX(-50%) translateY(0);
}
50% {
transform: translateX(-50%) translateY(10px);
}
}
考量固定元素
如果有固定導航列,需要調整高度:
.hero {
min-height: calc(100vh - 60px); /* 扣除導航高度 */
margin-top: 60px; /* 或用 padding-top */
}
行動裝置注意事項
在行動裝置上,100vh 可能會因為瀏覽器的網址列而有問題。可以使用:
.hero {
min-height: 100vh;
min-height: 100dvh; /* dynamic viewport height */
}
或使用 JavaScript 計算:
function setVH() {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
setVH();
window.addEventListener('resize', setVH);
.hero {
min-height: 100vh;
min-height: calc(var(--vh, 1vh) * 100);
}
最小高度 vs 固定高度
建議使用 min-height 而不是 height,這樣當內容超出時不會被裁切:
/* 推薦 */
.section {
min-height: 100vh;
}
/* 不推薦 */
.section {
height: 100vh;
overflow: hidden;
}