CSS實現(xiàn)元素?fù)螡M剩余空間的五種方法
CSS實現(xiàn)元素?fù)螡M剩余空間的5種方法 ??
在日常開發(fā)中,我們經(jīng)常需要讓某個元素占據(jù)容器的剩余空間。這是一個常見的布局需求,比如側(cè)邊欄+主內(nèi)容區(qū)、頭部+內(nèi)容區(qū)+底部等布局。本文將介紹5種不同的方法來實現(xiàn)這個需求,并分析各種方法的優(yōu)缺點。
Flexbox 方法
?? 詳見
./01-flexbox/index.html
Flexbox 是最現(xiàn)代和推薦的方式。通過設(shè)置 flex-grow: 1,元素會自動擴(kuò)展占據(jù)剩余空間。對于垂直布局,配合 height: 0 使用效果更好。
聊天應(yīng)用布局:

<div class="chat-container">
<header class="chat-header">
<img src="avatar.jpg" alt="用戶頭像">
<h2>聊天室</h2>
</header>
<main class="chat-messages">
<div class="message received">收到的消息</div>
<div class="message sent">發(fā)送的消息</div>
</main>
<footer class="chat-input">
<input type="text" placeholder="輸入消息...">
<button>發(fā)送</button>
</footer>
</div>
<style>
.chat-container {
display: flex;
flex-direction: column;
height: 100vh;
}
.chat-header {
height: 60px;
background: #075e54;
}
.chat-messages {
flex: 1;
height: 0; /* 關(guān)鍵屬性 */
overflow-y: auto;
}
.chat-input {
height: 60px;
background: #f0f0f0;
}
</style>實現(xiàn)思路解析:
垂直布局三要素:
display: flex+flex-direction: column創(chuàng)建垂直伸縮布局flex: 1讓中間區(qū)域自動占據(jù)剩余空間height: 0防止內(nèi)容撐開,確保嚴(yán)格遵守 flex 規(guī)則
滾動區(qū)域處理:
- 設(shè)置
overflow-y: auto使內(nèi)容過多時可滾動 - 不設(shè)置 height: 0 的話,內(nèi)容會撐開容器,破壞布局
- 設(shè)置
布局結(jié)構(gòu):
容器 (100vh)
├── 頭部 (固定60px)
├── 內(nèi)容 (flex: 1 + height: 0)
└── 底部 (固定60px)性能優(yōu)化:
- 使用 transform 代替定位提升性能
- 避免頻繁改變高度觸發(fā)重排
其他適用場景:
后臺管理系統(tǒng)布局
- 頂部固定導(dǎo)航欄
- 左側(cè)固定菜單欄
- 右側(cè)自適應(yīng)內(nèi)容區(qū)
移動端應(yīng)用布局
- 固定頂部header
- 可滾動的內(nèi)容區(qū)域
- 固定底部導(dǎo)航欄
文檔編輯器
- 工具欄
- 自適應(yīng)的編輯區(qū)域
- 狀態(tài)欄
優(yōu)點:
- 靈活且直觀
- 支持動態(tài)內(nèi)容
- 響應(yīng)式表現(xiàn)好
- 一維布局的最佳選擇
缺點:
- IE11及以下版本支持不完善
- 不適合復(fù)雜的二維布局
Grid 方法
?? 詳見
./02-grid/index.html
Grid 布局通過 fr 單位或 auto 來分配剩余空間,特別適合復(fù)雜的二維布局。
儀表盤布局:

<div class="dashboard">
<!-- 狀態(tài)卡片 -->
<div class="widget status-card">
<h3>總訪問量</h3>
<div class="number">1,234,567</div>
</div>
<div class="widget status-card">
<h3>活躍用戶</h3>
<div class="number">45,678</div>
</div>
<!-- 大尺寸部件 -->
<div class="widget large-widget">
<h3>訪問趨勢圖</h3>
</div>
</div>
<style>
.dashboard {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 100px 200px;
gap: 20px;
}
.widget {
background: white;
border-radius: 10px;
padding: 20px;
}
.large-widget {
grid-column: span 2;
}
/* 響應(yīng)式布局 */
@media (max-width: 768px) {
.dashboard {
grid-template-columns: repeat(2, 1fr);
}
}
</style>實現(xiàn)思路解析:
網(wǎng)格系統(tǒng)設(shè)計:
- 使用
repeat(4, 1fr)創(chuàng)建均等的四列布局 grid-template-rows定義不同的行高gap: 20px統(tǒng)一設(shè)置網(wǎng)格間距
- 使用
響應(yīng)式布局策略:
桌面端:4列
平板端:2列 (media query)
手機(jī)端:1列組件尺寸控制:
- 使用
grid-column: span 2讓組件跨越兩列 - 通過
grid-area可以實現(xiàn)更復(fù)雜的位置控制
- 使用
自適應(yīng)處理:
- fr 單位自動分配剩余空間
- 結(jié)合 minmax() 設(shè)置最小尺寸防止擠壓
適用場景:
圖片畫廊/瀑布流
- 自適應(yīng)的網(wǎng)格布局
- 不規(guī)則的圖片排列
- 響應(yīng)式的列數(shù)調(diào)整
網(wǎng)站首頁布局
- 多區(qū)塊的內(nèi)容排列
- 廣告位的靈活布局
- 響應(yīng)式的柵格系統(tǒng)
在線商城商品展示
- 商品網(wǎng)格
- 不同尺寸的促銷位
- 自適應(yīng)的分類展示
優(yōu)點:
- 二維布局更靈活
- 代碼簡潔明了
- 響應(yīng)式設(shè)計更簡單
- 支持復(fù)雜的對齊和間距控制
缺點:
- 老瀏覽器兼容性問題
- 對于簡單的一維布局可能顯得過重
- 學(xué)習(xí)曲線相對較陡
絕對定位方法
?? 詳見
./03-absolute/index.html
使用絕對定位和固定值來計算剩余空間,通過設(shè)置 top、right、bottom、left 值來精確控制位置。
視頻播放器控制層:

