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

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

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

闞瓊音 2025-04-12 科技 21 次瀏覽 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ù):

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

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

最新国产AV无码专区亚洲AV| 国产精品啪啪啪综合网| 国产女人自拍传媒| 欧美与欧洲交XXXX免费观看| a级一级久久毛片国产| 国产精品无码AV在线播放| 亚洲传媒黄色毛片| 国产亚洲欧欧| 黄片久久成人毛片| 国产精品巨作无遮拦| 综合色区中文字幕首页亚洲| 久久97性感人妻| 国产伦精品一区二区三区网站| 一本大道久久a久久综合| 婷婷成人网站在线观看| 久久毛片艾草一级| 91精品国产福利姬喷水福利在线| 亚洲色图第16页| 一区二区在线不卡国产| 日韩无码一卡二卡| 先锋影音av免费资源| 激情图片综合网| 国产一区二区精品久久岳| 99国产精品视频一区二区69堂| 秋霞干成人网站| 久久久美综合品牌| 国产精品无码一区二区老黄瓜| 色香色欲综合| 人妻热欧洲| 精品少妇一区二区三区在线| 亚洲中文字幕在线第二页| 中文字幕精品无码一区二区辛尤里| 少妇无码一区二区三区免费| 国产日韩欧美中文字幕一区二区| 哦哦哦舒服爽视频网站| 免费精品国产伊人| 欧美人一区二区三区| 国产91久久精品成人看网站| 精品免费在线播放| 欧美日韩一本大道香蕉欧美| 精品国产污污免费网站入口|