Detailed explanation of how CSS3 implements flexible layout
1. CSS3 Flexible Box
Flexible box is a new layout mode of CSS3.
CSS3 Flexible Box (Flexible Box or flexbox) is a layout method that ensures that elements have appropriate behavior when the page needs to adapt to different screen sizes and device types.
The purpose of introducing the flexible box layout model is to provide a more efficient way to arrange, align and allocate empty space to sub-elements in a container.
2. Browser support
The number in the table indicates the version number of the first browser that supports this attribute.
The -webkit- or -moz- immediately following the number is the prefix of the specified browser.
##Basic support (single-line flexbox)29.0 21.0 -webkit-11.022.0 18.0 -moz-6.1 -webkit-12.1 -webkit-Multi-line flexbox29.0 21.0 -webkit-11.028.06.1 -webkit-17.0 15.0 -webkit- 12.1Attributes | ![]() |
![]() |
![]() |
![]() |
![]() |
3. CSS3 Flexible Box Content
The flexible box is composed of a flexible container (Flex container) and a flexible sub-element (Flex item) . A flexible container is defined as a flexible container by setting the value of the display property to flex or inline-flex. The flexible container contains one or more flexible sub-elements. Note: The outside of the flexible container and inside the flexible sub-element are rendered normally. The flex box only defines how the flex child elements are laid out within the flex container. Flexible sub-elements are usually displayed in one line within the flexible box. By default there is only one row per container. The following elements show the elastic child elements displayed in a row, from left to right:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
4. CSS3 Flexible Box Commonly used attributes
AttributeDescriptionflex -directionSpecify the arrangement of child elements in the flexible containerflex-wrapSet whether to wrap the child elements of the flexible box when they exceed the parent containerflex-flowAbbreviation for flex-direction and flex-wrapalign-items Set the alignment of the flex box elements in the side axis (vertical axis) directionalign-contentModify the behavior of the flex-wrap attribute, similar to align-items, But instead of setting the alignment of child elements, set the row alignmentjustify-contentSet the alignment of the flexible box element in the main axis (horizontal axis) direction1. flex-direction 属性
决定项目的方向。
注意:如果元素不是弹性盒对象的元素,则 flex-direction 属性不起作用。
1 |
|
属性值
值 | 描述 |
---|---|
row | 默认值。元素将水平显示,正如一个行一样。 |
row-reverse | 与 row 相同,但是以相反的顺序。 |
column | 元素将垂直显示,正如一个列一样。 |
column-reverse | 与 column 相同,但是以相反的顺序。 |
2. flex-wrap 属性
flex-wrap 属性规定flex容器是单行或者多行,同时横轴的方向决定了新行堆叠的方向。
值 | 描述 |
---|---|
nowrap | 默认值。规定元素不拆行或不拆列。 |
wrap | 规定元素在必要的时候拆行或拆列。 |
wrap-reverse | 规定元素在必要的时候拆行或拆列,但是以相反的顺序。 |
1 |
|
可以取三个值:
(1) nowrap (默认):不换行。
(2)wrap:换行,第一行在上方。
(3)wrap-reverse:换行,第一行在下方。
3. flex-flow 属性
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
1 |
|
4. align-items属性
align-items 属性定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。
值 | 描述 |
---|---|
stretch | 默认值。项目被拉伸以适应容器。 |
center | 项目位于容器的中心。 |
flex-start | 项目位于容器的开头。 |
flex-end | 项目位于容器的结尾。 |
baseline | 项目位于容器的基线上。 |
5. justify-content属性
justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式。
值 | 描述 |
---|---|
flex-start | 默认值。项目位于容器的开头。 |
flex-end | 项目位于容器的结尾。 |
center | 项目位于容器的中心。 |
space-between | 项目位于各行之间留有空白的容器内。 |
space-around | 项目位于各行之前、之间、之后都留有空白的容器内。 |
五、弹性子元素属性
属性 | 描述 |
---|---|
order | 设置弹性盒子的子元素排列顺序。 |
flex-grow | 设置或检索弹性盒子元素的扩展比率。 |
flex-shrink | 指定了 flex 元素的收缩规则。flex 元素仅在默认宽度之和大于容器的时候才会发生收缩,其收缩的大小是依据 flex-shrink 的值。 |
flex-basis | 用于设置或检索弹性盒伸缩基准值。 |
flex | 设置弹性盒子的子元素如何分配空间。 |
align-self | 在弹性子元素上使用。覆盖容器的 align-items 属性。 |
1. order属性
1 |
|
2. flex-grow属性
1 |
|
3. flex-shrink属性
1 |
|
4. flex-basis属性
1 |
|
auto:默认值。长度等于元素的长度。如果该项目未指定长度,则长度将根据内容决定。
5. flex属性
flex 属性用于设置或检索弹性盒模型对象的子元素如何分配空间。
flex 属性是 flex-grow、flex-shrink 和 flex-basis 属性的简写属性。
1 2 3 |
|
值 | 描述 |
---|---|
flex-grow | 一个数字,规定项目将相对于其他元素进行扩展的量。 |
flex-shrink | 一个数字,规定项目将相对于其他元素进行收缩的量。 |
flex-basis | 项目的长度。合法值:"auto"、"inherit" 或一个后跟 "%"、"px"、"em" 或任何其他长度单位的数字。 |
auto | 与 1 1 auto 相同。 |
none | 与 0 0 auto 相同。 |
initial | 设置该属性为它的默认值,即为 0 1 auto。 |
inherit | 从父元素继承该属性。 |
6. align-self属性
1 2 3 |
|
值 | 描述 |
---|---|
auto | 默认值。元素继承了它的父容器的 align-items 属性。如果没有父容器则为 "stretch"。 |
stretch | 元素被拉伸以适应容器。 |
center | 元素位于容器的中心。 |
flex-start | 元素位于容器的开头。 |
flex-end | 元素位于容器的结尾。 |
baseline | 元素位于容器的基线上。 |
initial | 设置该属性为它的默认值。 |
inherit | 从父元素继承该属性。 |
取值同 align-items。
参考
本文转载自:https://www.jianshu.com/p/5856c4ae91f2
相关推荐:CSS视频教程
The above is the detailed content of Detailed explanation of how CSS3 implements flexible layout. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

