CSS 滿版區塊 (Full Screen Section)
滿版區塊是指佔據整個視窗高度的區塊,常用於首頁 Hero 區域、Landing Page 的各個區段。
基本滿版
.fullscreen {
height: 100vh;
}
<div class="fullscreen">滿版內容</div>
實作預覽 (基本滿版)
滿版內容展示 (模擬高度)
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;
}
<section class="hero">
<h1>歡迎來到我的網站</h1>
<p>這是一個使用 CSS 滿版技術製作的 Hero 區塊範例。</p>
<div class="hero-buttons">
<a href="#" class="btn-primary">開始使用</a>
<a href="#" class="btn-secondary">了解更多</a>
</div>
</section>
實作預覽 (Hero 區塊)
背景圖片 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;
}
<section class="hero">
<div class="hero-background">
<img src="bg.jpg" alt="背景圖片" />
</div>
<div class="hero-content">
<h1>背景圖片 Hero</h1>
<p>透過 Overlay 增加文字可讀性。</p>
</div>
</section>
實作預覽 (背景圖片 Hero)
背景圖片 Hero
透過 Overlay 增加文字可讀性。
影片背景
.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>
實作預覽 (影片背景)
影片背景 Hero
(此處以深色背景模擬影片)
### 分割畫面
```css
.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;
}
}
<div class="split-screen">
<div class="split-left">
<div class="content">
<h2>分割畫面</h2>
<p>左側文字內容。</p>
</div>
</div>
<div class="split-right"></div>
</div>
實作預覽 (分割畫面)
分割畫面
左側內容 / 右側圖片
多個滿版區段
.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%;
}
<section class="section">
<div class="section-content">
<h2>第一個滿版區塊</h2>
</div>
</section>
<section class="section">
<div class="section-content">
<h2>第二個滿版區塊</h2>
</div>
</section>
實作預覽 (多個滿版區段)
區塊 1
區塊 2
區塊 3
↑ 可在容器內捲動觀察
捲動指示
告訴使用者可以往下捲動:
.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);
}
}
<div class="scroll-indicator">
<span class="mouse"></span>
<p>Scroll Down</p>
</div>
實作預覽 (捲動指示)
↓
考量固定元素
如果有固定導航列,需要調整高度:
.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;
}