Flex layout grammar tutorial

Flex layout syntax tutorial

https://www.runoob.com/w3cnote/flex-grammar.html

Web page layout (layout) is a key application of CSS.

Share a picture

Tradition of layout The solution, based on the box model, relies on display attribute + position attribute + float attribute. It is very inconvenient for those special layouts, for example, vertical centering is not easy to achieve.

Share pictures

In 2009, W3C proposed a new solution—Flex layout, which can implement various page layouts simply, completely, and responsively. Currently, it has been supported by all browsers, which means that this feature can now be used very safely.

Share a picture

Flex layout will Become the first choice for future layout. This article describes the syntax of Flex layout.

The following content mainly refers to the following two articles: A Complete Guide to Flexbox and A Visual Guide to CSS3 Flexbox Properties.

One, what is the Flex layout?

Flex is the abbreviation of Flexible Box, which means “flexible layout” and is used to provide maximum flexibility for the box model.

Any container can be designated as a Flex layout.

.box{ display< span class="pun">: flex; }

Inline elements can also use Flex layout.

.box{ display< span class="pun">: inline-flex; }

Webkit kernel browsers must be prefixed with -webkit.

.box{ display< span class="pun">: -webkit-flex; /* Safari */ display:  flex; }< /span>

Note that after setting the Flex layout, the float, clear and vertical-align attributes of the child elements will be invalid.

Second, basic concepts

The elements that use Flex layout are called Flex containers, or “containers” for short. All of its child elements automatically become members of the container, called Flex items (flex items), or “items” for short.

Share a picture

The container exists by default Two axes: the horizontal main axis (main axis) and the vertical cross axis (cross axis). The start position of the main axis (the intersection with the frame) is called main start, and the end position is called main end; the start position of the cross axis is called cross start, and the end position is called cross end.

The items are arranged along the main axis by default. The main axis space occupied by a single project is called the main size, and the cross axis space occupied is called the cross size.

3. Attributes of the container

The following 6 attributes are set on the container.

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

3.1 flex-direction attribute

flex-direction attribute determines the main axis Direction (that is, the direction in which the items are arranged).

.box { flex< span class="pun">-direction: row | row-reverse | column | column-reverse;  }< /span>

share picture

It may have 4 values.

  • row (default value): The main axis is horizontal, and the starting point is at the left end.
  • row-reverse: The main axis is in the horizontal direction, and the starting point is at the right end.
  • column: The main axis is in the vertical direction, and the starting point is at the upper edge.
  • column-reverse: The main axis is in the vertical direction, and the starting point is at the bottom edge.

3.2 flex-wrap attribute

By default, items are arranged on a line (also known as “axis line”). The flex-wrap attribute defines how to wrap if an axis line cannot fit.

share picture

.box{ flex-wrap: nowrap | wrap | wrap-reverse; }< /span>

It may take three values.

(1) nowrap (default): do not wrap.

share picture

(2) wrap: wrap, the first line is at the top.

share picture

(3) wrap-reverse: wrap, the first line is below.

share picture

3.3 flex- flow

The flex-flow attribute is a short form of the flex-direction attribute and the flex-wrap attribute, and the default value is row nowrap.

.box { flex< span class="pun">-flow: <flex-direction> <flex-wrap>; }  span>

3.4 justify-content attribute

The justify-content attribute defines the alignment of the item on the main axis.

.box { justify< span class="pun">-content: flex-start | flex-end | center | space- between | space-around; }< /span>

share picture

It may take 5 values, and the specific alignment is related to the direction of the axis. The following assumes that the main axis is from left to right.

  • flex-start (default): left-aligned
  • flex-end: right-aligned
  • center: centered
  • space-between: align at both ends, the space between items is equal.
  • space-around: The space on both sides of each item is equal. Therefore, the interval between items is twice as large as the interval between items and the border.

3.5 align-items attribute

The align-items attribute defines how items are aligned on the cross axis.

.box { align< span class="pun">-items: flex-start | flex-end | center | baseline | stretch; }< /span>

share picture

