一个后端开发者的前端学习笔记

Flex 布局

2018-07-15  本文已影响13人  Ethan_zyc

Flex 布局

本文参考了阮一峰老师的Flex 布局教程:语法篇Flex 布局教程:实例篇,感谢

注意点:

  1. 行内元素也可以使用 Flex 布局。

    .box{
      display: inline-flex;
    }
    
  2. Webkit 内核的浏览器,必须加上-webkit前缀。

    .box{
      display: -webkit-flex; /* Safari */
      display: flex;
    }
    
  3. 设为 Flex 布局以后,子元素的floatclearvertical-align属性将失效。


概念

Flex 布局的英文全称是 Flexible Box,中文一般叫弹性盒子模型。

采用了 Flex 的元素称为 Flex 容器Flex Container,该元素下面的所有子元素自动成为该容器成员Flex Item,也可以称为容器项目。

容器默认存在两根轴:水平的主轴main axis和垂直的交叉轴cross axis


容器的属性


先说下本文采用的示例代码,后面的示例会基于这个修改,我会贴出关键部分,box 数量变化之类的就不贴出来了。

<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>Flex 布局</title>
    <style>
        .total {
            display: flex;
            flex-direction: row;
        }
        .box {
            font-size: 20px;
            line-height: 100px;
            text-align: center;
            height: 100px;
            width: 100px;
        }
        .box1 {
            background: #333333;
        }
        .box2 {
            background: #555555;
        }
        .box3 {
            background: #777777;
        }
        .box4 {
            background: #999999;
        }
    </style>
</head>
<body>
    <div class="total">
        <div class="box1 box">1</div>
        <div class="box2 box">2</div>
        <div class="box3 box">3</div>
        <div class="box4 box">4</div>
    </div>
</body>
</html>

flex-direction
row
.total {
    display: flex;
    flex-direction: row;
}
row.png
row-reverse
.total {
    display: flex;
    flex-direction: row-reverse;
}
row-reverse.png
column
.total {
    display: flex;
    flex-direction: column;
}
column.png
column-reverse
.total {
    display: flex;
    flex-direction: column-reverse;
}
column-reverse.png
flex-wrap
nowrap
.total {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
}
nowarp.png
wrap
flex-wrap: wrap;
wrap.png
wrap-severse
flex-wrap: wrap-reverse;
wrap-reverse.png
flex-flow

flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。可以将上面两者随意搭配,写起来方便一些。

.total {
    display: flex;
    flex-flow: row wrap;
}

justify-content

justify-content属性定义了子元素在主轴上的对齐方式。为了显示效果更好一点,这边我把最外层的背景色换了一下。

flex-start
.total {
    display: flex;
    flex-flow: row wrap;
    justify-content: center;
    height: 500px;
    background: #cccccc
}
flex-start.png
flex-end
justify-content: flex-end;
flex-end.png
center
justify-content: center;
center.png
space-between
justify-content: space-between;
space-between.png
space-around
justify-content: space-around;
space-around.png
align-items
flex-start / baseline / stretch (仅在我这个例子中相同,但注意自己的使用场景)
.total {
    display: flex;
    flex-flow: row wrap;
    justify-content: flex-start;
    align-items: flex-start;
    height: 500px;
    background: #cccccc
}
ali-flex-start.png
flex-end
align-items: flex-end;
ali-flex-end.png
center
align-items: center;
ali-center.png
align-content

align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

flex-start
.total {
    display: flex;
    flex-flow: row wrap;
    justify-content: flex-start;
    align-content: flex-start;
    height: 500px;
    background: #cccccc
}
ali-con-flex-start.png
flex-end
align-content: flex-end;
ali-con-flex-end.png
center
align-content: center
ali-con-center.png
space-between
align-content: space-between
ali-con-space-between.png
space-around
align-content: space-around
ali-con-space-around.png
stretch
align-content: space-around
ali-con-stretch.png

项目的属性


order

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

.box2 {
    background: #555555;
    order: 1;
}
order.png
flex-grow

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。前提是有剩余空间。

.box2 {
    background: #555555;
    flex-grow: 0.5;  // 放大为原来的两倍
}
flex-grow.png
flex-shrink

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。和上面类似,就不举例子了。前提是空间不足。

flex-basis

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

.box2 {

    background: #555555;

    flex-basis: 200px;

}
flex-basis.png
flex

flex属性是flex-grow, flex-shrinkflex-basis的简写,默认值为0 1 auto。后两个属性可选。

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

align-self

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch

// 父元素
.total {
    display: flex;
    flex-flow: row wrap;
    align-items: flex-start;
    height: 300px;
    background: #cccccc
}
// 子元素
.box2 {
    background: #555555;
    align-self: flex-end;
}
align-self.png

花了半天时间把 Flex 布局梳理了一遍,记忆也更深刻了,这篇 blog 其实感觉就是把阮一峰老师的那篇 copy 了一下,然后自己动手试一下,当然实战中布局不可能这么耿直,更多的还得参考阮一峰老师的实战篇。以后多练习吧。

上一篇下一篇

猜你喜欢

热点阅读