Flex 布局 (弹性布局/Flexible Box)

2018-02-23  本文已影响0人  静候那一米阳光

Flex 布局 (弹性布局/Flexible Box)

根据 Flex 布局教程:语法篇-阮一峰 整理,加入个人简单的理解。

更多实例参考Flex 布局教程:实例篇-阮一峰

容器

.box {
    display: flex;
}

容器属性

flex-direction

主轴的方向(即项目的排列方向).

flex-direction: row | row-reverse | column | column-reverse;

flex-wrap

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

flex-wrap: nowrap | wrap | wrap-reverse;

flex-flow

flex-direction属性和flex-wrap属性的简写形式.默认是 row nowrap

flex-flow: <flex-direction> || <flex-wrap>;

justify-content

项目在主轴上的对齐方式.

align-items

项目在交叉轴上的对齐方式.

align-items: flex-start | flex-end | center | baseline | stretch;

align-content

定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。个人认为是在flex-wrap:wrap一行放不下时的可看到效果。

align-content: flex-start | flex-end | center | space-between | space-around | stretch;

效果类似于justify-content,是是每一行为一个单位了,而justify-content是行内的每一个元素为一个单位。

项目属性

order

定义项目的排列顺序。数值越小,排列越靠前,默认为0。可以是负的。

order: <integer>; /* default 0 ,例如order:-1,order:1*/

flex-grow

flex-grow: <number>; /* default 0 */

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

当空间或刚好不足时,且no-wrap时,按照原来的宽度比例缩小。当空间多时,flex-grow大的拉伸的幅度大。

flex-shrink

定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

flex-shrink: <number>; /* default 1 */

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。负值对该属性无效。

flex-basis

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

flex-basis: <length> | auto; /* default auto */

flex

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

flex: auto | none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]

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

align-self

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

align-self: auto | flex-start | flex-end | center | baseline | stretch;

该属性可取6个值,除了auto,其他都与align-items属性完全一致。

【做实验的代码】

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
  html *{
    margin: 0;
    padding: 0;
  }
  .flex-box {
    display: flex;
    flex-direction: row;
    background-color: red;
    width: 100%;
    height: 500px;
    flex-wrap: wrap;
    /*justify-content: space-between;*/
    align-items: flex-start;
    /*align-content: space-around;*/
  }
  
  .flex-item {
    height: 100px;
    /*width: 100%;*/
    background-color: blue;
    border: 1px solid #fff;
    color: #fff;
    font-size: 24px;
  }
  </style>
</head>

<body>
  <section class="flex-box">
    <article class="flex-item" style="width:1000px;order:0;flex-grow:0;flex-shrink: 0;flex-basis:2;">1</article>
    <article class="flex-item" style="width:200px;order:0;flex-grow:0;flex-shrink: 0;flex-basis:2;">2</article>
    <article class="flex-item" style="width:300px;order:0;align-self: flex-end;">3</article>
    <article class="flex-item" style="width:100px;order:0">4</article>
    <article class="flex-item" style="width:200px;order:0">5</article>
    <article class="flex-item" style="width:300px;order:0">6</article>
    <article class="flex-item" style="width:100px;order:0">7</article>
    <article class="flex-item" style="width:200px;order:0;">8</article>
    <article class="flex-item" style="width:300px;order:0;flex-grow:1;align-self: center;">9</article>
  </section>
</body>

</html>
上一篇 下一篇

猜你喜欢

热点阅读