It may take 5 values. The specific alignment is related to the direction of the cross axis. The following assumes that the cross axis is from top to bottom.

  • flex-start: align the starting point of the cross axis.
  • flex-end: align the end points of the cross axis.
  • center: align the midpoint of the cross axis.
  • baseline: The baseline alignment of the first line of the project.
  • stretch (default): If the item has no height set or set to auto, it will occupy the height of the entire container.

3.6 align-content attribute

The align-content attribute defines the alignment of multiple axes. If the project has only one axis, this property has no effect.

.box { align< span class="pun">-content: flex-start | flex-end | center | space- between | space-around | stretch; }< /span>

share picture

This attribute may take 6 values.

  • flex-start: align with the starting point of the cross axis.
  • flex-end: align with the end of the cross axis.
  • center: align with the midpoint of the cross axis.
  • space-between: align with both ends of the cross axis, and the space between the axes is evenly distributed.
  • space-around: The space on both sides of each axis is equal. Therefore, the interval between the axis is twice as large as the interval between the axis and the frame.
  • stretch (default): The axis occupies the entire cross axis.

Four. Project attributes

The following 6 attributes are set on the project.

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

4.1 order attribute

The order attribute defines the order of the items. The smaller the value, the higher the arrangement, and the default is 0.

.item { order< span class="pun">: ; }

Share a picture

4.2 flex-grow attribute

< The p>flex-grow attribute defines the zoom ratio of the item, the default is 0, that is, if there is remaining space, it will not zoom.

.item { flex< span class="pun">-grow: ; /* default 0 */ }< /pre> 

Share picture

If all items are The flex-grow properties are all 1, and they will equally divide the remaining space (if any). If the flex-grow property of one item is 2 and the other items are all 1, the remaining space occupied by the former will be twice that of other items.

4.3 flex-shrink attribute

The flex-shrink attribute defines the reduction ratio of the item, the default is 1, that is, if the space is insufficient, the item will shrink.

.item { flex< span class="pun">-shrink: ; /* default 1 */ }< /pre> 

Share picture

If all items are The flex-shrink attributes are all 1, and when the space is insufficient, all will be reduced proportionally. If the flex-shrink property of one item is 0 and all other items are 1, then the former will not shrink when the space is insufficient.

Negative values ​​are invalid for this attribute.

4.4 flex-basis attribute

The flex-basis attribute defines the main size of the project before the extra space is allocated. The browser calculates whether there is extra space in the main axis based on this attribute. Its default value is auto, which is the original size of the project.

.item { flex< span class="pun">-basis:  | auto; /* default auto */ }< /pre> 

It can be set to the same value as the width or height attribute (such as 350px), and the item will occupy a fixed space.

4.5 flex attribute

The flex attribute is shorthand for flex-grow, flex-shrink and flex-basis, and the default value is 0 1 auto. The last two attributes are optional.

.item { flex< span class="pun">: none | [ <'flex-grow'> << span class="str">'flex-shrink'>? || <'flex-basis'> ] }< /span>

This attribute has two shortcut values: auto (1 1 auto) and none (0 0 auto).

It is recommended to use this attribute first, instead of writing three separate attributes, because the browser will calculate the relevant value.

4.6 align-self attribute

The align-self attribute allows a single item to be aligned differently from other items, and can override the align-items attribute. The default value is auto, which means the align-items attribute of the parent element is inherited. If there is no parent element, it is equivalent to stretch.

.item { align< span class="pun">-self: auto | flex-start | flex-end | center < span class="pun">| baseline | stretch; }< /span>

share picture

This attribute may take 6 values, except for auto, the others are exactly the same as the align-items attribute.

Web page layout (layout) is a key application of CSS.

Share a picture

Traditional layout The solution, based on the box model, relies on display attribute + position attribute + float attribute. It is very inconvenient for those special layouts, for example, vertical centering is not easy to achieve.

Share pictures

In 2009, W3C proposed a new solution—Flex layout, which can implement various page layouts simply, completely, and responsively. Currently, it has been supported by all browsers, which means that this feature can now be used very safely.

