彈性盒模型


設(shè)置在父元素上的屬性


一应闯、給父元素設(shè)置display:flex; display:inline-flex;

1纤控、所有子元素橫向排列;
2碉纺、即便子元素是塊元素船万,寬度也由內(nèi)容撐起;
3骨田、彈性盒模型的父元素不會出現(xiàn)高度塌陷耿导;
4、一旦形成彈性盒模型态贤,在子元素沒有設(shè)置高度時舱呻,默認和父元素高度一致;
5悠汽、子元素永遠不會溢出箱吕,即便寫了寬度,也會被縮減柿冲,被壓扁殖氏;
6、子元素為行內(nèi)元素時姻采,自動變?yōu)閴K元素的表現(xiàn)形式雅采,可以設(shè)置寬高。


image.png

二慨亲、flex-direction:row | row-reverse | column | column-reverse

image.png

三婚瓜、flex-wrap: nowrap | wrap | wrap-reverse

image.png

四、【主軸對齊】justify-content: flex-start | flex-end | center | space-between | sapce-around

區(qū)別:
flex-direction,改變了軸的方向刑棵,所以排列順序發(fā)生了改變巴刻。
justify-content 只是對其方式的改變。


image.png

五蛉签、【側(cè)軸對齊】align-item:flex-start | flex-end | center | baseline | stretch(默認值)

flex-start:以頂對齊胡陪,沒有高度的由內(nèi)容撐起沥寥。
center:以元素高度中間對齊。
stretch:以頂對齊柠座,沒有高度的默認等于父元素的高度邑雅。
baseline :以文字基線對齊。


image.png

六妈经、【設(shè)置各個行的對齊】align-content:flex-start | flex-end | center | spacing-around | spacing-between

align-content和justify-content有點像淮野,區(qū)別是justify-content用于main-axis的對齊,而align-content用于多行在容器內(nèi)的對齊方式吹泡。因此一定要多行(必須flex-wrap: wrap且容器不足以將所有元素放入一行內(nèi))才能出效果骤星,如果容器內(nèi)就一行是沒有效果的。

image.png


設(shè)置在子元素上的屬性


一爆哑、flex-grow:數(shù)值洞难;將剩余空間進行分配。默認值為0揭朝,即不進行分配廊营。

<style>
 .box{
        margin:50px auto;
        width: 300px;
        height: 100px;
        background: #aaa;
        display: flex;
        }
 .box p:nth-child(1){
        background: #f00;
        width: 100px;
        flex-grow: 1;
        }
 .box p:nth-child(2){
        background: #ff0;
        width: 50px;
        flex-grow: 2;
       }
</style>

<body>
    <div class="box">
        <p>1</p>
        <p>2</p>
    </div>
</body>

父元素寬度300px,兩個子元素共計150px萝勤,剩余150px露筒,
第一個p標簽flex-grow: 1; 分得150/(1+2)*1=50px 最后寬度100+50=150px;
第二個p標簽flex-grow: 2; 分得150/(1+2)*2=100px 最后寬度50+100=150px敌卓;

二慎式、flex-shrink:數(shù)值;將溢出空間進行分配趟径。默認值為1瘪吏。

<style>
 .box{
        margin:50px auto;
        width: 200px;
        height: 100px;
        background: #aaa;
        display: flex;
        }
 .box p:nth-child(1){
        background: #f00;
        width: 300px;
        flex-shrink: 1;
        }
 .box p:nth-child(2){
        background: #ff0;
        width: 100px;
        flex-shrink: 2;
       }
</style>

<body>
    <div class="box">
        <p>1</p>
        <p>2</p>
    </div>
</body>

父元素寬度200px,兩個子元素共計400px蜗巧,溢出200px掌眠,
第一個p標簽flex-shrink: 1; 裁去300*1/(300*1+1002)200
第二個p標簽flex-shrink: 2; 裁去100*2/(300*1+1002)200

三、flex-basis

效果相當于width

四幕屹、【重點】 flex:1

flex:1;
flex:1 1 0;(依次為flex-grow, flex-shrink, flex-basis)
含義:當有多余空間時全部分配給此元素蓝丙,當有溢出時,從這個元素上縮減望拖,寬度為0(之前如果寫了寬度則失效)

圣杯布局
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{margin:0;padding:0;}
        html,body{
            height: 100%;
        }
        body{
            display:flex;
            flex-direction: column;
        }
        header{
            height: 100px;
            background: #f0f;
        }
        footer{
            height: 100px;
            background: #0ff;
        }
        main{
            background: #0f0;
            flex:1;
            display: flex;
        }
        .main_left{
            background: #ff0;
            width: 100px;
        }
        .main_center{
            background: #f00;
            flex:1;
        }
        .main_right{
            background: #0f0;
            width: 100px;
        }
    </style>
</head>
<body>
    <header></header>
    <main>
        <div class="main_left"></div>
        <div class="main_center"></div>
        <div class="main_right"></div>
    </main>
    <footer></footer>
</body>
</html>

五渺尘、order:數(shù)值;值越大,越靠后

image.png

六说敏、align-self: auto | flex-start | flex-end | center | baseline | stretch

