弹性盒子详细版(效果加演示代码)

1、如何开启弹性盒子(自适应布局flex)

 开启弹性盒子后子盒子会强制一盒,子盒子不设置宽会自适应设置宽会

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 父盒子 -->
    <h4>父盒子</h4>
    <div class="div">
        <div class="a">子盒子1</div>
        <div class="a">子盒子2</div>
        <div class="a">子盒子3</div>
        <div class="a">子盒子4</div>
    </div>
</body>
</html>
<style>
    .div{
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        /* 开启弹性盒子 */
        display:flex;
    }
    .a{
        height: 50px;
        border: 1px solid red;
        margin-left: 10px;
    }
</style>

效果:

 



 2、flex布局(父盒子)属性加效果

(1)flex-direction:设置主轴的方向 (写一个属性就可以了)

属性:

        row 默认从左到右(常用)

        row-reverse 从右到左

        column 从上到下(常用)

        column-reverse 从下到上

<style>
    .div{
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        /* 开启弹性盒子 */
        display: flex;
        /* flex-direction:设置主轴的方向 */
        /* 默认从左到右(常用)*/
        flex-direction:row; 
        /* 从右到左 */
        flex-direction:column;
        /* 从上到下(常用)*/
        flex-direction:column-reverse;
        /* 从下到上 */
        flex-direction:row-reverse;
    }
    .a{
        height: 50px;
        width: 50px;
        border: 1px solid red;
        margin-left: 10px;
    }
</style>

 效果:

(1)row属性,效果

       

(2)row-reverse属性,效果

 (3)column属性,效果

(4)column-revers属性,效果

(2)justify-content:设置主轴上的子元素排列方式(写一个属性就可以)

    属性:

            flex-start                    默认值从头开始如果主轴X轴,则从左到右(常用)
            flex-end                     从尾部开始排序
            center                        在主轴居中对齐(如果主轴是X轴则水平居中)(常用)
            space-around           平分剩余空间(常用)
            space-between         现两边贴边在平分剩余空间(重要)(常用)

<style>
    .div{
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        /* 开启弹性盒子 */
        /* display:flex; */
        display: flex;
        /* flex-direction:设置主轴的方向,row默认值写不写都可以不会造成影响 */
        flex-direction:row ;
        /* justify-content:设置主轴上的子元素排列方式,flex-start默认值 */
        /* 默认值从头开始如果主轴X轴,则从左到右(常用)*/
        justify-content: flex-start;
        /* 从尾部开始排序 */
        justify-content: flex-end;
        /* 在主轴居中对齐(如果主轴是X轴则水平居中)(常用) */
        justify-content: center;
        /* 平分剩余空间(常用) */
        justify-content: space-around;
        /* 现两边贴边在平分剩余空间(重要)(常用) */
        justify-content: space-between;         
    }
    .a{
        height: 50px;
        width: 50px;
        border: 1px solid red;
    }
</style>

效果:

(1)flex-start 属性 效果

(2) flex-end 属性 效果

(3)center 属性 效果

(4) space-around  属性 效果

(5)space-between 属性 效果


(3)flex-wrap:设置子元素是否换行;

属性:

                nowrap 不换行

                wrap 换行

<style>
    .div{
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        /* 开启弹性盒子 */
        /* display:flex; */
        display: flex;
        /* flex-direction:设置主轴的方向,row默认值写不写都可以不会造成影响 */
        flex-direction:row ;
        /* justify-content:设置主轴上的子元素排列方式,flex-start默认值 */
        justify-content: flex-start;
        /* flex-wrap:设置子元素是否换行;nowrap 默认值 */
        /* 不换行(默认) */
        flex-wrap:nowrap;
        /* 换行 */
        flex-wrap:wrap;
    }
    .a{
        height: 50px;
        width: 100px;
        border: 1px solid red;
    }
</style>

效果:

(1)nowrap 属性 效果

(2)wrap 属性 效果

(4)flex-flow:复合属性;属性:

  flex-flow:row wrap;  相当于同时设置了 flex-direction 和 flex-wrap 这个我就不演示了可以参考flex-wrap:和这个的效果一样



 3、flex布局(子盒子)属性加效果

 弹性盒子子项常见属性:

(1)flex子项目占的份数

                flex: 2;             flex 属性定义子项目分配剩余空间,用flex来表示占多少份数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 父盒子 -->
    <h4>父盒子</h4>
    <div class="div">
        <div class="a">子盒子1</div>
        <div class="a">子盒子2</div>
        <div class="a">子盒子3</div>
        <div class="a">子盒子4</div>
    </div>
</body>
</html>
<style>
    .div{
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        /* 开启弹性盒子 */
        /* display:flex; */
        display: flex;
        /* flex-direction:设置主轴的方向,row默认值写不写都可以不会造成影响 */
        flex-direction:row ;
        /* justify-content:设置主轴上的子元素排列方式,flex-start默认值 */
        justify-content: flex-start;
        /* flex-wrap:设置子元素是否换行;nowrap 默认值 */
        flex-wrap:nowrap ;
    }
    .a{
        height: 50px;
        width: 50px;
        border: 1px solid red;
    }
    /*选择器*/
    .a:nth-child(2){
        /*选中子盒子2,flex给它两份大小*/
        flex: 2;
    }
</style>

效果:

        


(2)order属性定义子项的排列顺序(前后顺序)

order: -1; 数值越小,排列越靠前,默认为0。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 父盒子 -->
    <h4>父盒子</h4>
    <div class="div">
        <div class="a">子盒子1</div>
        <div class="a">子盒子2</div>
        <div class="a">子盒子3</div>
        <div class="a">子盒子4</div>
        <div class="a">子盒子5</div>
    </div>
</body>
</html>
<style>
    .div{
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        /* 开启弹性盒子 */
        /* display:flex; */
        display: flex;
        /* flex-direction:设置主轴的方向,row默认值写不写都可以不会造成影响 */
        flex-direction:row ;
        /* justify-content:设置主轴上的子元素排列方式,flex-start默认值 */
        justify-content: flex-start;
        /* flex-wrap:设置子元素是否换行;nowrap 默认值 */
        flex-wrap:nowrap ;

    }
    .a{
        height: 50px;
        width: 50px;
        border: 1px solid red;
    }
    .a:nth-child(2){
        order: 0;
        /*
        order :2;
        oreder:-2;
        */
    }
</style>

效果:

order=0时效果

 order=2时效果

 order=-2时效果

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小 汐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值