一、設(shè)置彈性元素的增長(zhǎng)因子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>設(shè)置彈性元素的增長(zhǎng)因子</title> <link rel="stylesheet" href="static/css/1.css"> </head> <body> <h3>(1): 所有彈性元素不增長(zhǎng),以原始寬度顯示,增長(zhǎng)因子為: 0(默認(rèn))</h3> <div class="container flex demo1"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <h3>(2): 將全部剩余空間分配給指定彈性元素,例如: 第三個(gè)</h3> <div class="container flex demo2"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(3): 全部剩余空間按增長(zhǎng)因子在不同彈性元素間分配</h3> <div class="container flex demo3"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(4): 增長(zhǎng)因子支持小數(shù), 因?yàn)槭前丛鲩L(zhǎng)比例分配</h3> <div class="container flex demo4"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(5): 每個(gè)彈性元素寬度不同時(shí), 同樣適用以上分配規(guī)律</h3> <div class="container flex demo5"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
@import "public.css"; .container{ width: 550px; } .item { width: 100px; } .demo1 > .item { flex-grow: 0; } .demo2 > .item:first-of-type { flex-grow: 1; } .demo2 > .item:nth-of-type(2) { flex-grow: 0; } .demo2 > .item:last-of-type { flex-grow: 0; } .demo3 > .item:first-of-type{ flex-grow: 2; } .demo3 > .item:nth-of-type(2) { flex-grow: 2; } .demo3 > .item:last-of-type { flex-grow: 6; } .demo4 > .item:first-of-type{ flex-grow: 0.2; } .demo4 > .item:nth-of-type(2) { flex-grow: 0.2; } .demo4 > .item:last-of-type { flex-grow: 0.6; } .demo5 > .item:first-of-type{ width: 100px; flex-grow: 1; } .demo5 > .item:nth-of-type(2) { width: 150px; flex-grow: 2; } .demo5 > .item:last-of-type { width: 180px; flex-grow: 3; } /* 100+150+180=430 550-430=120 120/6=20 100+20=120 150+20*2=190 180+20*3=210 */
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
二、設(shè)置彈性元素的縮減因子
<body> <h1>flex-shrink: 設(shè)置彈性元素的縮減因子</h1> <h3>(1): 所有彈性元素不縮減,以原始寬度顯示,縮減因子為: 0</h3> <div class="container flex demo1"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(2): 所有彈性元素自適應(yīng)容器寬度且不換行,縮減因子: 1 (默認(rèn))</h3> <div class="container flex demo2"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(3): 當(dāng)三個(gè)彈性元素的縮減因?yàn)樽硬幌嗟葧r(shí)</h3> <div class="container flex demo3"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(4): 縮減因子也可以是小數(shù),只要大于就可以</h3> <div class="container flex demo4"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(5): 當(dāng)每個(gè)彈性元素寬度不一樣時(shí), 完全是另一道風(fēng)景線</h3> <div class="container flex demo5"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> </body>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
@import "public.css"; .container{ width: 550px; } .item{ width: 250px; } .demo1 > .item{ flex-shrink: 0; } .demo2 > .item{ flex-shrink: 1; } .demo3 > .item:first-of-type{ flex-shrink: 1; } .demo3 > .item:nth-of-type(2){ flex-shrink: 2; } .demo3 > .item:last-of-type{ flex-shrink: 3; } /* 1+2+3=6 750-550=200 200/6=33.3. 1*33.33=33.33 2*33.33=66.66 3*33.33=99.99 250-33.33=216.67 250-66.66=183.34 250-99.9=150.01 3 */ .demo4 > .item:first-of-type{ flex-shrink: 0.2; } .demo4 > .item:nth-of-type(2){ flex-shrink: 0.2; } .demo4 > .item:last-of-type{ flex-shrink: 0.6; } /* 0.2+0.2+0.6=1 200*0.2=40 200*0.6=120 250-40=210 250-120=130 */ .demo5 > .item:first-of-type{ width: 100px; flex-shrink: 2; } .demo5 > .item:nth-of-type(2){ width: 250px; flex-shrink: 3 ; } .demo5 > .item:last-of-type{ width: 300px; flex-shrink: 5; } /* 2+3+5=10 100+250+300=650-550=100 100/((100*2)+(250*3)+(300*5))=0.0408 0.0816 0.1224 0.204 100*0.0816=8.16 250*0.1224=30.6 100*0.3108=61.2 100-8.16=91.84 250-30.6=219.4 300-61.2=238.8 */
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
三、設(shè)置彈性元素的基準(zhǔn)尺寸
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>設(shè)置彈性元素的基準(zhǔn)尺寸</title> <link rel="stylesheet" href="static/css/3.css"> </head> <body> <h1>flex-basis: 設(shè)置彈性元素的基準(zhǔn)尺寸</h1> <h3>(1): 在未設(shè)置彈性元素寬度時(shí), 以內(nèi)容寬度顯示</h3> <div class="container flex demo1"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(2): 存在自定義元素寬度時(shí),則以該寬度顯示</h3> <div class="container flex demo2"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(3): 自動(dòng)狀態(tài)下, 由瀏覽器根據(jù)預(yù)設(shè)值自行判定</h3> <div class="container flex demo3"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(4): 當(dāng)元素存在自定義寬度與基準(zhǔn)寬度時(shí), 以基準(zhǔn)寬度為準(zhǔn) </h3> <div class="container flex demo4"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(5): 元素基準(zhǔn)寬度支持百分比設(shè)置 </h3> <div class="container flex demo5"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
@import "public.css"; .container{ width: 550px; } .demo1 >.item{ flex-basis: content; } .demo2 >.item{ width: 100px; } .demo3 >.item{ flex-basis: auto; } .demo4 > .item{ width: 100px; flex-basis: 150px; } .demo5 > .item:first-of-type{ flex-basis: 20%; } .demo5 > .item:nth-of-type(2){ flex-basis: 30%; } .demo5 > .item:last-of-type{ flex-basis: 50%; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
四、簡(jiǎn)化彈性元素的基本設(shè)置
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>簡(jiǎn)化彈性元素的基本設(shè)置</title> <link rel="stylesheet" href="static/css/4.css"> </head> <body> <h1>簡(jiǎn)化彈性元素的基本設(shè)置</h1> <h3>(1): 根據(jù)寬度計(jì)算,允許縮減適應(yīng)容器</h3> <div class="container flex demo1"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(2): 根據(jù)寬度計(jì)算,元素完全彈性以適應(yīng)容器</h3> <div class="container flex demo2"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(3): 元素完全失去彈性, 以原始大小呈現(xiàn)</h3> <div class="container flex demo3"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(4): 一個(gè)數(shù)值表示增長(zhǎng)因子,其它值默認(rèn): flex: 1 1 auto</h3> <div class="container flex demo4"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(5): 第三個(gè)有具體數(shù)值時(shí), 以它為計(jì)算標(biāo)準(zhǔn)</h3> <div class="container flex demo5"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> <h3>(6): 單獨(dú)設(shè)置某一個(gè)元素彈性大小 </h3> <div class="container flex demo6"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
@import "public.css"; .container{ width: 550px; } .demo1 > .item{ width: 100px; height: 60px; flex: initial; /* flex: 0 1 auto*/ } .demo2 > .item{ width: 100px; height: 60px; flex: auto; /* flex: 1 1 auto*/ } .demo3 > .item{ width: 100px; height: 60px; flex: none; /* flex: 0 0 auto*/ } .demo4 > .item{ width: 100px; height: 60px; flex: 1; /* flex: 1 1 atto*/ } .demo5 > .item{ width: 100px; height: 60px; flex: 1 0 200px; } .demo6 > .item{ width: 100px; height: 60px; } .demo6 > .item:last-of-type{ flex: 1 0 0; /* flex: 1 1 50%;*/ /* flex: 1 0 50%;*/ }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
五、單獨(dú)設(shè)置元素在交叉軸上排列方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>單獨(dú)設(shè)置元素在交叉軸上排列方式</title> <link rel="stylesheet" href="static/css/5.css"> </head> <body> <h1>單獨(dú)設(shè)置元素在交叉軸上排列方式</h1> <div class="container flex"> <span class="item">item1</span> <span class="item">item2</span> <span class="item">item3</span> </div> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
@import url(public.css); .container{ width: 550px; height: 300px; flex-flow: column nowrap; align-items: flex-end; } /*.item:first-of-type{*/ /* align-self: flex-start;*/ /*}*/ /*.item:nth-of-type(2){*/ /* align-self: flex-end;*/ /*}*/ /*.item:nth-of-type(3){*/ /* align-self: center;*/ /*}*/ /*.item:nth-of-type(4){*/ /* align-self:auto;*/ /*}*/ /*.item:nth-of-type(2){*/ /* background-color: #9ad3d4;*/ /* width: auto;*/ /* align-self: stretch;*/ /*}*/ /*.item:last-of-type{*/ /* align-self: self-start;*/ /*}*/ .item{ width: 100px; height: 60%; } .item:first-of-type{ align-self: flex-start; } .item:last-of-type{ align-self: flex-end; } .item:nth-of-type(2){ background-color: #888888; width: auto; align-self: stretch; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
六、order學(xué)習(xí)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>order</title> <link rel="stylesheet" href="static/css/6.css"> </head> <body> <h2 >order演示</h2> <div class="main"> <div class=" order1">1</div> <div class=" order2">2</div> <div class=" order3">3</div> <div class=" order4">4</div> </div> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
.main{ width: 400px; height: 150px; border: 1px solid black; /*display: -webkit-flex;*/ display: flex; } .main >div{ width: 50px; height: 50px; border: 1px solid brown; } .order1{ background-color: yellow; } .order2{ background-color: lime; } .order3{ background-color: blue; } .order4{ background-color: aqua; } .order1{ order:4; } .order2{ order:1; } .order3{ order:3; } .order4{ order:2; } /*.order1{*/ /* order: initial;*/ /*}*/ /*.order2{*/ /* order: revert;*/ /*}*/ /*.order1{*/ /* order: inherit;*/ /* }*/ /*.order2{*/ /* order: unset;*/ /*}*/
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
*{ margin: 0; padding: 0; } .body{ height: 100vh; display: flex; flex-flow: column nowrap; } .header, .footer{ min-height: 100px; background-color: lightseagreen; flex: auto; text-align: center; font-size: 1.5rem; } .main{ height: 70vh; background-color: lime; flex: 1; text-align: center; font-size: 1.2rem; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
七、移動(dòng)端首頁(yè)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>移動(dòng)端首頁(yè)</title> <link rel="stylesheet" href="static/css/8.css"> </head> <body> <header class="header">頭部</header> <main class="main">主體</main> <footer class="footer">底部</footer> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
八、彈性盒子仿寫圣杯布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>彈性盒子仿寫圣杯布局</title> <link rel="stylesheet" href="static/css/9.css"> </head> <body> <header>頭部</header> <main> <article> 內(nèi)容區(qū) </article> <aside class="left">左側(cè)</aside> <aside class="right">右側(cè)</aside> </main> <footer>底部</footer> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
*{ margin: 0; padding: 0; } html,body{ height: 900px; border: 1px solid black; } body{ display: flex; flex-direction: column; } header{ flex: 0 0 50px; border: 1px solid black; background-color: #9ad3d4; } footer{ flex: 0 0 60px; background-color: #444444; } main{ display: flex; background-color: blue; flex: 1; text-align: center; font-size: 2rem; } article{ flex-grow: 1; order: 2; background-color: lime; width: 70%; } .right{ order: 3; background-color: yellow; width: 15%; } .left{ width: 15%; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
九手寫flex屬性
十、程序抄寫
十一、總結(jié)
學(xué)習(xí)了flex的布局,但是有個(gè)問(wèn)題請(qǐng)答復(fù)一下
.demo5 > .item:first-of-type{
width: 100px;
flex-shrink: 2;
}
.demo5 > .item:nth-of-type(2){
width: 250px;
flex-shrink: 3 ;
}
.demo5 > .item:last-of-type{
width: 300px;
flex-shrink: 5;
}
/*
2+3+5=10
100+250+300=650-550=100
100/((100*2)+(250*3)+(300*5))=0.0408
0.0816 0.1224 0.204
100*0.0816=8.16
250*0.1224=30.6
300*0.204=61.2
100-8.16=91.84
250-30.6=219.4
300-61.2=238.8
*/
算法有無(wú)問(wèn)題,我允許后發(fā)現(xiàn)對(duì)不上
允許后的數(shù)字為
94.28
219.27
236.48
請(qǐng)告知我那點(diǎn)計(jì)算有誤
微信掃碼
關(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)