Share a picture

Flex layout will Become the first choice for future layout. This article describes the syntax of Flex layout.

The following content mainly refers to the following two articles: A Complete Guide to Flexbox and A Visual Guide to CSS3 Flexbox Properties.

One, what is the Flex layout?

Flex is the abbreviation of Flexible Box, which means "flexible layout" and is used to provide maximum flexibility for the box model.

Any container can be designated as a Flex layout.

.box{ display< span class="pun">: flex; }

Inline elements can also use Flex layout.

.box{ display< span class="pun">: inline-flex; }

Webkit kernel browsers must be prefixed with -webkit.

.box{ display< span class="pun">: -webkit-flex; /* Safari */ display:  flex; }< /span>

Note that after setting the Flex layout, the float, clear and vertical-align attributes of the child elements will be invalid.

Second, basic concepts

The elements that use Flex layout are called Flex containers, or "containers" for short. All of its child elements automatically become members of the container, called Flex items (flex items), or "items" for short.

Share a picture

The container exists by default Two axes: the horizontal main axis (main axis) and the vertical cross axis (cross axis). The start position of the main axis (the intersection with the frame) is called main start, and the end position is called main end; the start position of the cross axis is called cross start, and the end position is called cross end.

The items are arranged along the main axis by default. The main axis space occupied by a single project is called the main size, and the cross axis space occupied is called the cross size.

3. Attributes of the container

The following 6 attributes are set on the container.

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

3.1 flex-direction attribute

flex-direction attribute determines the main axis Direction (that is, the direction in which the items are arranged).

.box { flex< span class="pun">-direction: row | row-reverse | column | column-reverse;  }< /span>

share picture

It may have 4 values.

  • row (default value): The main axis is horizontal, and the starting point is at the left end.
  • row-reverse: The main axis is in the horizontal direction, and the starting point is at the right end.
  • column: The main axis is in the vertical direction, and the starting point is at the upper edge.
  • column-reverse: The main axis is in the vertical direction, and the starting point is at the bottom edge.

3.2 flex-wrap attribute

By default, items are arranged on a line (also known as "axis line"). The flex-wrap attribute defines how to wrap if an axis line cannot fit.

share picture

.box{ flex-wrap: nowrap | wrap | wrap-reverse; }< /span>

It may take three values.

(1) nowrap (default): do not wrap.

share picture

(2) wrap: wrap, the first line is at the top.

share picture

(3) wrap-reverse: wrap, the first line is below.

share picture

3.3 flex- flow

The flex-flow attribute is a short form of the flex-direction attribute and the flex-wrap attribute, and the default value is row nowrap.

.box { flex< span class="pun">-flow: <flex-direction> <flex-wrap>; }  span>

3.4 justify-content attribute

The justify-content attribute defines the alignment of the item on the main axis.

.box { justify< span class="pun">-content: flex-start | flex-end | center | space- between | space-around; }< /span>

share picture

It may take 5 values, and the specific alignment is related to the direction of the axis. The following assumes that the main axis is from left to right.

  • flex-start (default): left-aligned
  • flex-end: right-aligned
  • center: centered
  • space-between: align at both ends, the space between items is equal.
  • space-around: The space on both sides of each item is equal. Therefore, the interval between items is twice as large as the interval between items and the border.

3.5 align-items attribute

The align-items attribute defines how items are aligned on the cross axis.

.box { align< span class="pun">-items: flex-start | flex-end | center | baseline | stretch; }< /span>

share picture

It may take 5 values. The specific alignment is related to the direction of the cross axis. The following assumes that the cross axis is from top to bottom.

  • flex-start: align the starting point of the cross axis.
  • flex-end: align the end points of the cross axis.
  • center: align the midpoint of the cross axis.
  • baseline: The baseline alignment of the first line of the project.
  • stretch (default): If the item has no height set or set to auto, it will occupy the height of the entire container.

3.6 align-content attribute

The align-content attribute defines the alignment of multiple axes. If the project has only one axis, this property has no effect.

