日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Flexible Box布局基础知识详解

發(fā)布時(shí)間:2025/5/22 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Flexible Box布局基础知识详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.基本概念,借用阮一峰老師的一張圖:

容器默認(rèn)存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點(diǎn))叫做main start,結(jié)束位置叫做main end;交叉軸的開始位置叫做cross start,結(jié)束位置叫做cross end。

項(xiàng)目默認(rèn)沿主軸排列。單個(gè)項(xiàng)目占據(jù)的主軸空間叫做main size,占據(jù)的交叉軸空間叫做cross size。

2.容器的基本屬性

flex-direction flex-wrap flex-flow justify-content align-items align-content

?2.1 flex-direction? 屬性決定主軸的方向 (及行排列)

.box{flex-direction:row | row-reverse | column |column-reverse /*有四個(gè)值 分別的顯示效果*/}
默認(rèn)值:row

?

html5實(shí)現(xiàn)代碼:<div class="box"><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div></div> css3部分實(shí)現(xiàn)代碼:body{margin: 0 auto;width: 1000px;}.box{background: gold;margin: 1px;display: flex; /*必須設(shè)置這個(gè)*/flex-direction: row; /*一排的方式排列*/}.box-item{width: 100px;height: 100px;line-height: 100px;background: #ccc;color: white;text-align: center;margin: 5px;}

?實(shí)現(xiàn)效果:

如果css3改成 flex-direction: row-reverse;

?

其他兩個(gè)屬性類推;

2.2 flex-wrap? 定義如果一條軸線排不下,如何換行

.box{flex-wrap:nowrap | wrap | wrap-reverse;} 默認(rèn):nowrap html部分代碼:<div class="box1"><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div></div> css 部分代碼:.box1{background: gold;margin: 1px;display: flex;flex-flow: wrap;}.box-item{width: 100px;height: 100px;line-height: 100px;background: #ccc;color: white;text-align: center;margin: 5px;}

效果:

?這是換行的效果,其他效果可以嘗試;

2.3 flex-flow 是flex-direction 和?flex-wrap的縮寫;所以當(dāng)獨(dú)寫上面的要寫兩個(gè)

默認(rèn)值為row norap

.box{flex-flow: flex-direction || flex-wrap}

?2.4 justify-content 屬性定義了項(xiàng)目在主軸上的對(duì)齊方式

.box{justify-content:flex-start | flex-end | center | space-between | space-around;} flex-start(默認(rèn)值):左對(duì)齊flex-end:右對(duì)齊center: 居中space-between:兩端對(duì)齊,項(xiàng)目之間的間隔都相等。space-around:每個(gè)項(xiàng)目兩側(cè)的間隔相等。所以,項(xiàng)目之間的間隔比項(xiàng)目與邊框的間隔大一倍。

?試一種效果:

html5代碼:
<div class="box2"><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div></div> css3代碼:
.box2
{background: gold;margin: 1px;display: flex;justify-content: center; /**可以換換其他的屬性值*/}.box-item{width: 100px;height: 100px;line-height: 100px;background: #ccc;color: white;text-align: center;margin: 5px;}

效果圖:

其他的可以自己試試:

flex-start(默認(rèn)值):左對(duì)齊flex-end:右對(duì)齊center: 居中space-between:兩端對(duì)齊,項(xiàng)目之間的間隔都相等。space-around:每個(gè)項(xiàng)目兩側(cè)的間隔相等。所以,項(xiàng)目之間的間隔比項(xiàng)目與邊框的間隔大一倍。

2.5 align-items 定義項(xiàng)目在交叉軸上如何對(duì)齊(縱軸)

.box{align-items:flex-start | flex-end |center | baseline |stretch} html5代碼:<div class="box3"><div class="box-item">1</div><div class="box-item item-tall">2</div><div class="box-item">3</div><div class="box-item">4</div><div class="box-item">1</div><div class="box-item item-tall">2</div><div class="box-item">3</div><div class="box-item">4</div> <div class="box-item">1</div><div class="box-item item-tall">2</div><div class="box-item">3</div><div class="box-item">4</div> </div> css3代碼:.box3{background: gold;margin: 1px;display: flex;align-items:flex-end; /*可以換其他值看看*/flex-wrap: wrap;}.box-item{width: 100px;height: 100px;line-height: 100px;background: #ccc;color: white;text-align: center;margin: 5px;}.item-tall{height: 200px; /*交叉軸,高度不一*/line-height: 200px;}

效果:

其他的可以自己試試:

flex-start:交叉軸的起點(diǎn)對(duì)齊。flex-end:交叉軸的終點(diǎn)對(duì)齊。center:交叉軸的中點(diǎn)對(duì)齊。baseline: 項(xiàng)目的第一行文字的基線對(duì)齊。stretch(默認(rèn)值):如果項(xiàng)目未設(shè)置高度或設(shè)為auto,將占滿整個(gè)容器的高度。

?

2.6 align-content? 屬性定義了多根軸線(多行)的對(duì)齊方式,如果項(xiàng)目只有一根軸線,該屬性起不來作用

.box {align-content: flex-start | flex-end | center | space-between | space-around | stretch;} html代碼:<div class="box3 box3-tall"><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div><div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div> <div class="box-item">1</div><div class="box-item">2</div><div class="box-item">3</div><div class="box-item">4</div> </div> css代碼:.box3{background: gold;margin: 1px;display: flex;flex-wrap: wrap;align-content: space-around;}.box-tall {height: 300px;}.box-item{width: 100px;height: 100px;line-height: 100px;background: #ccc;color: white;text-align: center;margin: 5px;}

效果:

其他的可以自己試試:

flex-start:與交叉軸的起點(diǎn)對(duì)齊。flex-end:與交叉軸的終點(diǎn)對(duì)齊。center:與交叉軸的中點(diǎn)對(duì)齊。space-between:與交叉軸兩端對(duì)齊,軸線之間的間隔平均分布。space-around:每根軸線兩側(cè)的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。stretch(默認(rèn)值):軸線占滿整個(gè)交叉軸。

3.容器里子元素的屬性

order屬性定義項(xiàng)目的排列順序。數(shù)值越小,排列越靠前,默認(rèn)為0。 flex-grow屬性定義項(xiàng)目的放大比例,默認(rèn)為0,即如果存在剩余空間,也不放大。 flex-shrink屬性定義了項(xiàng)目的縮小比例,默認(rèn)為1,即如果空間不足,該項(xiàng)目將縮小。 flex-basis屬性定義了在分配多余空間之前,項(xiàng)目占據(jù)的主軸空間(main size)。瀏覽器根據(jù)這個(gè)屬性,計(jì)算主軸是否有多余空間。它的默認(rèn)值為auto,即項(xiàng)目的本來大小。
align-self屬性允許單個(gè)項(xiàng)目有與其他項(xiàng)目不一樣的對(duì)齊方式,可覆蓋align-items屬性。默認(rèn)值為auto,表示繼承父元素的align-items屬性,如果沒有父元素,則等同于stretch

通常我們定義flex:1;

表示的就是這三個(gè);

3.1 order 屬性

html代碼: <div class="box4"><div class="box-item1 ">1</div><div class="box-item1 order">2</div> /*注意是第二個(gè)元素有Order類*/</div> css代碼:.box4{background: gold;margin: 1px;display: flex;}.box-item1{flex: 1;line-height: 100px;background: #ccc;color: white;text-align: center;margin: 5px;}.order{background: blue;order: -1; }

效果圖:

如果我這樣設(shè)置:

.order{background: blue;order: -1;flex-grow:2; /*多了這個(gè)*/}

其他的去試一試,大概就是這樣

轉(zhuǎn)載于:https://www.cnblogs.com/sulishibaobei/p/7450769.html

總結(jié)

以上是生活随笔為你收集整理的Flexible Box布局基础知识详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。