久久午夜无码,日日射天天射五月丁香婷婷我来了 ,欧美黑人又长又粗在线视频,午夜天网站

flex布局阮一峰(css布局方案匯總28個實例圖文并茂)

flex布局阮一峰(css布局方案匯總28個實例圖文并茂)

闞瓊音 2025-04-12 科技 19 次瀏覽 0個評論
css布局方案匯總(28個實例圖文并茂)

簡介

布局在我們前端日常開發(fā)來說是非常重要的,一個好的布局能簡化代碼的同時還能提高網(wǎng)頁的性能。常見的布局方法有浮動(float)布局、絕對定位(position)布局、表格布局(table)、彈性(flex)布局、網(wǎng)格(grid)布局。關(guān)于布局方法本文不做詳細講解,筆者推薦看阮一峰老師 flex布局教程 和阮一峰老師 grid布局教程。

本文主要講解水平垂直居中、單欄布局、雙欄布局、三欄布局一些項目中常用的布局方案。

本文代碼全部使用codepen在線代碼工具進行演示。

居中

居中在我們?nèi)粘9ぷ髦羞€是會經(jīng)常碰到。

水平居中

對于水平居中一般可以使用如下四種方式

對于行內(nèi)元素我們可以在父元素上設(shè)置text-align:center;來實現(xiàn)。對于定長塊級元素我們可以使用margin: 0 auto;來實現(xiàn)。我們可以在父元素上使用flex布局來實現(xiàn)。我們可以在父元素上使用grid布局來實現(xiàn)。<div class="div1"> <span>行內(nèi)元素水平居中</span></div><div class="div2"> <span>行內(nèi)元素水平居中</span> <div>塊級元素水平居中</div></div><div class="div3"> <span>行內(nèi)元素水平居中</span> <div>塊級元素水平居中</div></div><div class="div4">塊級元素水平居中</div>.div1 { text-align: center;}.div2 { display: flex; justify-content: center;}.div3 { display: grid; justify-content: center;}.div4 { width: 130px; margin: 0 auto;}

效果如下

css布局方案匯總(28個實例圖文并茂)

點擊查看代碼運行實例

垂直居中

對于垂直居中一般可以使用如下三種方式

我們可以在父元素上設(shè)置line-height等于height來實現(xiàn)。我們可以在父元素上使用flex布局來實現(xiàn)。我們可以在父元素上使用grid布局來實現(xiàn)。我們可以在父元素上使用table布局來實現(xiàn)。<div class="div1"> <span>行內(nèi)元素垂直居中</span><!-- <div>塊級元素垂直居中</div> --></div><div class="div2"> <span>行內(nèi)元素垂直居中</span> <div>塊級元素垂直居中</div></div><div class="div3"> <span>行內(nèi)元素垂直居中</span> <div>塊級元素垂直居中</div></div><div class="div4"> <span>行內(nèi)元素垂直居中</span> <div>塊級元素垂直居中</div></div>.div1 { height: 100px; background: lightgreen; line-height: 100px;}.div2 { height: 100px; background: lightblue; display: flex; align-items: center;}.div3 { height: 100px; background: lightgreen; display: grid; align-content: center;}.div4 { height: 100px; background: lightblue; display: table-cell; vertical-align: middle;}

效果如下

css布局方案匯總(28個實例圖文并茂)

點擊查看代碼運行實例

水平垂直同時居中

比如我們想實現(xiàn)如下水平垂直同時居中的效果

css布局方案匯總(28個實例圖文并茂)

實現(xiàn)水平垂直同時居中我們可以使用絕對定位、table布局、flex布局 或 grid布局來實現(xiàn)。

首先我們創(chuàng)建一個需要居中的盒子。

<div class="box"></div>純絕對定位.box { position: absolute; width: 200px; height: 100px; background: red; top: 0; left: 0; right: 0; bottom: 0; margin: auto;}

點擊查看代碼運行實例

絕對定位加負外邊距

這種方式需要知道居中元素的具體寬高,不然負的margin沒法設(shè)置。

.box { position: absolute; width: 200px; height: 100px; background: red; left: 50%; top: 50%; margin-left: -100px; margin-top: -50px;}

點擊查看代碼運行實例

絕對定位加平移

這種平移的方式就不需要考慮居中盒子的具體寬高了。

.box { position: absolute; width: 200px; height: 100px; background: red; left: 50%; top: 50%; transform: translate(-50%, -50%);}

點擊查看代碼運行實例

使用flex實現(xiàn)html,body { height: 100%; }body { background: gray; display: flex; align-items: center; justify-content: center;}.box { width: 200px; height: 100px; background: red;}

