search
HomeWeb Front-endCSS TutorialDetailed introduction to the basic syntax of flex layout

This article brings you a detailed introduction to the basic syntax of flex layout. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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

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.

flex layout: .box{display:flex;}

Inline elements can also use flex layout: .box{display:inline-flex;}

Browsers with Webkit core must add the -webkit prefix: .box{display:-webkit-flex; display:flex ;}

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

2. Basic concepts

Flex container (flex container): Elements that adopt Flex layout are called flex containers.

Flex project (flex item): All child elements of the flex container automatically become container members, that is, flex items.

The two axes of the container: the horizontal main axis (main axis), the vertical cross axis (cross axis).

Main axis: start position->main start, end position->main end, projects are arranged along the main axis by default.

Cross axis: start position-> cross start, end position->cross end

The main axis space occupied by a single item in the container is called->main size, and the cross-axis space is called->cross size

3.FLex attributes

flex attributes are divided into container attributes and project attributes.

Container properties: flex-direction, flex-wrap, flex-flow, justify-content, align-items, align-content

Item properties: order, flex-grow, flex-shrink, flex-basis, flex , align-self

3.1ContainerProperties

flex-direction: Determine the direction of the main axis (i.e. the direction of item arrangement)

Row: Horizontal direction, from left to right. (Default attribute)

Row-reverse: Horizontal direction, from right to left.

Column: Vertical direction, from top to bottom.

column-reverse: Vertical direction, from bottom to top.

flex-wrap: Specifies how to wrap if an axis cannot fit.

nowrap: No line wrapping (default attribute)

Wrap: Line wrapping, the first line is at the top

wrap-reverse: Line wrapping, the first line is at the bottom, (only the number of lines is reversed, the single line still starts from Arranged from left to right, each row is).

flex-flow: Combining flex-direction and flex-wrap, the writing is as follows

 .box{flex-flow :column wrap-reverse}

justify-content: Define the alignment of the item on the main axis

flex-start: Left-aligned (default value), the item is aligned on the main axis Align the starting point of

flex-end: right-aligned, the items are aligned with the end point of the main axis

center: centered

space-between: aligned at both ends, the intervals between items are equal

Space-around: The space on both sides of each item is equal. Therefore, the distance between items is twice as large as the distance between items and borders

align-items: Define the alignment of items on the cross axis (Only single line, multi-line alignment Defined by align-content)

stretch: If the item does not set a height or is set to auto, it will occupy the height of the entire container (default value)

flex-start: from top Go to the bottom, take the starting point of the cross axis as the baseline, and extend to the end point

 flex-end: From bottom to top, take the end point of the cross axis as the baseline, and extend toward the starting point

center: Cross axis Align the midpoint of the item, and place the midpoint of the item on the midpoint of the cross axis

baseline: Align with the first line of text of each item in a line.

align- content: Defines the alignment along the cross axis when there are multi-line items in the container. If the item has only one axis, this property has no effect

stretch: The axis is full The entire cross axis (each row has an axis), default attribute

flex-start: align with the starting point of the cross axis

flex-end: align with the end point of the cross axis

Center: Align with the midpoint of the cross axis

Space-between: Multi-line items are evenly spaced on the cross axis

Space-around: Multi-line items are on both sides of the cross axis intervals are equal. Therefore, the distance between items on the cross axis is twice as large as the distance between items and borders

 Note: justify-content, align-items and align-content are properties of the container, not the properties of the item. They are to set the items in a container to be aligned at a certain position on the axis

3.2 Project attributes

order: Define the order of items. The smaller the value, the higher the arrangement. The default value is 0, written as follows

 .item{order:5}

flex-grow: Define the attribute item magnification ratio, the default is 1

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

flex-shrink: Define the shrinkage ratio of the item , the default is 1

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. Negative values ​​are invalid for this property

flex-basis : Defines the main axis space (main size) occupied by the project before allocating excess space. The browser calculates whether there is excess space on the main axis based on this attribute. Its default value is auto, which is the original size of the project

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

flex: Combining the properties of flex-grow, flex-shrink and flex-basis, the default value is 0 1 auto, it is recommended to use

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

align-self: allows a single item to be aligned with other items Different alignment methods can override the align-items attribute. The default value is auto, which means inheriting the align-items attribute of the parent element. If there is no parent element, it is equivalent to stretch

. This attribute may take 6 values. , except auto, everything else is exactly the same as the align-items attribute

[Related recommendations: CSS video tutorial]

The above is the detailed content of Detailed introduction to the basic syntax of flex layout. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.