flex布局应用

作用:改变子元素在父元素中的空间排列方式

重要属性:flex-direction、flex-wrap、justify-content、align-items


1.flex-direction

flex-direction属性决定主轴的方向(即项目的排列方向)。

四个值:

  • row(默认值):主轴为水平方向,起点在左端。
  • row-reverse:主轴为水平方向,起点在右端。
  • column:主轴为垂直方向,起点在上沿。
  • column-reverse:主轴为垂直方向,起点在下沿。
<!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>
    <style>
        .box{
            display: flex;
            flex-direction: row;
        }
        .box1{
            height: 200px;
            width: 200px;
            background-color: pink; 
        }
        .box2{
            height: 300px;
            width: 400px;
            background-color: skyblue;  
        }
        .box3{
            height: 350px;
            width: 200px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
    </div>
</body>
</html>

row(默认值)结果

row-reverse结果

 column结果

 column-reverse结果

 


2.flex-wrap

默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

它可能取三个值。

(1)nowrap(默认):不换行。

(2)wrap:换行,第一行在上方。

(3)wrap-reverse:换行,第一行在下方。

 

<style>
        .box{
            display: flex;
            flex-wrap: nowrap;
        }
        .box1{
            height: 200px;
            width: 200px;
            background-color: pink; 
        }
        .box2{
            height: 300px;
            width: 400px;
            background-color: skyblue;  
        }
        .box3{
            height: 350px;
            width: 200px;
            background-color: gold;
        }
    </style>

nowrap(默认值)结果

 

 <style>
        .box{
            display: flex;
            flex-wrap:wrap;
            width: 600px;
        }
        .box1{
            height: 200px;
            width: 200px;
            background-color: pink; 
        }
        .box2{
            height: 300px;
            width: 400px;
            background-color: skyblue;  
        }
        .box3{
            height: 350px;
            width: 200px;
            background-color: gold;
        }
    </style>

wrap结果

 注意:只有当第一行放不下,才会换行

wrap-reverse结果


 

 3.justify-content

 justify-content属性定义了项目在主轴上的对齐方式。

它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

  • flex-start(默认值):左对齐
  • flex-end:右对齐
  • center: 居中
  • space-between:两端对齐,项目之间的间隔都相等。
  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

 

 <style>
        .box{
            display: flex;
            justify-content: flex-start;
          
        }
        .box1{
            height: 200px;
            width: 200px;
            background-color: pink; 
        }
        .box2{
            height: 300px;
            width: 400px;
            background-color: skyblue;  
        }
        .box3{
            height: 350px;
            width: 200px;
            background-color: gold;
        }

flex-start(默认值)结果

 

flex-end结果

 

 center结果

 space-between结果

 space-around结果


 4.align-items

它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。

  • flex-start:交叉轴的起点对齐。
  • flex-end:交叉轴的终点对齐。
  • center:交叉轴的中点对齐。
  • baseline: 项目的第一行文字的基线对齐。
  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

 flex-start结果

 flex-end结果

 center结果

 baseline结果

 


5. flex-flow

flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。

.box {
  flex-flow: <flex-direction> <flex-wrap>;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值