<div class="video-container">
<video poster="thumbnail.jpg">
<source src="video.mp4" type="video/mp4">
</video>
<div class="controls-overlay">
<div class="top-controls">
<h2>視頻標(biāo)題</h2>
<button class="control-button">??</button>
</div>
<div class="progress-bar">
<div class="progress"></div>
</div>
<div class="bottom-controls">
<button class="control-button">??</button>
<div class="time-display">02:30 / 10:00</div>
<button class="control-button">??</button>
</div>
</div>
</div>
<style>
.video-container {
position: relative;
width: 100%;
height: 100vh;
}
.controls-overlay {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: linear-gradient(
to bottom,
rgba(0,0,0,0.7) 0%,
rgba(0,0,0,0) 20%,
rgba(0,0,0,0) 80%,
rgba(0,0,0,0.7) 100%
);
}
.bottom-controls {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 60px;
padding: 15px;
}
</style>實現(xiàn)思路解析:
層級控制:
視頻容器 (relative)
├── 視頻元素
└── 控制層 (absolute)
├── 頂部控件
├── 進(jìn)度條
└── 底部控件漸變遮罩設(shè)計:
- 使用 linear-gradient 創(chuàng)建漸變背景
- 頂部和底部漸變營造層次感
- 中間區(qū)域透明保持視頻可見
交互優(yōu)化:
- hover 時顯示控制層
- 使用 transition 實現(xiàn)平滑過渡
- 控件布局考慮觸控友好性
響應(yīng)式考慮:
- 控件尺寸使用相對單位
- 移動端適配大小按鈕
- 保持關(guān)鍵控件始終可見
適用場景:
固定元素覆蓋
- 全屏遮罩層
- 模態(tài)框背景
- 全屏加載指示器
廣告位布局
- 固定位置的廣告條
- 懸浮廣告
- 角標(biāo)定位
特殊交互界面
- 拖拽區(qū)域
- 自定義滾動條
- 圖片裁剪區(qū)域
優(yōu)點:
- 瀏覽器兼容性好
- 性能較好
- 精確控制位置
- 層級控制容易
缺點:
- 不夠靈活
- 需要手動計算位置
- 脫離文檔流可能影響其他元素
- 響應(yīng)式布局較難維護(hù)
calc() 方法
?? 詳見
./04-calc/index.html
使用 CSS 的 calc() 函數(shù)動態(tài)計算尺寸,可以混合使用不同單位進(jìn)行計算。
多欄布局:

<div class="article-container">
<h2>等寬三欄布局</h2>
<div class="three-columns">
<div class="column">第一欄</div>
<div class="column">第二欄</div>
<div class="column">第三欄</div>
</div>
<h2>混合寬度布局</h2>
<div class="mixed-columns">
<aside class="sidebar">側(cè)邊欄</aside>
<main class="main-content">主內(nèi)容區(qū)</main>
</div>
</div>
<style>
/* 三欄等寬布局 */
.three-columns {
display: flex;
gap: 30px;
}
.three-columns .column {
width: calc(100% / 3 - 20px);
/* 減去間距的權(quán)重 */
}
/* 混合寬度布局 */
.mixed-columns {
display: flex;
gap: 30px;
}
.mixed-columns .sidebar {
width: 300px;
}
.mixed-columns .main-content {
width: calc(100% - 330px);
/* 減去側(cè)邊欄寬度和間距 */
}
@media (max-width: 768px) {
.three-columns,
.mixed-columns {
flex-direction: column;
}
.three-columns .column,
.mixed-columns .sidebar,
.mixed-columns .main-content {
width: 100%;
}
}
</style>實現(xiàn)思路解析:
寬度計算原理:
/* 三欄等寬布局 */ width: calc(100% / 3 - 20px) /* 總寬度均分減去間距 */ /* 混合布局 */ width: calc(100% - 330px) /* 總寬度減去固定寬度和間距 */
間距處理策略:
- 使用 gap 屬性設(shè)置列間距
- 在計算時減去對應(yīng)的間距值
- 響應(yīng)式布局時重置間距
精確計算:
- 考慮邊框和內(nèi)邊距的影響
- 使用 box-sizing: border-box
- 預(yù)留邊距防止小數(shù)點誤差
響應(yīng)式設(shè)計:
- 斷點處切換為垂直布局
- 重置為 100% 寬度
- 保持間距的一致性
適用場景:
自定義滾動容器
- 減去固定高度的滾動區(qū)域
- 考慮padding和邊框的精確計算
- 動態(tài)高度的內(nèi)容區(qū)域
表單布局
- 標(biāo)簽和輸入框組合
- 多列表單布局
- 自適應(yīng)的輸入?yún)^(qū)域
媒體播放器
- 進(jìn)度條計算
- 控制欄布局
- 縮略圖網(wǎng)格
優(yōu)點:
- 精確控制
- 支持動態(tài)計算
- 可混合不同單位
- 適合精確的數(shù)學(xué)計算
缺點:
- 需要明確知道其他元素的尺寸
- IE9及以下不支持
- 計算規(guī)則可能復(fù)雜
- 性能略低于固定值
table 布局方法
?? 詳見
./05-table/index.html
使用 CSS table 布局屬性來分配空間,特別適合需要等高列或自動空間分配的場景。
數(shù)據(jù)表格:

