Flex layout syntax tutorial

伊谢尔伦
Release: 2016-11-29 09:59:50
Original
1278 people have browsed it

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

Flex layout syntax tutorial

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

Flex layout syntax tutorial

In 2009, W3C proposed a new solution - Flex layout, which can realize various page layouts simply, completely and responsively. Currently, it is supported by all browsers, which means it is now safe to use this feature.

 Flex layout will become the preferred solution for future layouts. This article introduces its syntax, and the next article gives the Flex writing method of common layouts.

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

 1. What is Flex layout?

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

Any container can be designated as Flex layout.

.box{
  display: flex;
}
Copy after login

  Inline elements can also use Flex layout.

.box{
  display: inline-flex;
}
Copy after login

 Browsers with Webkit core must add the -webkit prefix.

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

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

2. Basic Concepts

Elements that use Flex layout are called Flex containers (flex containers), or "containers" for short. All its child elements automatically become container members, called Flex items (flex items), referred to as "items".

Flex layout syntax tutorial

Containers have two axes by default: the horizontal main axis and the vertical cross axis. The starting position of the main axis (the intersection with the border) is called main start, and the ending position is called main end; the starting position of the cross axis is called cross start, and the ending position is called cross end.

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

3. Container properties

The following 6 properties are set on the container.

flex-direction

flex-wrap

flex-flow

justify-content

align-items

align-content

 3.1 flex-direction attribute

 The flex-direction attribute determines the direction of the main axis (ie, item arrangement direction).

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

Flex layout syntax tutorial

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 horizontal and the starting point is at the right end.

column: The main axis is vertical, and the starting point is on the upper edge.

column-reverse: The main axis is vertical and the starting point is at the lower edge.

 3.2 flex-wrap attribute

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

Flex layout syntax tutorial

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

 It may take three values.

(1) nowrap (default): No line wrapping.

Flex layout syntax tutorial

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

Flex layout syntax tutorial

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

Flex layout syntax tutorial

 3.3 flex-flow

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

.box {
  flex-flow: <flex-direction> || <flex-wrap>;
}
Copy after login

  3.4 justify-content属性

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

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

Flex layout syntax tutorial

 它可能取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;
}
Copy after login

Flex layout syntax tutorial

它可能取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;
}
Copy after login

Flex layout syntax tutorial

 该属性可能取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: <integer>;
}
Copy after login

Flex layout syntax tutorial

4.2 flex-grow属性

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

.item {
  flex-grow: <number>; /* default 0 */
}
Copy after login

Flex layout syntax tutorial

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

  4.3 flex-shrink属性

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

.item {
  flex-shrink: <number>; /* default 1 */
}
Copy after login

Flex layout syntax tutorial

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

  负值对该属性无效。

  4.4 flex-basis属性

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

.item {
  flex-basis: <length> | auto; /* default auto */
}
Copy after login

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

  4.5 flex属性

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

.item {
  flex: none | [ <&#39;flex-grow&#39;> <&#39;flex-shrink&#39;>? || <&#39;flex-basis&#39;> ]
}
Copy after login

  该属性有两个快捷值: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;
}
Copy after login

Flex layout syntax tutorial

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template