There are two ways to center a div in HTML: Use the text-align attribute (text-align: center): For simpler layouts. Use flexible layout (Flexbox): Provide more flexible layout control. The steps include: enabling Flexbox (display: flex) in the parent element. Set the div as a Flex item (flex: 1). Use the align-items and justify-content properties for vertical and horizontal centering.

How to use CSS Flexible Layout to implement equal-height column layout CSS Flexible Box Layout (CSS FlexibleBox Layout), referred to as Flex layout, is a module used for page layout. Flex layout makes it easier for us to implement equal-height column layouts, so that they can be displayed at equal heights regardless of the height of the content. In this article, we will introduce how to use CSSFlex layout to achieve equal height column layout. Below are specific code examples. HTML structure: &

How to implement irregular grid layout through CSSFlex elastic layout. In web design, it is often necessary to use grid layout to achieve page segmentation and layout. Usually grid layout is regular, and each grid is the same size. Sometimes we may need to implement some irregular grid layout. CSSFlex elastic layout is a powerful layout method that can easily implement various grid layouts, including irregular grid layouts. Below we will introduce how to use CSSFlex elastic layout to achieve different

A Guide to CSS Flexible Layout Properties: positionsticky and flexbox Flexible layout has become a very popular and useful technique in modern web design. It can help us create adaptive web page layouts so that web pages can display and respond well on different devices and screen sizes. This article will focus on two flexible layout properties: position:sticky and flexbox. We'll discuss their usage in detail, with concrete code examples

The display attribute in CSS controls the layout of elements on the web page. Its meaning: inline: elements are arranged inline, flowing with the text. block: Elements are arranged at a block level, occupying an exclusive row and occupying the width. inline-block: combines the inline and block features, arranges inline but can set the size. none: hide the element. Flex: Use flexible layout to automatically adjust the size and position of elements. grid: Use grid layout to precisely control element position and size.

HTML tutorial: How to use Flexbox for adaptive equal-height, equal-width, equal-spacing layout, specific code examples are required. Introduction: In modern web design, layout is a very critical factor. For pages that need to display a large amount of content, how to reasonably arrange the position and size of elements to achieve good visibility and ease of use is an important issue. Flexbox (flexible box layout) is a very powerful tool through which various flexible layout needs can be easily realized. This article will introduce Flexbox in detail