點擊查看代碼運行實例

使用grid實現(xiàn)html,body { height: 100%; }body { background: gray; display: grid;/* align-content: center; justify-content: center; */ /* align-content和justify-content的簡寫 */ place-content: center;}.box { width: 200px; height: 100px; background: red;}

點擊查看代碼運行實例

使用table加外邊距實現(xiàn)

使用table布局需要注意如下

display: table時padding會失效display: table-row時margin、padding同時失效display: table-cell時margin會失效<div class="box"> <div class="child"></div></div>.box { background: red; height: 300px; width: 600px; display: table-cell; vertical-align: middle;}.child { width: 200px; height: 200px; background: lightgreen; margin: 0 auto;}

點擊查看代碼運行實例

單欄布局

單欄布局我們常用在網(wǎng)頁框架上,一般我們把網(wǎng)頁分為 header、content、footer三部分。

css布局方案匯總(28個實例圖文并茂)

在不同的項目我們可能對這三部分的樣式需求有所差別,比如需要頂部固定、需要底部固定等等。

頂?shù)撞慷疾还潭?p >比如想實現(xiàn)如下效果,footer在內(nèi)容不足的時候吸附在窗口底部,當(dāng)內(nèi)容多的時候又可以被抵到窗口下面。

css布局方案匯總(28個實例圖文并茂)

使用padding加負margin實現(xiàn)<div class="wrap"> <div class="header">header</div> <div class="content">content</div></div><div class="footer">footer</div>html, body { height: 100%; margin: 0;}.wrap { min-height: 100%; padding-bottom: 50px; overflow: auto; box-sizing: border-box;}.header { height: 50px; background: lightblue;}.content { background: lightpink; /* 這里的高度只是為了模擬內(nèi)容多少 */ height: 100px; /* height: 1000px; */}.footer { height: 50px; background: lightgreen; margin-top: -50px;}

點擊查看代碼運行實例

使用flex實現(xiàn)<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div></div>html, body { height: 100%; margin: 0;}.wrap { display: flex; flex-direction: column; min-height: 100%;}.header { height: 50px; background: lightblue;}.content { background: lightpink; /* 這里的高度只是為了模擬內(nèi)容多少 */ height: 100px; /* height: 1000px; */ flex-grow: 1;}.footer { height: 50px; background: lightgreen;}

點擊查看代碼運行實例

頂部固定使用padding加負margin加fixed實現(xiàn)頂部固定布局<div class="header">header</div><div class="wrap"> <div class="content">content</div></div><div class="footer">footer</div>html, body { height: 100%; margin: 0;}.header { height: 50px; background: lightblue; position: fixed; width: 100%;}.wrap { min-height: 100%; padding-bottom: 50px; overflow: auto; box-sizing: border-box;}.content { margin-top: 50px; background: lightpink; /* 這里的高度只是為了模擬內(nèi)容多少 */ height: 100px; /* height: 1000px; */}.footer { height: 50px; background: lightgreen; margin-top: -50px;}

點擊查看代碼運行實例

使用flex加fixed定位實現(xiàn)<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div></div>html, body { height: 100%; margin: 0;}.wrap { display: flex; min-height: 100%; flex-direction:column;}.header { height: 50px; background: lightblue; position: fixed; width: 100%;}.content { background: lightpink; /* 這里的高度只是為了模擬內(nèi)容多少 */ /* height: 100px; */ height: 1000px; margin-top: 50px; flex-grow: 1;}.footer { height: 50px; background: lightgreen;}

點擊查看代碼運行實例

底部固定使用padding加負margin實現(xiàn)底部固定布局<div class="wrap"> <div class="header">header</div> <div class="content">content</div></div><div class="footer">footer</div>html, body { height: 100%; margin: 0;}.wrap { height: 100%; padding-bottom: 50px; overflow: auto; box-sizing: border-box;}.header { height: 50px; background: lightblue;}.content { background: lightpink; height: 100px; height: 1000px;}.footer { height: 50px; background: lightgreen; margin-top: -50px;}

點擊查看代碼運行實例

使用flex加fixed定位實現(xiàn)<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div></div>html, body { height: 100%; margin: 0;}.wrap { display: flex; min-height: 100%; flex-direction:column;}.header { height: 50px; background: lightblue;}.content { background: lightpink; /* 這里的高度只是為了模擬內(nèi)容多少 */ /* height: 100px; */ height: 1000px; flex-grow: 1; margin-bottom: 50px;}.footer { height: 50px; background: lightgreen; position: fixed; width: 100%; bottom: 0;}

點擊查看代碼運行實例

