美文网首页css
CSS3 flex布局

CSS3 flex布局

作者: Lia代码猪崽 | 来源:发表于2018-10-10 17:38 被阅读4次
  • 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性。
  • 它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。
  • 2009年,W3C提出了一种新的方案—-Flex布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。

一、什么是Flex布局

Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。

任何一个容器都可以指定为Flex布局。

.box{
  display: flex;
}

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

.box{
  display: inline-flex;
}

Webkit内核的浏览器,必须加上-webkit前缀。

.box{
  display: -webkit-flex; /* Safari */
  display: flex;
}

注意,设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。

二、基本概念

1.容器、项目

采用Flex布局的元素,称为Flex容器(flex container),简称”容器”。它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称”项目”。

2.主轴、交叉轴

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

  • 主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;
  • 交叉轴的开始位置叫做cross start,结束位置叫做cross end。
  • 项目默认沿主轴排列。
  • 单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。
image.png

三、容器的属性

以下6个属性设置在容器上。

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

以下都沿用这一套代码:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            width: 48px;
            height: 48px;
            line-height: 48px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">1</div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <div class="children">6</div>
    <div class="children">7</div>
    <div class="children">8</div>
    <div class="children">9</div>
</div>
</body>
</html>
1.flex-direction属性

flex-direction属性决定主轴的方向(即项目的排列方向),它可能有4个值:

  • row(默认值):主轴为水平方向,起点在左端。
  • row-reverse:主轴为水平方向,起点在右端。
  • column:主轴为垂直方向,起点在上沿。
  • column-reverse:主轴为垂直方向,起点在下沿。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-direction属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-direction: row;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            width: 48px;
            height: 48px;
            line-height: 48px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">1</div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <div class="children">6</div>
    <div class="children">7</div>
    <div class="children">8</div>
    <div class="children">9</div>
</div>
</body>
</html>
属性值为row
属性值为row-reverse
属性值为column
属性值为column-reverse
2.flex-wrap属性

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

  • nowrap(默认):不换行。
  • wrap:换行,第一行在上方。
  • wrap-reverse:换行,第一行在下方。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-wrap属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-wrap: wrap ;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            width: 48px;
            height: 48px;
            line-height: 48px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">1</div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <div class="children">6</div>
    <div class="children">7</div>
    <div class="children">8</div>
    <div class="children">9</div>
    <div class="children">10</div>
    <div class="children">11</div>
    <div class="children">12</div>
    <div class="children">13</div>
    <div class="children">14</div>
</div>
</body>
属性值为row
属性值为wrap
属性值为wrap-reverse
3.flex-flow

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

4.justify-content

justify-content属性定义了项目在主轴上的对齐方式。
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右:

  • flex-start(默认值):左对齐
  • flex-end:右对齐
  • center: 居中
  • space-between:两端对齐,项目之间的间隔都相等。
  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的justify-content属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            justify-content: flex-start ;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 10px;
            width: 48px;
            height: 48px;
            line-height: 48px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">1</div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <!--<div class="children">6</div>-->
    <!--<div class="children">7</div>-->
    <!--<div class="children">8</div>-->
    <!--<div class="children">9</div>-->
    <!--<div class="children">10</div>-->
    <!--<div class="children">11</div>-->
    <!--<div class="children">12</div>-->
    <!--<div class="children">13</div>-->
    <!--<div class="children">14</div>-->
</div>
</body>
</html>
属性值为flex-start
属性值为flex-end
属性值为center
属性值为space-between
属性值为space-around
5.align-items

align-items属性定义项目在交叉轴上如何对齐。
它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下:

  • flex-start:交叉轴的起点对齐。
  • flex-end:交叉轴的终点对齐。
  • center:交叉轴的中点对齐。
  • baseline: 项目的第一行文字的基线对齐。
  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的align-items属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            align-items: flex-start ;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 10px;
            width: 48px;
            height: 48px;
            line-height: 48px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">1</div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <!--<div class="children">6</div>-->
    <!--<div class="children">7</div>-->
    <!--<div class="children">8</div>-->
    <!--<div class="children">9</div>-->
    <!--<div class="children">10</div>-->
    <!--<div class="children">11</div>-->
    <!--<div class="children">12</div>-->
    <!--<div class="children">13</div>-->
    <!--<div class="children">14</div>-->
</div>
</body>
</html>
属性值为flex-start
属性值为flex-end
属性值为center
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的align-items属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            align-items: baseline   ;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 10px;
            width: 48px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <!--<div class="children">6</div>-->
    <!--<div class="children">7</div>-->
    <!--<div class="children">8</div>-->
    <!--<div class="children">9</div>-->
    <!--<div class="children">10</div>-->
    <!--<div class="children">11</div>-->
    <!--<div class="children">12</div>-->
    <!--<div class="children">13</div>-->
    <!--<div class="children">14</div>-->
</div>
</body>
</html>
属性值为baseline
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-direction属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            align-items: stretch;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 10px;
            width: 48px;
            /*height: 88px;*/
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <!--<div class="children">6</div>-->
    <!--<div class="children">7</div>-->
    <!--<div class="children">8</div>-->
    <!--<div class="children">9</div>-->
    <!--<div class="children">10</div>-->
    <!--<div class="children">11</div>-->
    <!--<div class="children">12</div>-->
    <!--<div class="children">13</div>-->
    <!--<div class="children">14</div>-->
