
批改狀態(tài):合格
老師批語(yǔ):非常棒, 作業(yè)要求全部都有, 很認(rèn)真, 也一定能學(xué)會(huì)
display
屬性(共2種)序號(hào) | 屬性值 | 描述 | 備注 |
---|---|---|---|
1 | flex; | 創(chuàng)建 flex 塊級(jí)容器 | 內(nèi)部子元素自動(dòng)成為 flex 項(xiàng)目 |
2 | inline-flex; | 創(chuàng)建 flex 行內(nèi)容器 | 內(nèi)部子元素自動(dòng)成為 flex 項(xiàng)目 |
序號(hào) | 容器/項(xiàng)目 | 默認(rèn)行為 |
---|---|---|
1 | 容器主軸 | 水平方向 |
2 | 項(xiàng)目排列 | 沿主軸起始線排列(當(dāng)前起始線居左) |
3 | 項(xiàng)目類型 | 自動(dòng)轉(zhuǎn)換”行內(nèi)塊級(jí)”元素,不管之前是什么類型 |
4 | 容器主軸空間不足時(shí) | 項(xiàng)目自動(dòng)收縮尺寸以適應(yīng)空間變化,不會(huì)換行顯示 |
5 | 容器主軸存在未分配空間時(shí) | 項(xiàng)目保持自身大小不會(huì)放大并充滿空間 |
<head> <style> .dome{ width: 300px; height: 200px; display: flex; } </style></head><body> <div class="dome"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div></body>
flex-direction
屬性:序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | row 默認(rèn)值 | 主軸水平: 起始線居中,項(xiàng)目從左到右顯示 |
2 | row-reverse | 主軸水平:起始線居右, 項(xiàng)目從右向左顯示 |
3 | column | 主軸垂直: 起始線居上,項(xiàng)目從上向下顯示 |
4 | column-reverse | 主軸垂直: 起始線居下,項(xiàng)目從下向上顯示 |
<style> .dome{ width: 300px; height: 200px; display: flex; flex-direction: row-reverse; } .item { width: 100px; height: 50px; background-color:red; font-size: 3rem; } </style>
<style> .dome{ width: 300px; height: 200px; display: flex; flex-direction: column-reverse; } .item { width: 100px; height: 50px; background-color:red; font-size: 3rem; } </style>
flex-warp
屬性:序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | nowrap 默認(rèn)值 | 項(xiàng)目不換行: 單行容器 |
2 | wrap | 項(xiàng)目換行: 多行容器,第一行在上方 |
3 | wrap-reverse | 項(xiàng)目換行: 多行容器,第一行在下方 |
項(xiàng)目不換行:
<style>
.dome{
width: 300px;
height: 200px;
display: flex;
flex-wrap: nowrap;
}
.item {
width: 150px;
height: 50px;
background-color:red;
font-size: 3rem;
}
</style>
項(xiàng)目換行(上)
<style>
.dome{
width: 300px;
height: 200px;
display: flex;
flex-wrap: wrap;
}
.item {
width: 150px;
height: 50px;
background-color:red;
font-size: 3rem;
}
</style>
項(xiàng)目換行(下)
<style>
.dome{
width: 300px;
height: 200px;
display: flex;
flex-wrap: wrap-reverse;
}
.item {
width: 150px;
height: 50px;
background-color:red;
font-size: 3rem;
}
</style>
(1)flex-flow
是屬性flex-direction
和flex-wrap
的簡(jiǎn)寫(xiě)
(2)語(yǔ)法:flex-flow: flex-direction flex-warp
(3)row nowarp
為默認(rèn)值
<style> .dome{ width: 300px; height: 200px; display: flex; flex-flow: row-reverse wrap; } .item { width: 150px; height: 50px; background-color:red; font-size: 3rem; } </style>
justify-content
屬性序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | flex-start 默認(rèn) | 所有項(xiàng)目與主軸起始線對(duì)齊 |
2 | flex-end | 所有項(xiàng)目與主軸終止線對(duì)齊 |
3 | center | 所有項(xiàng)目與主軸中間線對(duì)齊: 居中對(duì)齊 |
4 | space-between | 兩端對(duì)齊: 剩余空間在頭尾項(xiàng)目之外的項(xiàng)目間平均分配 |
5 | space-around | 分散對(duì)齊: 剩余空間在每個(gè)項(xiàng)目二側(cè)平均分配 |
6 | space-evenly | 平均對(duì)齊: 剩余空間在每個(gè)項(xiàng)目之間平均分配 |
終止線對(duì)齊:
<style>
.dome{
width: 400px;
height: 200px;
display: flex;
justify-content: flex-end;
}
.item {
width: 100px;
height: 50px;
background-color:red;
font-size: 3rem;
}
</style>
居中對(duì)齊:
.dome{
width: 400px;
height: 200px;
display: flex;
justify-content: center;
}
兩端對(duì)齊:
.dome{
width: 400px;
height: 200px;
display: flex;
justify-content: space-between;
}
分散對(duì)齊:
.dome{
width: 400px;
height: 200px;
display: flex;
justify-content: space-around;
}
平均對(duì)齊:
.dome{
width: 400px;
height: 200px;
display: flex;
justify-content: space-evenly;
}
align-items
屬性:序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | flex-start 默認(rèn) | 與交叉軸起始線對(duì)齊 |
2 | flex-end | 與交叉軸終止線對(duì)齊 |
3 | center | 與交叉軸中間線對(duì)齊: 居中對(duì)齊 |
.dome{ width: 400px; height: 200px; display: flex; align-items: flex-end; }
居中對(duì)齊:
.dome{
width: 400px;
height: 200px;
display: flex;
align-items: center;
}
align-content
屬性:序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | stretch 默認(rèn) | 項(xiàng)目拉伸占據(jù)整個(gè)交叉軸 |
1 | flex-start | 所有項(xiàng)目與交叉軸起始線(頂部)對(duì)齊 |
2 | flex-end | 所有項(xiàng)目與交叉軸終止線對(duì)齊 |
3 | center | 所有項(xiàng)目與交叉軸中間線對(duì)齊: 居中對(duì)齊 |
4 | space-between | 兩端對(duì)齊: 剩余空間在頭尾項(xiàng)目之外的項(xiàng)目間平均分配 |
5 | space-around | 分散對(duì)齊: 剩余空間在每個(gè)項(xiàng)目二側(cè)平均分配 |
6 | space-evenly | 平均對(duì)齊: 剩余空間在每個(gè)項(xiàng)目之間平均分配 |
(1)該屬性僅適用于多行容器
(2)多行容器中, 交叉軸會(huì)有多個(gè)項(xiàng)目, 剩余空間在項(xiàng)目之間分配才有意義
<style> .dome{ width: 200px; height: 200px; display: flex; flex-wrap: wrap; align-content: flex-end; }
.dome{ width: 200px; height: 200px; display: flex; flex-wrap: wrap; align-content: space-around; }
align-self
屬性:序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | auto 默認(rèn)值 | 繼承 align-items 屬性值 |
2 | flex-start | 與交叉軸起始線對(duì)齊 |
3 | flex-end | 與交叉軸終止線對(duì)齊 |
4 | center | 與交叉軸中間線對(duì)齊: 居中對(duì)齊 |
5 | stretch | 在交叉軸方向上拉伸 |
6 | baseline | 與基線對(duì)齊(與內(nèi)容相關(guān)用得極少) |
該屬性可覆蓋容器的align-items
, 用以自定義某個(gè)項(xiàng)目的對(duì)齊方式
.item:first-of-type{ height: initial; align-self: stretch; } .item:nth-of-type(2){ align-self: flex-end; } .item:last-of-type{ align-self: center; }
flex-grow
屬性:(1)在容器主軸上存在剩余空間時(shí), flex-grow
才有意義
(2)該屬性的值,稱為放大因子
序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | 0 默認(rèn)值 | 不放大,保持初始值 |
2 | initial | 設(shè)置默認(rèn)值,與0 等效 |
3 | n | 放大因子: 正數(shù) |
<style> .dome{ width: 300px; height: 200px; display: flex; /* flex-wrap: wrap; */ } .item { width: 50px; height: 50px; background-color:red; font-size: 3rem; } .item:first-of-type{ flex-grow: 1; } .item:nth-of-type(2){ background-color: lawngreen; flex-grow: 2; } .item:last-of-type{ background-color: lightseagreen; flex-grow: 3; } </style>
300-(50)*3=150
150/(1+2+3)=25
則:第一個(gè)元素為:50=25=75px
第二個(gè)元素為:50+25*2=100px
第三個(gè)元素為:50+25*3=125px
flex-shrink
屬性序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | 1 默認(rèn)值 | 允許項(xiàng)目收縮 |
2 | initial | 設(shè)置初始默認(rèn)值,與 1 等效 |
3 | 0 | 禁止收縮,保持原始尺寸 |
4 | n | 收縮因子: 正數(shù) |
當(dāng)容器主軸 “空間不足” 且 “禁止換行” 時(shí), flex-shrink
才有意義
<style> .dome{ width: 300px; height: 200px; display: flex; flex-flow: row nowrap; } .item { width: 150px; height: 50px; background-color:red; font-size: 3rem; } .item:first-of-type{ flex-shrink: 1; } .item:nth-of-type(2){ background-color: lawngreen; flex-shrink: 2; } .item:last-of-type{ background-color: lightseagreen; flex-shrink: 3; } </style>
三個(gè)元素共:150*3=450px
算出應(yīng)該收縮的大小:450-300=150px
算出每一個(gè)收縮倍數(shù)所要收縮的大?。?50/(1+2+3)=25px
則:第一個(gè)元素的寬度應(yīng)該是:150-25*1=125px
第二個(gè)元素的寬度應(yīng)該是:150-25*2=100px
第三個(gè)元素的寬度應(yīng)該是:150-25*3=75px
flex-basis
屬性序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | auto | 默認(rèn)值: 項(xiàng)目原來(lái)的大小 |
2 | px | 像素 |
3 | % | 百分比 |
(1)在分配多余空間之前,項(xiàng)目占據(jù)的主軸空間
(2)瀏覽器根據(jù)這個(gè)屬性,計(jì)算主軸是否有多余空間
(3)該屬性會(huì)覆蓋項(xiàng)目原始大小(width/height)
(4)該屬性會(huì)被項(xiàng)目的min-width/min-height
值覆蓋
即:優(yōu)先級(jí): 項(xiàng)目大小 < flex-basis
< min-width/max-height
<style> .dome{ width: 300px; height: 200px; display: flex; flex-flow: row nowrap; } .item { width: 50px; height: 50px; background-color:red; font-size: 3rem; } .item:first-of-type{ flex-basis: 100px; } .item:nth-of-type(2){ background-color: lawngreen; flex-basis: 30%; } .item:last-of-type{ background-color: lightseagreen; } </style>
語(yǔ)法:flex: flex-grow flex-shrink flex-basis
序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | 第一個(gè)值: 整數(shù) (0/1) | flex-grow |
2 | 第二個(gè)值: 整數(shù)(0/1) | flex-shrink |
3 | 第三個(gè)值: 有效寬度 | flex-basis |
序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | 第一個(gè)值: 整數(shù) | flex-grow |
3 | 第二個(gè)值: 有效寬度 | flex-basis |
序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | 整數(shù) | flex-grow |
2 | 有效寬度 | flex-basis |
3 | 關(guān)鍵字 | initial / auto / none |
序號(hào) | 案例 | 描述 |
---|---|---|
1 | flex: 1 | flex: 1 1 auto |
2 | flex: 180px | flex: 1 1 180px |
3 | initial | flex: 0 1 auto |
4 | auto | flex: 1 1 auto |
5 | none | flex: 0 0 auto |
三值語(yǔ)法:
.item:first-of-type{ flex: 0 1 100px }
二值語(yǔ)法:
.item:first-of-type{ flex: 0 150px }
總結(jié):
(1)display:flex
創(chuàng)建flex 塊級(jí)容器而display:inline-flex
創(chuàng)建flex 行內(nèi)容器
(2)flex-flow
是屬性flex-direction
和flex-wrap
的簡(jiǎn)寫(xiě)。語(yǔ)法為: flex-flow: flex-direction flex-wrap
(3)justify-content
屬性僅當(dāng)容器中主軸方向上存在剩余空間時(shí), 該屬性才有意義
(4)align-items
屬性僅適用于單行容器,且當(dāng)容器中交叉軸方向上存在剩余空間時(shí), 該屬性才有意義
(5)align-content
屬性僅適用于多行容器,且剩余空間在項(xiàng)目之間分配才有意義
(6)align-self
屬性可以覆蓋容器的align-items
,用于自定義某個(gè)元素的對(duì)齊方式
(7)flex
屬性可以對(duì)擴(kuò)大,收縮和項(xiàng)目計(jì)算尺寸進(jìn)行簡(jiǎn)化。語(yǔ)法為:flex: flex-grow flex-shrink flex-basis
(8)當(dāng)flex
屬性只有兩個(gè)屬性值時(shí),語(yǔ)法為:flex: flex-grow flex-basis
(9)當(dāng)flex
屬性只有一個(gè)屬性值時(shí),若是0/1,則為flex-grow
;若為有效寬度是,則為flex-basis
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)