頂?shù)撞慷脊潭ㄊ褂胒ixed實現(xiàn)頂?shù)撞抗潭ú季?lt;div class="header">header</div><div class="content">content</div><div class="footer">footer</div>復(fù)制代碼html, body { height: 100%; margin: 0;}.header { height: 50px; background: lightblue; position: fixed; width: 100%;}.content { background: lightpink; padding-top: 50px; padding-bottom: 50px; /* height: 100px; */ height: 1000px;}.footer { height: 50px; background: lightgreen; position: fixed; bottom: 0; width: 100%;}

點擊查看代碼運行實例

使用flex加fixed定位實現(xiàn)<div class="wrap"> <div class="header">header</div> <div class="content">content</div> <div class="footer">footer</div></div>html, body { height: 100%; margin: 0;}.wrap { display: flex; min-height: 100%; flex-direction:column;}.header { height: 50px; background: lightblue; position: fixed; width: 100%;}.content { background: lightpink; /* 這里的高度只是為了模擬內(nèi)容多少 */ /* height: 100px; */ height: 1000px; flex-grow: 1; margin-bottom: 50px; margin-top: 50px;}.footer { height: 50px; background: lightgreen; position: fixed; width: 100%; bottom: 0;}

點擊查看代碼運行實例

兩欄布局

兩欄布局就是一邊固定,另外一邊自適應(yīng),效果如下

css布局方案匯總(28個實例圖文并茂)

實現(xiàn)兩欄布局的方法也有很多,筆者接下來介紹用的比較多的幾種方式。

左 float,然后右 margin-left(右邊自適應(yīng))<div class="aside"></div><div class="main"></div>div { height: 500px;}.aside { width: 300px; float: left; background: yellow;}.main { background: aqua; margin-left: 300px;}

點擊查看代碼運行實例

右 float,然后右 margin-right(左邊自適應(yīng))<div class="aside"></div><div class="main"></div>div { height: 500px;}.aside { width: 300px; float: right; background: yellow;}.main { background: aqua; margin-right: 300px;}

點擊查看代碼運行實例

absolute定位加margin-left(右邊自適應(yīng))<div class="wrap"> <div class="aside"></div> <div class="main"></div></div>div { height: 500px;}.wrap { position: relative;}.aside { width: 300px; background: yellow; position: absolute;}.main { background: aqua; margin-left: 300px;}

點擊查看代碼運行實例

absolute定位加margin-right(左邊自適應(yīng))<div class="wrap"> <div class="aside"></div> <div class="main"></div></div>div { height: 500px;}.wrap { position: relative;}.aside { width: 300px; background: yellow; position: absolute; right: 0;}.main { background: aqua; margin-right: 300px;}

點擊查看代碼運行實例

使用flex實現(xiàn)<div class="wrap"> <div class="aside"></div> <div class="main"></div></div>div { height: 500px;}.wrap { display: flex;}.aside { flex: 0 0 300px; background: yellow; }.main { background: aqua; flex: 1 1;}

點擊查看代碼運行實例

使用grid實現(xiàn)<div class="wrap"> <div class="aside"></div> <div class="main"></div></div> height: 500px;}.wrap { display: grid; grid-template-columns: 300px auto;}.aside { background: yellow; }.main { background: aqua;}

點擊查看代碼運行實例

三欄布局

三欄布局就是兩邊固定,中間自適應(yīng)布局,效果如下

css布局方案匯總(28個實例圖文并茂)

實現(xiàn)三欄布局的方法也有很多,筆者接下來介紹用的比較多的幾種方式。

position + margin-left + margin-right實現(xiàn)三欄布局<div class="left"></div><div class="middle"></div><div class="right"></div>html,body { margin: 0;}div { height: 500px;}.left { position: absolute; left: 0; top: 0; width: 200px; background: green;}.right { position: absolute; right: 0; top: 0; width: 200px; background: red;}.middle { margin-left: 200px; margin-right: 200px; background: lightpink;}

點擊查看代碼運行實例

float + margin-left + margin-right實現(xiàn)三欄布局<div class="left"></div><div class="right"></div><div class="middle"></div>html,body { margin: 0;}div { height: 500px;}.left { width: 200px; background: green; float: left;}.right { width: 200px; background: yellow; float: right;}.middle { margin-left: 200px; margin-right: 200px; background: lightpink;}

點擊查看代碼運行實例

flex實現(xiàn)三欄布局<div class="wrap"> <div class="left"></div> <div class="middle"></div> <div class="right"></div></div>html,body { margin: 0;}div { height: 500px;}.wrap { display: flex;}.left { flex: 0 0 200px; background: green;}.right { flex: 0 0 200px; background: yellow;}.middle { background: lightpink; flex: 1 1;}