.box { align< span class="pun">-content: flex-start | flex-end | center | space- between | space-around | stretch; }< /span>

share picture

This attribute It may take 6 values.

  • flex-start: align with the starting point of the cross axis.
  • flex-end: align with the end of the cross axis.
  • center: align with the midpoint of the cross axis.
  • space-between: align with both ends of the cross axis, and the space between the axes is evenly distributed.
  • space-around: The space on both sides of each axis is equal. Therefore, the interval between the axis is twice as large as the interval between the axis and the frame.
  • stretch (default): The axis occupies the entire cross axis.

Four. Project attributes

The following 6 attributes are set on the project.

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

4.1 order attribute

The order attribute defines the order of the items. The smaller the value, the higher the arrangement, and the default is 0.

.item { order< span class="pun">: ; }

Share a picture

4.2 flex-grow attribute

< The p>flex-grow attribute defines the magnification ratio of the item, the default is 0, that is, if there is remaining space, it will not be magnified.

.item { flex< span class="pun">-grow: ; /* default 0 */ }< /pre>  

分享图片

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

4.3 flex-shrink属性

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

.item { flex-shrink: ; /* default 1 */ }

分享图片

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

负值对该属性无效。

4.4 flex-basis属性

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

.item { flex-basis:  | auto; /* default auto */ }

它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。

4.5 flex属性

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

.item { flex: none | [ <‘flex-grow‘> <‘flex-shrink‘>? || <‘flex-basis‘> ] }

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

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

4.6 align-self属性

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

.item { align-self: auto | flex-start | flex-end | center | baseline | stretch; }

分享图片

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

网页布局(layout)是CSS的一个重点应用。

分享图片

布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性。它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。

分享图片

2009年,W3C提出了一种新的方案—-Flex布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。

分享图片

Flex布局将成为未来布局的首选方案。本文介绍Flex布局的语法。

以下内容主要参考了下面两篇文章:A Complete Guide to Flexbox 和 A Visual Guide to CSS3 Flexbox Properties。

一、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属性将失效。

二、基本概念

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

分享图片

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。

项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。

三、容器的属性

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

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

3.1 flex-direction属性

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

.box { flex-direction: row | row-reverse | column | column-reverse; }

分享图片

它可能有4个值。

  • row(默认值):主轴为水平方向,起点在左端。
  • row-reverse:主轴为水平方向,起点在右端。
  • column:主轴为垂直方向,起点在上沿。
  • column-reverse:主轴为垂直方向,起点在下沿。

3.2 flex-wrap属性

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

分享图片

.box{ flex-wrap: nowrap | wrap | wrap-reverse; }

它可能取三个值。

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

分享图片

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

分享图片

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

分享图片

3.3 flex-flow

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

.box { flex-flow: <flex-direction> <flex-wrap>; }

3.4 justify-content属性

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

.box { justify-content: flex-start | flex-end | center | space-between | space-around; }

分享图片

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

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

3.5 align-items属性

align-items属性定义项目在交叉轴上如何对齐。

.box { align-items: flex-start | flex-end | center | baseline | stretch; }

分享图片

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

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

3.6 align-content属性

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

.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }

分享图片

该属性可能取6个值。

  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch(默认值):轴线占满整个交叉轴。

四、项目的属性

以下6个属性设置在项目上。

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

4.1 order属性

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

.item { order: ; }

分享图片

4.2 flex-grow属性

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

.item { flex-grow: ; /* default 0 */ }

分享图片

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

4.3 flex-shrink属性

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

.item { flex-shrink: ; /* default 1 */ }

分享图片

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

负值对该属性无效。

4.4 flex-basis属性

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

.item { flex-basis:  | auto; /* default auto */ }

它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。

4.5 flex属性

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

.item { flex: none | [ <‘flex-grow‘> <‘flex-shrink‘>? || <‘flex-basis‘> ] }

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

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

4.6 align-self属性

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

.item { align-self: auto | flex-start | flex-end | center | baseline | stretch; }

分享图片

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

Leave a Comment

Your email address will not be published.