CSS中display flex整理(布局利器)

關(guān)于display:flex布局,有人了解頗深,我也是看著別人的東西學(xué)習(xí)的。
display:flex的布局是什么、基本概念之類的我根本就不了解,只會(huì)用。每次看到概念之類的東西,我都是掃一眼就過(guò)去。
第一個(gè)屬性和用法:flex-direction
我了解的方法有4個(gè):row(水平排列)、row-revese(水平反向排列)、column(垂直排列)、column-reserve(垂直反向排列)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div style="width:300px;border:1px solid red;display: flex;flex-direction: row;"> <div style="width: 100px;height: 100px;background-color: black;"></div> <div style="width: 100px;height: 100px;background-color: green;"></div> <div style="width: 100px;height: 100px;background-color: yellow;"></div> <div style="width: 100px;height: 100px;background-color: blue;"></div> </div> </body> </html>
上面的代碼和效果圖是屬性為row時(shí)的效果
注意:雖然是設(shè)置好的寬度,但是父容器只有300px,子div沒(méi)法達(dá)到100px,而是適應(yīng)父容器
只需要將 flex-direction: row代碼替換成flex-direction:row-revese 或者flex-direction:column 或則flex-direction:column-reserve,就可以得到不同的效果
下面是效果圖:
row-revese
-------
column
-------
column-reverse
-------
第二個(gè)屬性和用法:flex-wrap
這是換行屬性:nowrap(不換行)、wrap(換行)、wrap-reverse(方向換行)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div style="width:300px;border:1px solid red;display: flex;flex-wrap: wrap;"> <div style="width: 100px;height: 100px;background-color: black;"></div> <div style="width: 100px;height: 100px;background-color: green;"></div> <div style="width: 100px;height: 100px;background-color: yellow;"></div> <div style="width: 100px;height: 100px;background-color: blue;"></div> </div> </body> </html>
這是換行的代碼和效果圖
-------
將屬性flex-wrap: wrap 替換成nowrap(不換行)、wrap-reverse(方向換行)得到的效果圖如下:
nowrap
-----
wrap-reverse
---------
第三個(gè)屬性和用法:justify-content
包含的屬性有:justify-content: start | end | flex-start | flex-end | center | left | right | space-between | space-around | space-evenly | stretch | safe | unsafe | baseline | first baseline | last baseline
(這些屬性都是抄別人的)
flex-start(默認(rèn)值):左對(duì)齊;
左對(duì)齊
flex-end:右對(duì)齊
右對(duì)齊
center:居中;
居中對(duì)齊space-between:兩端對(duì)齊,項(xiàng)目之間間隔相等;
兩端對(duì)齊space-around:每個(gè)項(xiàng)目?jī)蓚?cè)的間隔相等,即項(xiàng)目之間的間隔比項(xiàng)目與邊框的間隔大一倍。
兩側(cè)間隔相等在下小白,也是從那里偷來(lái)了很多東西
總結(jié)
以上所述是小編給大家介紹的CSS中display flex整理(布局利器) ,希望對(duì)大家有所幫助!
相關(guān)文章
詳解CSS中的display:flex||inline-flex屬性
這篇文章主要給大家介紹了CSS中的display:flex和display:inline-flex屬性,文中分別通過(guò)兩段實(shí)例代碼給大家介紹了display:flex和display:inline-flex的使用效果,感興趣的2016-12-20- Flex是Flexible Box的縮寫(xiě),翻譯成中文就是“彈性盒子”,用來(lái)為盒裝模型提供最大的靈活性,這篇文章主要介紹了前端彈性布局神器display:flex的相關(guān)知識(shí),需要的朋友可以參2023-06-12