</div>
</body>
</html>
属性值为stretch
6.align-content

align-content属性定义了多根轴线的对齐方式,该属性可能取6个值。

注意:如果项目只有一根轴线,该属性不起作用。所以要搭配flex-direction: row;flex-wrap: wrap;

  • stretch(默认值):轴线占满整个交叉轴。
  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-direction属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-wrap: wrap;
            align-content: stretch;
            align-content: flex-start;
            width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 5px;
            width: 88px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children">2</div>
    <div class="children">3</div>
    <div class="children">4</div>
    <div class="children">5</div>
    <div class="children">6</div>
    <div class="children">7</div>
    <div class="children">8</div>
    <div class="children">9</div>
    <div class="children">10</div>
    <div class="children">11</div>
    <div class="children">12</div>
    <div class="children">13</div>
    <div class="children">14</div>
</div>
</body>
</html>
属性值为stretch
属性值为flex-start
属性值为flex-end
属性值为center
属性值为space-between
属性值为space-around

四、项目的属性

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self
1.order属性

默认情况下,Flex项目按源顺序排列。
但是,order属性控制它们在Flex容器中的显示顺序。值越小,越靠前。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的order属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
             width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 5px;
            width: 88px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
        .children1 {
            order: -1;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children children1">2</div>
    <div class="children">3</div>
    <div class="children children1">4</div>
    <div class="children">5</div>
    <div class="children children1">6</div>
    <div class="children">7</div>
    <div class="children children1">8</div>
    <div class="children">9</div>
    <div class="children children1">10</div>
    <div class="children">11</div>
    <div class="children children1">12</div>
    <div class="children">13</div>
    <div class="children children1">14</div>
</div>
</body>
</html>
加了children1类的模块顺序在前面了
2.flex-grow
  • flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
  • 如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。
  • 如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-grow属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
             width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 5px;
            width: 88px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
        .children1 {
            flex-grow: 1;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children children1">2</div>
    <div class="children">3</div>
    <div class="children children1">4</div>
    <div class="children">5</div>
    <div class="children children1">6</div>
    <div class="children">7</div>
    <div class="children children1">8</div>
    <div class="children">9</div>
    <div class="children children1">10</div>
    <div class="children">11</div>
    <div class="children children1">12</div>
    <div class="children">13</div>
    <div class="children children1">14</div>
</div>
</body>
</html>
只有有足够空间的最后一行加了children1的模块放大了一倍
3.flex-shrink
  • flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
  • 如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。
  • 如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
  • 负值对该属性无效。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-shrink属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-direction: row;
            /*flex-wrap: wrap;*/
             width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 5px;
            width: 88px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
        .children1 {
            flex-shrink: 0;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children children1">2</div>
    <div class="children">3</div>
    <div class="children children1">4</div>
    <div class="children">5</div>
    <div class="children children1">6</div>
    <div class="children">7</div>
    <div class="children children1">8</div>
    <div class="children">9</div>
    <div class="children children1">10</div>
    <div class="children">11</div>
    <div class="children children1">12</div>
    <div class="children">13</div>
    <div class="children children1">14</div>
</div>
</body>
</html>
添加了children1的模块都没有缩小
4.flex-basis
  • flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。
  • 浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
  • 它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的flex-basis属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
             width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 5px;
            width: 88px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
        .children1 {
            flex-basis: 60px;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children children1">2</div>
    <div class="children">3</div>
    <div class="children children1">4</div>
    <div class="children">5</div>
    <div class="children children1">6</div>
    <div class="children">7</div>
    <div class="children children1">8</div>
    <div class="children">9</div>
    <div class="children children1">10</div>
    <div class="children">11</div>
    <div class="children children1">12</div>
    <div class="children">13</div>
    <div class="children children1">14</div>
</div>
</body>
</html>
模块宽度变为60px
5.flex
  • flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
  • 该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
  • 建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
6.align-self
  • align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。
  • 默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex布局的align-self属性</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .parent {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
             width: 500px;
            height: 500px;
            background-color: palevioletred;
        }
        .children {
            margin: 5px;
            width: 88px;
            height: 88px;
            line-height: 20px;
            text-align: center;
            color: #fff;
            background-color: rebeccapurple;
            border: 1px solid red;
        }
        .children1 {
            align-self: flex-end;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="children">
        <p>1</p>
        <p>2</p>
    </div>
    <div class="children children1">2</div>
    <div class="children">3</div>
    <div class="children children1">4</div>
    <div class="children">5</div>
    <div class="children children1">6</div>
    <div class="children">7</div>
    <div class="children children1">8</div>
    <div class="children">9</div>
    <div class="children children1">10</div>
    <div class="children">11</div>
    <div class="children children1">12</div>
    <div class="children">13</div>
    <div class="children children1">14</div>
</div>
</body>
</html>
加了children1类的模块对齐方式发生改变

参考资料

https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://www.runoob.com/w3cnote/flex-grammar.html

相关文章

网友评论

    本文标题:CSS3 flex布局

    本文链接:https://www.haomeiwen.com/subject/uqqmaftx.html