flex弹性布局

主轴与交叉轴

在这里插入图片描述
默认按主轴对齐(flex-direction: row)

<style>
        .main{
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            font-size: 20px;
        }
    </style>
<div class="main">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>

结果展示
在这里插入图片描述

flex容器与子项

在这里插入图片描述

轴对齐

在这里插入图片描述

flex-direction: row-reverse;

   <style>
        .main{
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            flex-direction: row-reverse;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            font-size: 20px;
        }
    </style>
<div class="main">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>

结果展示
在这里插入图片描述

flex-direction: column;

在这里插入图片描述

flex-direction: column-reverse;

在这里插入图片描述

换行与缩写

在这里插入图片描述

flex-wrap: wrap;

默认在会在同一行展示,即便7个div的宽度超过父级div也扔在第一行显示,wrap进行换行

 <style>
        .main{
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            flex-wrap: wrap;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            font-size: 20px;
        }
    </style>
    <div class="main">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
</div>

结果展示
在这里插入图片描述

flex-wrap: wrap-reverse

在这里插入图片描述
尽量不要用弹性布局做多行多列

flex-flow: column wrap;

在这里插入图片描述
flex-flow将对齐方式与换行写在一起

内容对齐

在这里插入图片描述

justify-content: space-around;

    <style>
        .main{
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            justify-content: space-around;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            font-size: 20px;
        }
    </style>
<div class="main">
    <div>1</div>
    <div>2</div>
    <div>3</div>
</div>

在这里插入图片描述

justify-content: space-between

在这里插入图片描述

justify-content: space-evenly

在这里插入图片描述

交叉轴对齐

在这里插入图片描述

align-content: space-evenly

flex-wrap: wrap;
align-content: space-evenly;
要一起使用, 不折行不生效

    <style>
        .main{
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            flex-wrap: wrap;
            align-content: space-evenly;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            font-size: 20px;
        }
    </style>

在这里插入图片描述

align-items: center;

用来处理单行

在这里插入图片描述

内联与块的上下左右居中对齐

单行文字居中对齐

3种方式


    <style>
        /*.box{*/
            /*width: 300px;*/
            /*height: 200px;*/
            /*background: skyblue;*/
            /*display: flex;*/
            /*align-items: center;*/
        /*}*/

        /*.box{*/
            /*width: 300px;*/
            /*height: 200px;*/
            /*background: skyblue;*/
            /*display: flex;*/
            /*flex-wrap: wrap;*/
            /*align-content: center;*/
        /*}*/


        .box{
            width: 300px;
            height: 200px;
            background: skyblue;
            line-height: 200px;
        }

    </style>

<div class="box">
    测试文字
</div>

在这里插入图片描述
多行文字使用前两个弹性布局

块级居中

 <style>
        .box{
            width: 300px;
            height: 200px;
            background: skyblue;
            display: flex;
            justify-content: center;
            align-items: center;

        }

        .box div{
            width: 100px;
            height: 100px;
            background: pink;

        }

    </style>
   或者另一种方法
    <style>

        .box{
            width: 300px;
            height: 200px;
            background: skyblue;
            position: relative;
        }

        .box div{
            width: 100px;
            height: 100px;
            background: pink;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);

        }

    </style>
<div class="box">
  <div></div>
</div>

在这里插入图片描述

不定项居中

在这里插入图片描述

   <style>

       .box{
           width: 300px;
           height: 150px;
           background: skyblue;
           display: flex;
           justify-content: center;
           align-items: end;
       }

       .box div{
           width: 30px;
           height: 30px;
           background: pink;
           border-radius: 50%;
           margin: 10px;

       }

   </style>

均分列居中

在这里插入图片描述

<style>
    .main{
        height: 200px;
        background: skyblue;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        padding: 0 20px;
    }
    .main div{
        width: 30px;
        height: 30px;
        background: pink;
        border-radius: 50%;
    }
</style>


<div class="main">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

子项分组布局

在这里插入图片描述

  <style>
        .main {
            height: 200px;
            background: skyblue;
            display: flex;
            align-items: center;
        }


        .main div {
            width: 50px;
            height: 100px;
            background: pink;
            margin-right: 10px;
        }
        .main div:nth-of-type(1){
            margin-right: auto; /*从哪里开始分组*/
        }

    </style>

<div class="main">
    <div >1</div>
    <div >2</div>
    <div >3</div>

</div>

flex子项

在这里插入图片描述

flex-grow

flex-grow: 1;比例值为1占满剩余的所有空间

<style>
        .main {
            width: 500px;
            height: 300px;
            background: skyblue;
            display: flex;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            flex-grow: 0.5;
        }


    </style>