<div class="table-container">
<div class="data-table">
<div class="table-header">
<div class="table-row">
<div class="table-cell">ID</div>
<div class="table-cell">項目名稱</div>
<div class="table-cell">進(jìn)度</div>
<div class="table-cell">狀態(tài)</div>
</div>
</div>
<div class="table-body">
<div class="table-row">
<div class="table-cell">001</div>
<div class="table-cell">項目重構(gòu)</div>
<div class="table-cell">
<div class="progress-bar">
<div class="progress" style="width: 75%"></div>
</div>
</div>
<div class="table-cell">
<span class="status">進(jìn)行中</span>
</div>
</div>
</div>
</div>
</div>
<style>
.data-table {
display: table;
width: 100%;
border-collapse: collapse;
}
.table-header {
display: table-header-group;
background: #f8f9fa;
}
.table-body {
display: table-row-group;
}
.table-row {
display: table-row;
}
.table-cell {
display: table-cell;
padding: 15px;
border-bottom: 1px solid #dee2e6;
vertical-align: middle;
}
/* 列寬設(shè)置 */
.table-cell:nth-child(1) { width: 80px; }
.table-cell:nth-child(2) { width: 200px; }
.table-cell:nth-child(3) { width: 200px; }
.table-cell:nth-child(4) { width: 120px; }
</style>實現(xiàn)思路解析:
表格結(jié)構(gòu)設(shè)計:
table-container
└── data-table (display: table)
├── table-header (table-header-group)
│ └── table-row
│ └── table-cell × N
└── table-body (table-row-group)
└── table-row × N
└── table-cell × N列寬控制策略:
- 固定列使用具體像素值
- 自適應(yīng)列不設(shè)寬度自動分配
- 使用 min-width 防止內(nèi)容擠壓
樣式優(yōu)化:
- 使用 border-collapse 處理邊框
- 通過 vertical-align 控制對齊
- nth-child 實現(xiàn)斑馬紋效果
特點利用:
- 自動等高列
- 自動垂直對齊
- 天然的響應(yīng)式行為
適用場景:
- 數(shù)據(jù)密集型界面
- 數(shù)據(jù)表格
- 列表展示
- 價格對比表
最佳實踐建議 ??
優(yōu)先使用 Flexbox
- 對于大多數(shù)現(xiàn)代網(wǎng)站,F(xiàn)lexbox 是最佳選擇
- 代碼簡單,維護(hù)方便
需要考慮兼容性時
- 如果需要支持舊版瀏覽器,可以考慮 calc() 或絕對定位方案
- 可以使用 @supports 進(jìn)行優(yōu)雅降級
復(fù)雜布局場景
- 對于復(fù)雜的網(wǎng)格布局,優(yōu)先使用 Grid
- 可以配合 Flexbox 實現(xiàn)更復(fù)雜的布局
總結(jié)
每種方法都有其適用場景,選擇合適的方法要考慮:
- 瀏覽器兼容性要求
- 布局復(fù)雜度
- 響應(yīng)式需求
- 維護(hù)成本
建議在實際項目中:
- 優(yōu)先使用 Flexbox/Grid 等現(xiàn)代方案
- 需要兼容老瀏覽器時,可以采用 calc()/絕對定位方案
- 結(jié)合項目實際情況選擇最合適的方案
到此這篇關(guān)于CSS實現(xiàn)元素?fù)螡M剩余空間的五種方法的文章就介紹到這了,更多相關(guān)css元素?fù)螡M剩余空間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
這篇文章主要介紹了CSS讓子元素div的高度填滿父容器的剩余空間的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨2021-02-23