設(shè)置元素自身在側(cè)軸上的對齊方式鸥跟。

自轉(zhuǎn)骰子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{margin:0;padding:0;}
        .stage{
            margin:50px auto;
            width: 800px;
            height: 300px;
            background: #ccc;
            display: flex;
            position: relative;
            perspective: 1000px;
        }
        .con{
            width: 65px;
            height: 65px;
            position: absolute;
            left:0;
            top:0;
            bottom:0;
            right:0;
            margin:auto;
            transform-style: preserve-3d;  
            animation: rr 2s infinite linear;
        }
        .box{
            padding:5px;
            box-sizing: border-box;
            width: 70px;
            height: 70px;
            background: #e6e6e6;
            border-left:4px solid #d7d7d7;
            border-bottom:4px solid #bbb;
            border-radius:5px;
            display:flex;
            position: absolute;
            left:0;
            top:0;
            bottom:0;
            right:0;
            margin:auto;
        }
        span{
            display:block;
            width: 18px;
            height: 18px;
            border-radius: 50%;
            background: #333;
        }
        .box:nth-child(1){justify-content: center; align-items:center;}
        .box{justify-content: space-between;}

        .box:nth-child(2) span:nth-child(1){align-items: flex-start;}
        .box:nth-child(2) span:nth-child(2){align-self: flex-end;}

        .box:nth-child(3) span:nth-child(1){align-items: flex-start;}
        .box:nth-child(3) span:nth-child(2){align-self: center;}
        .box:nth-child(3) span:nth-child(3){align-self: flex-end;}

        .box:nth-child(4) p{display: flex; flex-direction: column; justify-content: space-between}

        .box:nth-child(5) p{display: flex; flex-direction: column; justify-content: space-between}
        .box:nth-child(5) p:nth-child(2){justify-content: center;}

        .box:nth-child(6) p{display: flex; flex-direction: column; justify-content: space-between}


       .box:nth-child(1){transform: translateZ(35px)}
        .box:nth-child(2){transform: translateZ(-35px)}
        .box:nth-child(3){transform: rotateY(90deg) translateZ(35px)}
        .box:nth-child(4){transform: rotateY(-90deg) translateZ(35px)}
        .box:nth-child(5){transform: rotateX(90deg) translateZ(35px)}
        .box:nth-child(6){transform: rotateX(-90deg) translateZ(35px)} 

        @keyframes rr {
            from{transform:rotateX(0) rotateY(0)}
            to{transform:rotateX(360deg) rotateY(360deg)}
        }
    </style>
</head>
<body>
    <div class="stage">
        <div class="con">
        
            <div class="box">
                <span></span>
            </div>
            <div class="box">
                <span></span>
                <span></span>
            </div>
            <div class="box">
                <span></span>
                <span></span>
                <span></span>
            </div>
            <div class="box">
                <p>
                    <span></span>
                    <span></span>
                </p>
                <p>
                    <span></span>
                    <span></span>
                </p>
            </div>
            <div class="box">
                <p>
                    <span></span>
                    <span></span>
                </p>
                <p><span></span></p>
                <p>
                    <span></span>
                    <span></span>
                </p>
            </div>
            <div class="box">
                <p>
                    <span></span>
                    <span></span>
                    <span></span>
                </p>
                <p>
                    <span></span>
                    <span></span>
                    <span></span>
                </p>
            </div>
            
        </div>
    </div>
</body>
</html>

七、注意事項

彈性盒布局盡量不要使元素脫離文檔流,否則整個布局就會混亂医咨。盡量不適用以下屬性:
float:left | right;
clear:both;
position:absolute;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枫匾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子拟淮,更是在濱河造成了極大的恐慌干茉,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惩歉,死亡現(xiàn)場離奇詭異,居然都是意外死亡俏蛮,警方通過查閱死者的電腦和手機撑蚌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搏屑,“玉大人争涌,你說我怎么就攤上這事±绷担” “怎么了亮垫?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長伟骨。 經(jīng)常有香客問我饮潦,道長,這世上最難降的妖魔是什么携狭? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任继蜡,我火速辦了婚禮,結(jié)果婚禮上逛腿,老公的妹妹穿的比我還像新娘稀并。我一直安慰自己,他們只是感情好单默,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布碘举。 她就那樣靜靜地躺著,像睡著了一般搁廓。 火紅的嫁衣襯著肌膚如雪引颈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天境蜕,我揣著相機與錄音线欲,去河邊找鬼。 笑死汽摹,一個胖子當著我的面吹牛李丰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逼泣,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼趴泌,長吁一口氣:“原來是場噩夢啊……” “哼舟舒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起嗜憔,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤秃励,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后吉捶,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體夺鲜,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年呐舔,在試婚紗的時候發(fā)現(xiàn)自己被綠了币励。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡珊拼,死狀恐怖食呻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情澎现,我是刑警寧澤仅胞,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站剑辫,受9級特大地震影響干旧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜妹蔽,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一莱革、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧讹开,春花似錦盅视、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至成艘,卻和暖如春赏半,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背淆两。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工断箫, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人秋冰。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓仲义,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子埃撵,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容