<div class="main">
    <div >1</div>
</div>

在这里插入图片描述
在这里插入图片描述

flex-shrink收缩比例

在这里插入图片描述

当子容器超出父容器默认收缩到父容器大小
在这里插入图片描述

 <style>
        .main {
            width: 500px;
            height: 300px;
            background: skyblue;
            display: flex;
        }

        .main div{
            width: 600px;
            height: 100px;
            background: pink;
        }



    </style>

<div class="main">
    <div >1</div>
</div>

在这里插入图片描述

    <style>
        .main {
            width: 500px;
            height: 300px;
            background: skyblue;
            display: flex;
        }
        .main div{
            width: 600px;
            height: 100px;
            background: pink;
            flex-shrink: 0.5;
        }


    </style>


<div class="main">
    <div >1</div>
</div>

flex-basis

在这里插入图片描述

代码

    <style>
        .main {
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            flex-direction: column;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
            flex-basis: 200px;
        }


    </style>
<div class="main">
    <div >1</div>
</div>

结果展示
在这里插入图片描述
flex-basis: auto;/可选值 0% auto 200px 100%/

  <style>
        .main {
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            align-items: flex-start;
        }
        .main div{
            background: pink;
            flex-basis: auto;/*可选值 0% auto 200px  100%*/
        }


    </style>
<div class="main">
    <div >测试文字</div>
</div>

结果展示

在这里插入图片描述

flex缩写

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

order

在这里插入图片描述

代码

 <style>
        .main {
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            align-items: flex-start;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
        }
        .main div:nth-of-type(1){
            order: 1;
        }
        .main div:nth-of-type(4){
            order: -1;
        }

    </style>
<div class="main">
    <div >1</div>
    <div >2</div>
    <div >3</div>
    <div >4</div>
</div>

结果展示
在这里插入图片描述

align-self

在这里插入图片描述

代码

<style>
        .main {
            width: 500px;
            height: 500px;
            background: skyblue;
            display: flex;
            align-items: flex-start;
        }
        .main div{
            width: 100px;
            height: 100px;
            background: pink;
        }

        .main div:nth-of-type(4){
            height: 50px;
            align-self: flex-end;
        }

    </style>
<div class="main">
    <div >1</div>
    <div >2</div>
    <div >3</div>
    <div >4</div>
</div>

结果展示
在这里插入图片描述

等高布局

在这里插入图片描述

   <style>
        .main {
            width: 500px;
            background: skyblue;
            display: flex;
            justify-content: space-between;
        }


        .main div{
            width: 100px;
            background: pink;
        }

    </style>

<div class="main">
    <div >
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
    </div>
    <div >
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
        <p>测试内容</p>
    </div>
</div>

二列或三列布局

在这里插入图片描述

二列布局

代码

 <style>
        body{
            margin: 0;
        }
        .main {
            height: 100vh;
            background: skyblue;
            display: flex;
        }


        .col1{
            width: 100px;
            background: pink;
        }
        .col2{
            flex-grow: 1;
            background: greenyellow;
        }
    </style>
    <div class="main">
    <div class="col1"></div>
    <div class="col2"></div>
</div>

在这里插入图片描述
三列
代码

    <style>
        body{
            margin: 0;
        }
        .main {
            height: 100vh;
            background: skyblue;
            display: flex;
        }


        .col1{
            width: 100px;
            background: pink;
        }
        .col2{
            flex-grow: 1;
            background: greenyellow;
        }
        .col3{
            width: 100px;
            background: darkseagreen;
        }
    </style>
<div class="main">
    <div class="col1"></div>
    <div class="col2"></div>
    <div class="col3"></div>
</div>

结果展示
在这里插入图片描述

sticky footer布局

在这里插入图片描述
代码

<style>
        body{
            margin: 0;
        }
        .main {
            min-height: 100vh;
            display: flex;
            background: skyblue;
            flex-direction: column;
        }
        .header{
            height: 100px;
            background: pink;
        }

        .content{
            background: greenyellow;
            flex-grow: 1;
        }
        .footer{
            height: 100px;
            background: orangered;
        }


    </style>


<div class="main">
    <div class="header"></div>
    <div class="content">
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>




    </div>
    <div class="footer"></div>
</div>

结果展示
在这里插入图片描述

溢出项布局

在这里插入图片描述

代码

<style>
    body{
        margin: 0;
    }
    .main{
        height: 100px;
        background: skyblue;
        display: flex;
        align-items: center;
    }
    .main div{
        width:100px;
        height: 80px;
        background: pink;
        margin-right: 10px;
        flex-shrink: 0;
    }
</style>
<div class="main">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
</div>

代码展示

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值