Home Web Front-end CSS Tutorial Summary of CSS3 flex layout

Summary of CSS3 flex layout

Feb 21, 2020 pm 05:49 PM
css3 flex Summarize

Summary of CSS3 flex layout

CSS3 flex layout summary

In 2009, W3C proposed a new solution----Flex layout, which can be simple and complete , implement various page layouts responsively. Currently, it is supported by all browsers.

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 a Flex layout.

(Recommended learning: CSS tutorial)

Inline elements can also use Flex layout.

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

It should be noted that browsers with Webkit core must add the -webkit prefix.

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

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

Six properties commonly used in flex layout

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

##1、flex-directionThe attribute determines the direction of the main axis (that is, the arrangement direction of the items).

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

    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.

2. flex-wrap<strong></strong>Property definition, if one axis line cannot be arranged, how to wrap it.

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

  • nowrap (default): No line wrapping.
  • wrap: Wrap, with the first line at the top.
  • wrap-reverse: Wrap, the first line is below.

3. The flex-flow<strong></strong> attribute is the flex-direction attribute and the flex-wrap attribute. Short form, default value is row nowrap.

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

4, justify-content<strong></strong> attribute defines the alignment of the item on the main axis.

-content: flex-start | flex-end | center | space-between | space-
Copy after login

    flex-start (default): left-aligned
  • flex-end: right-aligned
  • center: centered
  • space-between : Align both ends, with equal spacing between items.
  • space-around: Each item is equally spaced on both sides. Therefore, the space between items is twice as large as the space between items and the border.

5, align-items<strong></strong>property defines how items are aligned on the cross axis.

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

  • flex-start: Align the starting point of the cross axis.
  • flex-end: The end point alignment of the cross axis.
  • center: Align the midpoint of the cross axis.
  • baseline: The baseline alignment of the first line of text of the item.
  • stretch (default value): If the item does not set a height or is set to auto, it will occupy the entire height of the container.

6, align-content<strong></strong> attribute defines the alignment of multiple axes. This property has no effect if the project has only one axis.

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

This attribute may take 6 values.

  • flex-start: Align with the starting point of the cross axis.
  • flex-end: Align with the end point of the cross axis.
  • center: Aligned with the midpoint of the cross axis.
  • space-between: Align with both ends of the cross axis, and the intervals between the axes are evenly distributed.
  • space-around: Each axis is equally spaced on both sides. Therefore, the distance between the axes is twice as large as the distance between the axes and the frame.
  • stretch (default value): The axis occupies the entire cross axis.
There are also the following 6 properties set on the project.

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self
order attribute Define the order in which items are sorted. The smaller the value, the higher the ranking. The default is 0.


.item {
  order: <integer>;
}
Copy after login

flex-growThe attribute defines the magnification ratio of the item. The default is 0, that is, if there is remaining space, it will not be enlarged.

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

If all items have a

flex-grow property of 1, they will equally divide the remaining space (if any). If one item's flex-grow property is 2 and all other items are 1, the former will occupy twice as much remaining space as the other items.

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

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

If the

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

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

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

flex属性是flex-grow, flex-shrinkflex-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)。

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
Copy after login
.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
Copy after login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to achieve wave effect with pure CSS3? (code example) How to achieve wave effect with pure CSS3? (code example) Jun 28, 2022 pm 01:39 PM

How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

Use CSS skillfully to realize various strange-shaped buttons (with code) Use CSS skillfully to realize various strange-shaped buttons (with code) Jul 19, 2022 am 11:28 AM

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

How to hide elements in css without taking up space How to hide elements in css without taking up space Jun 01, 2022 pm 07:15 PM

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

Summarize the usage of system() function in Linux system Summarize the usage of system() function in Linux system Feb 23, 2024 pm 06:45 PM

Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

How to implement lace borders in css3 How to implement lace borders in css3 Sep 16, 2022 pm 07:11 PM

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

It turns out that text carousel and image carousel can also be realized using pure CSS! It turns out that text carousel and image carousel can also be realized using pure CSS! Jun 10, 2022 pm 01:00 PM

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

How to enlarge the image by clicking the mouse in css3 How to enlarge the image by clicking the mouse in css3 Apr 25, 2022 pm 04:52 PM

Implementation method: 1. Use the ":active" selector to select the state of the mouse click on the picture; 2. Use the transform attribute and scale() function to achieve the picture magnification effect, the syntax "img:active {transform: scale(x-axis magnification, y Axis magnification);}".

Take you step by step to implement 3D dice using CSS Flex and Grid layout (with code) Take you step by step to implement 3D dice using CSS Flex and Grid layout (with code) Sep 23, 2022 am 09:58 AM

In front-end interviews, we are often asked how to implement dice/mahjong layout using CSS. The following article will introduce to you how to use CSS to create a 3D dice (Flex and Grid layout implement 3D dice). I hope it will be helpful to you!

See all articles