css的弹性盒子flex的属性

Flex是“Fexible Box”意为“弹性盒子”是一种布局方式

display:flex;属性是放在父容器身上,下面的子容器会自动变成flex的项目

  #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
    }

.zi{
    width: 200px;
    height: 100px;
    border:1px dashed gray;
}
<body>

<div id="fu">

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

<div class="zi">2</div>

<div class="zi">3</div>

<div class="zi">4</div>

<div class="zi">5</div>

<div class="zi">6</div>


</div>



    
</body>

正常排列,不加display:flex;属性

 

#fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
    }

.zi{
    width: 200px;
    height: 100px;
    border:1px dashed gray;
}

在父容器加display:flex;属性后

盒子会横向排成一列,而且盒子多的话,宽度会发生变化

 

采用flex布局的容器,存在两个轴,主轴和侧轴,默认的主轴就是x轴

改变主轴的属性是flex-direction:;

默认值是 row 就是横向排列

 column    纵向排列

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
        flex-direction: column;
    }

 

 row-reverse  反横向排列

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
        flex-direction: row-reverse;
    }

 column-reverse 反纵向排列

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
        flex-direction: column-reverse;
    }

 

记住  哪个方向排列,哪个方向就是主轴

调整主轴对齐方式 justify-content:;,以x轴为例

 flex-start   默认值  左对齐

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
          justify-content: flex-start;
      
    }
.zi{
    width: 200px;
    height: 100px;
    border:1px dashed gray;
}

<body>

<div id="fu">

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

<div class="zi">2</div>

<div class="zi">3</div>



</div>



    
</body>

 

flex-end        右对齐

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
      justify-content:flex-end;
    }

 

center       水平居中

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
        justify-content:center;
      
    }

 

space-between 两端对齐方式

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
        justify-content:space-between;
      
    }

 

space-around  距离环绕

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
        justify-content:space-around;
      
    }

 

调整侧轴的对齐方式

align-items 调整侧轴的对齐方式

  flex-start   默认值  上对齐

  #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
       align-items: flex-start;
      
    }

.zi{
    width: 200px;
    height: 100px;
    border:1px dashed gray;
}
<body>

<div id="fu">

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

<div class="zi">2</div>

<div class="zi">3</div>



</div>



    
</body>

 

flex-end        下对齐

   #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
       align-items: flex-end;
      
    }

center       垂直居中

 #fu{
        width: 800px;
        height: 800px;
        border:5px solid black;
        display: flex;
       align-items: center;
      
    }

注意:

行内元素在弹性盒子中会变成块元素

justify-center  横向排列方式

align-center      纵向排列方式

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值