點擊查看代碼運行實例

grid實現(xiàn)三欄布局<div class="wrap"> <div class="left"></div> <div class="middle"></div> <div class="right"></div></div>html,body { margin: 0;}div { height: 500px;}.wrap { display: grid; grid-template-columns: 200px auto 200px;}.left { background: green;}.right { background: yellow;}.middle { background: lightpink;}

點擊查看代碼運行實例

圣杯布局

圣杯布局在項目中基本上不會再使用了,在面試中我們會經(jīng)常碰到,所以需要了解。

主要用到了浮動和和相對定位。

<div class="container"> <div class="content">中間內(nèi)容</div> <div class="left">左側(cè)區(qū)域</div> <div class="right">右側(cè)區(qū)域</div></div>div { height: 500px;}.container { padding: 0 200px 0 200px; border: 1px solid black;}.content { float: left; width: 100%; background: #f00;}.left { width: 200px; background: #0f0; float: left; margin-left: -100%; position: relative; left: -200px;}.right { width: 200px; background: #00f; float: left; margin-left: -200px; position: relative; right: -200px;}

點擊查看代碼運行實例

雙飛翼布局

雙飛翼布局在項目中基本上不會再使用了,在面試中我們會經(jīng)常碰到,所以需要了解。

主要用到了浮動。

<div class="main"> <div class="content">content</div></div><div class="left">left</div><div class="right">right</div>div { height: 500px;}.main { float: left; width: 100%; background: #f00;}.main .content { /* margin、padding這兩種方式都可以 */ /* margin-left: 200px; margin-right: 300px; */ padding-left: 200px; padding-right: 300px;}.left { width: 200px; background: #0f0; float: left; margin-left: -100%;}.right { width: 200px; background: #00f; float: left; margin-left: -200px;}

點擊查看代碼運行實例

總結(jié)

因為flex和grid布局方法已經(jīng)很強大了,日常工作中99%的布局都可以使用這兩種方式來實現(xiàn)。所以筆者建議能使用flex或者grid布局方法實現(xiàn)的就盡量使用這兩種布局方式實現(xiàn)。因為不僅簡單而且負面作用也很少。

浮動布局和絕對定位布局會導(dǎo)致元素脫離文檔流,會帶來一些負面作用,有時會導(dǎo)致一些意想不到的結(jié)果。

關(guān)于flex布局的兼容性和grid布局的兼容性,目前已經(jīng)支持的很好了,大家可以放心使用。

flex布局的兼容性

css布局方案匯總(28個實例圖文并茂)

grid布局的兼容性

css布局方案匯總(28個實例圖文并茂)

后記

感謝小伙伴們的耐心觀看,本文為筆者個人學(xué)習(xí)筆記,如有謬誤,還請告知,萬分感謝!如果本文對你有所幫助,還請點個關(guān)注點個贊~,您的支持是筆者不斷更新的動力!

轉(zhuǎn)載請注明來自夕逆IT,本文標(biāo)題:《flex布局阮一峰(css布局方案匯總28個實例圖文并茂)》

每一天,每一秒,你所做的決定都會改變你的人生!

發(fā)表評論

快捷回復(fù):

評論列表 (暫無評論,19人圍觀)參與討論

還沒有評論,來說兩句吧...

乱人伦激情视频| 伊人久久综合网亚洲| 亚洲女同中文字幕| 98国产福利精品小视频| 一本大道 无码AV| 亚洲欧美一区二区三区乱码 | 亚洲s免费| 国产成人综合日韩色欲| 久久五月丁香中文字幕 | 国产流白浆影院| 亚洲AV日韩AV蜜桃在线播放| 五月综合av| 久久久久亚洲AV无码专区乐透| 国产精品黑色丝袜久久| 久久与精品| 久久亚洲中文字幕精品国产二区| 亚洲日韩人妻精品| 超碰超碰免费在线| 四虎海外免费| 开心 色 播 欧美| 无码毛片一区2区| 亚洲欧美日韩高清一区二区三区| 日韩激情免费视频| 91精品国产综合久久久久五月天| 日日躁夜夜躁狠狠躁| 色悠悠国产在线| 亚洲午夜精品久久久久久白云 | 欧美日韩精品综合在线一区二区| 成人综合亚洲欧美一区| 正定县| 久草AV一区成人| 中文字幕成人免费| 亚洲免费在线视频| 亚洲欧美色综合图小说| 国产精品成熟老女人| 在线观看欧美一级片| 日韩精品无码一区二| 国产无遮挡又爽又大又粗| 亚州色站综合| AAA少妇高潮大片免费下载| 亚洲日韩福利|