A detailed explanation of Flex layout in CSS3

青灯夜游
Release: 2022-11-02 13:36:21
forward
1670 people have browsed it

This article will take you through the Flex layout in CSS3, I hope it will be helpful to you!

A detailed explanation of Flex layout in CSS3

Introduction

What is Flex layout

Flex is the abbreviation of Flexible Box , also known as flexbox layout.
Flex layout composition:

  • flex container (flex container)
  • flex items (flex items)
  • main axismain axis
  • cross axiscross axis

The role of Flex layout

Before flex layout appeared, the web page layout methods were standard flow, floating, positioning, etc. It is relatively troublesome to solve more complex problems. [Learning video sharing:css video tutorial,web front-end]

Andflexlayout can be:

  • Automatic elastic scaling
  • Easily design a flexible responsive layout structure
  • Precisely and flexibly control the layout of block-level boxes
  • Applicable to both PC and mobile terminals

Flex container (parent element) property

First define the Flex container before using flex layout.

display:flex;
Copy after login

After defining the Flex container, you can use the corresponding attributes to change the layout of the sub-elements so that the sub-elements can be automatically squeezed or stretched.

Corresponding attributes:

1. justify-content 主轴元素对齐方式 2. align-items 交叉轴元素对齐方式 3. flex-direction 设置主轴方向 4. flex-wrap 主轴一行满了换行 5. align-content 交叉轴行对齐方式 6. flex-flow 同时设置 flex-direction和 flex-wrap属性
Copy after login

1. justify-content

Thejustify-contentattribute of the container can set the child element inAlignment of main axis direction. (Remember todisplay:flex;define the container first)

justify-content: center;//居中对齐
Copy after login

A detailed explanation of Flex layout in CSS3

justify-content: space-between;//间距在子元素之间
Copy after login

A detailed explanation of Flex layout in CSS3

justify-content: space-evenly;//主轴方向所有地方的间距都相等
Copy after login

A detailed explanation of Flex layout in CSS3

justify-content: space-around;//间距加在子元素的两侧(中间大的是两个子元素的加在一起)
Copy after login

A detailed explanation of Flex layout in CSS3
Code:

      主轴对齐方式  
1
2
3
Copy after login

2. align-items

Thealign-itemsattribute of the container can be set to The alignment of the element in thecross-axis direction.

From this we can set the container attributesjustify-contentandalign-itemsto be centered so that the element can be perfectly centered.

align-items: center;//居中
Copy after login

A detailed explanation of Flex layout in CSS3

align-items: stretch;//拉伸,默认值(现有状态,这里测试去掉子级的高度)
Copy after login

A detailed explanation of Flex layout in CSS3

align-items: flex-start;//将子元素在容器顶部对齐
Copy after login

A detailed explanation of Flex layout in CSS3

align-items: flex-end;//将子元素在容器底部对齐
Copy after login

A detailed explanation of Flex layout in CSS3
Code:

      交叉轴对齐方式  
1
2
3
Copy after login

3. flex-direction

Theflex-directionproperty of the container can change the main axis direction of the flex layout. The default flex spindle direction is horizontal to the right. If you change the main axis direction, the cross axis direction will also change.

flex-direction: column;//主轴方向为垂直方向(从上到下)
Copy after login

A detailed explanation of Flex layout in CSS3

flex-direction: column-reverse;//主轴方向为垂直方向(从下到上)
Copy after login

A detailed explanation of Flex layout in CSS3

flex-direction: row;//主轴方向为水平方向(从左到右)
Copy after login

A detailed explanation of Flex layout in CSS3

flex-direction: row-reverse;//主轴方向为水平方向(从右到左)
Copy after login

A detailed explanation of Flex layout in CSS3
Achieve vertical centering after modifying the main axis direction :

display:flex; flex-direction: column; justify-content: center;
Copy after login

A detailed explanation of Flex layout in CSS3

4. flex-wrap

After defining theflexcontainer, if the child element passes If the width exceeds the main axis direction, the child elements in the container will automatically expand and contract.
Such as:

      弹性盒子换行  
1
2
3
4
5
6
7
8
9
Copy after login

A detailed explanation of Flex layout in CSS3
Solution: Theflex-wrapproperty of the container can make sub-elements beyond the main axis of the container display in new lines.

flex-wrap: nowrap;//默认值,不换行 flex-wrap: wrap;//换行,从上到下
Copy after login

A detailed explanation of Flex layout in CSS3

flex-wrap: wrap-reverse;//换行,从下到上
Copy after login

A detailed explanation of Flex layout in CSS3

5. align-content

##align- of the container The contentattribute can adjust the alignment of sub-element rows (needs to be set first and then).

align-content: center;//居中对齐 align-content: space-around;//间距加在子元素的两侧(中间大的是两个子元素的加在一起) align-content: space-between;//间距在子元素之间
Copy after login

前三者的属性跟主轴对齐方式一样就不再赘述。

align-content: stretch;拉伸,默认值(现有状态,这里测试去掉子级的高度)
Copy after login

A detailed explanation of Flex layout in CSS3

6.flex-flow

flex-flow属性是用于同时设置flex-directionflex-wrap属性的简写属性。

flex-flow: row wrap;
Copy after login

Flex项(子元素)属性

我们可以设置相应属性让flex 容器的直接子元素成为弹性(flex)项目。(在使用flex布局之前首先定义 Flex 容器。

相应属性:

1. flex-grow 2. flex-shrink 3. flex-basis 4. flex 5. align-self 6. order
Copy after login

1. flex-grow

使用flex-grow属性来定义弹性盒子内部子元素的放大比例(当所有子元素宽度之和小于父元素的宽度时子元素如何分配父元素的剩余空间)。

      Document  
Copy after login

A detailed explanation of Flex layout in CSS3

2. flex-shrink

使用flex-shrink属性来定义弹性盒子内部子元素的缩小比例(当所有子元素宽度之和大于父元素的宽度时子元素如何缩小自己的宽度)。

      Document  
Copy after login

A detailed explanation of Flex layout in CSS3

3. flex-basis

使用flex-basis属性来设置子元素的宽度,默认值为auto(作用跟width一样,优先级比width高,就算width在后面也会显示flex-basis)。

4. flex

使用flex属性来同时设置flex-grow、flex-shrink、flex-basis这3个属性,flex属性就是一个复合属性。
实际应用一般用复合属性。
语法:

flex: grow shrink basis;//顺序不能改变,默认值为0 1 auto;
Copy after login

5. align-self

使用align-self属性设置子元素项目的对齐方式。

注意:align-self属性会覆盖容器的align-items属性所设置的对齐方式。

      Document  
1
2
3
Copy after login

A detailed explanation of Flex layout in CSS3

6. order

使用order属性来定义子元素的排列顺序。

      Document  
1
2
3
Copy after login

A detailed explanation of Flex layout in CSS3

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of A detailed explanation of Flex layout in CSS3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!