一、FlexBox
概念
FlexBox
翻譯為彈性盒子。- 彈性盒子是一種用于按行或按列布局元素的一維布局方式。
- 元素可以膨脹以填充額外的空間,收縮以適應更小的空間。
- 我們使用
FlexBox
來進行布局的方案稱為flex
布局。
二、flex
布局的重要概念
- 兩個重要的概念
- 開啟
flex
布局的元素叫做flex container
flex container
里面的子元素叫做flex item
- 開啟
- 當
flex container
中的子元素變成flex item
時,具備以下特點flex item
的布局將受到flex container
屬性設置來控制和布局。flex item
不再嚴格區分塊元素和行內級元素。flex item
默認情況下是包裹 內容的,但是可以設置寬度和高度。
- 設置
display
屬性為flex
或者inline-flex
可以為flex container
。flex
:flex container
以block-level
形式存在。inline-flex
:flex container
以inline-level
形式存在。
- 案例1:設置
display
屬性為flex
<!DOCTYPE html>
<html><head><title>Document</title><style>.box {display: flex;background-color: aqua;}</style></head><body>AAA<div class="box"><div class="item">box1</div><div class="item">box2</div><div class="item">box3</div><div class="item">box4</div></div>BBB</body>
</html>
- 案例2:設置
display
屬性為inline-flex
<!DOCTYPE html>
<html><head><title>Document</title><style>.box {display: inline-flex;background-color: aqua;}</style></head><body>AAA<div class="box"><div class="item">box1</div><div class="item">box2</div><div class="item">box3</div><div class="item">box4</div></div>BBB</body>
</html>
三、flex的布局模型
- 默認以
main axis
方向排序
- 應用在
flex container
上的css
屬性flex-flow
flex-direction
flex-wrap
justify-content
align-items
align-content
- 應用在
flex item
上的css
屬性flex-grow
flex-basis
flex-shrink
order
align-self
flex
四、應用在 flex container
上的 css
屬性
1、flex-direction
flex itsms
默認都是沿著main axis
(主軸)從main start
開始往main end
方向排布-
flex-direction
決定了main axis
的方向,有四個取值
-
row
(默認值)
-
row-reverse
:row
的反轉
-
column
:列成為主軸方向
-
column-reverse
:列主軸反轉
-
-
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;/* 修改主軸的方向 *//* flex-direction: row; 默認值 *//* flex-direction: row-reverse; *//* flex-direction: column; */flex-direction: column-reverse;}.item {width: 100px;height: 100px;background-color: aquamarine;}.item1 {background-color: orange;}.item2 {background-color: red;}.item3 {background-color: blueviolet;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div></div></body>
</html>
2、flex-wrap
-
flex-wrap
決定了flex container
顯示單行還是多行。
-
nowrap
:(默認值)單行
-
wrap
:多行
-
wrap-reverse
:多行(對比wrap
,cross start
與 )
-
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;/* 決定了 flex container 顯示單行還是多行。 *//* flex-wrap: nowrap;默認值 *//* flex-wrap: wrap; */flex-wrap: wrap-reverse;}.item {width: 100px;height: 100px;background-color: aquamarine;}.item1 {background-color: orange;}.item2 {background-color: red;}.item3 {background-color: blueviolet;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div><div class="item item1">box5</div><div class="item item2">box6</div><div class="item item3">box7</div><div class="item item4">box8</div></div></body>
</html>
3、flex-flow
-
flex-flow
屬性是flex-direction
和flex-wrap
的縮寫。
- 順序任何,并且都可以省略
- 順序任何,并且都可以省略
4、justify-content
justify-content
決定了flex item
在main axis
上的對齊方式-
flex-start
:(默認值)與main start
對齊
-
flex-end
:與main end
對齊
-
center
: 居中
-
space-between
:flex items
之間的距離相等- 與
main start
、main end
兩端對齊
-
space-around
:flex items
之間的距離相等flex items
與main start
、main end
之間的距離是flex items
之間距離的一半
-
space-evenly
:flex items
之間的距離相等flex items
與main start
、main end
之間的距離是flex items
之間的距離
-
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;/* justify-content 決定了 flex item 在 main axis 上的對齊方式 *//* justify-content: flex-start; 默認值 *//* justify-content: flex-end; *//* justify-content: center; *//* justify-content: space-between; *//* justify-content: space-around; */justify-content: space-evenly;}.item {width: 100px;height: 100px;background-color: aquamarine;}.item1 {background-color: orange;}.item2 {background-color: red;}.item3 {background-color: blueviolet;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div></div></body>
</html>
5、align-item
align-item
決定了flex items
在cross axis
上的對齊方式
-
-
normal
:彈性布局中,效果和strench
一樣
-
stretch
:當flex items
在cross axis
方向的size
為auto
時候,會自動拉伸至填充flex container
-
flex-start
:與cross start
對齊
-
flex-end
:與cross end
對齊
-
center
:居中對齊
-
baseline
:于基準線對齊
-
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;align-items: normal;/* align-items: stretch; *//* align-items: flex-start; *//* align-items: flex-end; *//* align-items: baseline; */}.item {width: 100px;height: 100px;background-color: aquamarine;height: auto;}.item1 {background-color: orange;}.item2 {background-color: red;}.item3 {background-color: blueviolet;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div></div></body>
</html>
6、align-content
-
align-content
決定了多行flex items
在cross axis
上的對齊方式,用法和justify-content
類似
-
stretch
:(默認值),與align-item
的stretch
類似
-
flex-strat
:與cross start
對齊
-
flex-end
:與cross end
對齊
-
center
:居中對齊
-
space-between
:flex items
之間的距離相等flex items
與cross start
和cross end
對齊
-
space-around
:flex items
之間的距離相等flex items
與cross start
和cross end
之間的距離是flex items
之間距離的一半
-
space-evenly
:flex items
之間的距離相等flex items
與cross start
和cross end
之間的距離 等于flex items
之間的距離
-
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;flex-wrap: wrap;align-content: stretch;/* align-content: flex-start; *//* align-content: flex-end; *//* align-content: center; *//* align-content: space-between; *//* align-content: space-around; *//* align-content: space-evenly; */}.item {width: 120px;height: 120px;height: auto;background-color: aquamarine;}.item1 {background-color: orange;}.item2 {background-color: red;}.item3 {background-color: blueviolet;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div><div class="item item1">box5</div><div class="item item2">box6</div><div class="item item3">box7</div><div class="item item4">box8</div><div class="item item1">box9</div><div class="item item2">box10</div><div class="item item3">box11</div><div class="item item4">box12</div></div></body>
</html>
五、應用在 flex item
上的 css
屬性
1、order
-
order
決定了flex item
的排列順序
- 可以設置任意整數(正整數、負整數、0),值越小就越
- 數字越小,位置越靠前;數字越大,位置越靠后
- 默認值是
0
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;flex-wrap: wrap;}.item {width: 120px;height: 120px;background-color: aquamarine;}.item1 {background-color: orange;order: 2;}.item2 {background-color: red;order: 3;}.item3 {background-color: blueviolet;order: 1;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div></div></body>
</html>
2、align-self
-
flex-items
可以通過align-self
覆蓋flex container
設置的align-items
-
auto
:遵從flex container
設置的align-items
-
可以設置的值,效果和
align-items
一致-
stretch
-
flex-start
-
flex-end
-
center
-
baseline
-
-
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;}.item {width: 120px;height: 120px;background-color: aquamarine;}.item1 {background-color: orange;/* align-self: center; */}.item2 {background-color: red;/* align-self: auto; *//* align-self: stretch;height: auto; *//* align-self: flex-start; *//* align-self: flex-end; *//* align-self: center; */align-self: baseline;}.item3 {background-color: blueviolet;/* align-self: center; */}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div></div></body>
</html>
3、flex-grow
-
flex-grow
決定了flex items
如何拓展(拉伸、增長)
- 可以設置任意非負數(正小數、正整數、
0
),默認值是0
- 當
flex container
在main axis
上有剩余size
時,flex-grow
才會生效
- 如果所有
flex items
的flex-grow
總和sum
超過1
,每個flex item
拓展的 size 為:flex container 的剩余 size * ( flex-grow / sum )
- 可以設置任意非負數(正小數、正整數、
-
flex items
拓展后的最終size
不能超過max-width
/max-height
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;}.item {width: 120px;height: 120px;background-color: aquamarine;}.item1 {background-color: orange;flex-grow: 2;}.item2 {background-color: red;flex-grow: 0.2;}.item3 {background-color: blueviolet;flex-grow: 0.5;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div></div></body>
</html>
4、flex-shrink
-
flex-shrink
決定了flex items
如何收縮(縮小)- 可以設置任意非負數(正小數、正整數、
0
),默認值是1
- 當
flex items
在main axis
上 超過了flex container
的size
時,flex-shrink
才會生效
- 可以設置任意非負數(正小數、正整數、
-
如果所有
flex items
的flex-shrink
總和sum
超過1
,每個flex item
收縮的 size 為:flex items
超出flex container 的 size
*收縮比例(
flex-shrink) / sum
-
flex items
收縮后的最終size
不能超過min-width
/min-height
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;}.item {width: 120px;height: 120px;background-color: aquamarine;/* flex-shrink: 0; */}.item1 {background-color: orange;}.item2 {background-color: red;flex-shrink: 5;}.item3 {background-color: blueviolet;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div><div class="item item1">box5</div><div class="item item2">box6</div><div class="item item3">box7</div></div></body>
</html>
5、flex-basis
flex-basis
用來設置flex items
在main axis
方向上的base size
auto
:默認值- 具體的寬度數值(100px)
- 決定
flex-basis
最終 base size 的因素,優先級從高到低:max-width
、max-height
、min-width
、min-height
flex-basis
width
、height
- 內容本身的
size
6、flex
屬性
-
flex
屬性是flex-grow
、flex-shrink
、flex-basis
的簡寫,flex
可以指定1
個、2
個或者3
個值。
-
單值語法,值必須是以下其中之一:
-
一個無單位數(
<number>
):它會被當作flex-grow
的值
-
一個有效寬度值(
width
):它會被當作flex-basis
的值 -
關鍵字:
none
、auto
或initial
-
-
雙值語法,第一個值必須為一個無單位數(
<number>
),并且會被當作flex-grow
的值- 第二個值必須是以下之一:
- 一個無單位數,它會被當作
flex-shrink
的值 - 一個有效寬度值(
width
):它會被當作flex-basis
的值
- 一個無單位數,它會被當作
- 第二個值必須是以下之一:
-
三值語法:
- 第一個值必須是一個無單位數(
<number>
),并且它會被當作flex-grow
的值 - 第二個值必須是一個無單位數(
<number>
),并且它會被當作flex-shrink
的值 - 第三個值必須是一個有效寬度值(
width
),并且它會被當作flex-basis
的值
- 第一個值必須是一個無單位數(
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;}.item {/* width: 120px; */height: 120px;background-color: aquamarine;}.item1 {background-color: orange;}.item2 {background-color: red;/* flex: auto; *//* flex: none; *//* flex: initial; *//* flex: 2; *//* flex: 0.5 200px; */flex: 0.6 1 10px;}.item3 {background-color: blueviolet;/* flex: 3; *//* flex: 15px; */}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div></div></body>
</html>
六、flex
布局中使用justify-content
后,最后一行的布局問題
1、問題
-
想要的結果:
-
使用
justify-content: space-between;
后實際結果:
2、解決方法
- 在最后追加
n
個span
元素。n 的值 = 列數 - 2
。 - 設置
span
元素的寬度 =flex item
的寬度
※ span
換成 i
元素也行。
<!DOCTYPE html>
<html><head><title>Document</title><style>.container {height: 500px;width: 500px;background-color: antiquewhite;margin: 0 auto;display: flex;flex-wrap: wrap;justify-content: space-between;}.item {width: 150px;height: 150px;background-color: aquamarine;}.item1 {background-color: orange;}.item2 {background-color: red;}.item3 {background-color: blueviolet;}.container span {width: 150px;}</style></head><body><div class="container"><div class="item item1">box1</div><div class="item item2">box2</div><div class="item item3">box3</div><div class="item item4">box4</div><div class="item item4">box5</div><div class="item item3">box6</div><div class="item item2">box7</div><div class="item item1">box8</div><!-- 列數- 2 = 追加的span個數 --><span></span></div></body>
</html>