


CSS flex layout properties: the difference between align-items and align-content
When using flex
for layout, I found that there are two attributes that seem to have similar functions: align-items
and CSS flex layout properties: the difference between align-items and align-content
. At first glance, They are used to define the cross axis of the elements in the flex container (the main axis is the direction defined by flex-deriction
, the default is row, then the cross axis is perpendicular to the main axis, which is the column. Otherwise, they interchange, and flex is basically The concept of alignment is as shown in the figure below), so what is the difference between them?
This article uses example code to study this (flex-direction defaults to horizontal direction, the environment is Google browser: version 72), which is mainly divided into three Part:
① Translate a good answer from stack overflow.
② Use your own code example to show the difference.
③ Summary.
Note: This article is limited to the case where the attribute value is center. Please try other attribute values yourself. [Learning video sharing: css video tutorial, web front-end]
##1. Answer on stack overflow (translation)
See the question for details: https://stackoverflow.com/questions/31250174/css-flexbox-difference-between-align-items-and-CSS flex layout properties: the difference between align-items and align-content- justfiy-content property can be applied to all flex containers. Its function is to set the alignment of flex items on the main axis. The effects of different values are as follows:
-
-
-
is only applicable to multi-line flex containers (that is, in flex containers This attribute only has an effect when the child item is more than one row). Its function is to treat the child item as a whole when the flex container has extra space on the cross axis (when the attribute value is: flex-start, flex-end, center ) to align. The effects of different values are as follows:
In fact, this statement is not very accurate (see the example in Section 2.3). Let’s verify it through the example code below.
2. Do it yourself
2.1 The case where the sub-item is a single row
Initial code (the code in the following examples omits the parts that are irrelevant to flex and remain unchanged,React is used here, so it is
className) as follows:
<div className='flex'> <div className='i1'>1</div> <div className='i2'>2</div> <div className='i3'>3</div> <div className='i4'>4</div> </div>corresponds CSS:
.flex { width: 500px; margin: 10px; text-align: center; border: 2px solid #9a9a9a; display: flex; /* 默认的flex-direction为row,则交叉轴方向为column,即垂直方向*/ } .flex div { width: 100px; margin: 5px; } .i1 { background-color: #ffb685; height: 130px; } .i2 { background-color: #fff7b1; height: 50px; width: 120px; } .i3 { background-color: #b1ffc8; height: 100px; } .i4 { background-color: #b1ccff; height: 60px; }The effect is as follows:
Conclusion: In all flex layouts, there are actually browser default properties here : align-items: normal; and CSS flex layout properties: the difference between align-items and align-content: normal;, the effect is top alignment.
2.1.1 The height of the flex container is not set
Initial state:.flex { display: flex; }The effect is as follows:
Conclusion: There is a default attribute align-items: normal;, the effect is top alignment.
- Set
align-items: center
##.flex { display: flex; align-items: center; }
The effect is as follows:
Conclusion
: You can see that the height of the container is the height of the tallest child, and all the children in a row are centered on the cross axis, which is the height of the child. The center line coincides with the center line of the flex cross axis.
- Set
-
CSS flex layout properties: the difference between align-items and align-content: center
##
.flex { display: flex; CSS flex layout properties: the difference between align-items and align-content: center; }
The effect is as follows:
Conclusion: You can see that there is no difference from the initial state, that is, when the flex container does not set a height and the child has only one row,
CSS flex layout properties: the difference between align-items and align-contentAttributes have no effect. 2.1.2 Set height of flex container
Initial state: .flex {
height: 500px; /* 给flex容器添加一个高度 */
display: flex;
}
The effect is as follows:
结论: 与flex容器不设置高度差不多,只是外层容器的高度增加而已。
设置
align-items : center
.flex { height: 500px; display: flex; align-items: center; }
效果如下所示:
结论:可以看到在一行的所有子项全都在交叉轴上居中对齐,与flex容器高度不设置时的效果一样(只不过此时高度最大的子项也居中对齐了)。
设置
CSS flex layout properties: the difference between align-items and align-content: center
.flex { display: flex; CSS flex layout properties: the difference between align-items and align-content: center; }
效果如下所示:
结论:可以看到,此时CSS flex layout properties: the difference between align-items and align-content: center;
并没有起作用,效果与CSS flex layout properties: the difference between align-items and align-content一样。
2.2 子项为多行的情况
CSS flex layout properties: the difference between align-items and align-content:
<div> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </div>
对应的CSS:
.flex { width: 500px; margin: 10px; border: 2px solid #9a9a9a; text-align: center; display: flex; flex-wrap: wrap; /* 使flex容器一行放不下子项换行*/ } .i5 { background-color: #c8b1ff; height: 40px; } .i6 { background-color: #ffb1e5; height: 80px; }
效果如下所示:
结论:同单行一样,这里也有浏览器默认的属性:align-items: normal; 和 CSS flex layout properties: the difference between align-items and align-content: normal;,效果为顶部对齐。
2.2.1 flex容器不设置高度
CSS flex layout properties: the difference between align-items and align-content:
.flex { display: flex; flex-wrap: wrap; }
效果如下所示:
结论:默认顶部对齐,每一行的高度为该行子项中高度最大的那个值。
设置
align-items : center
.flex { display: flex; flex-wrap: wrap; align-items: center; }
效果如下所示:
结论:可以看到各行的子项都在各自行上居中对齐(各行的高度由高度最高的子项决定,flex容器的高度为所有行的高度最高的子项高度之和)。
设置
CSS flex layout properties: the difference between align-items and align-content: center
.flex { display: flex; flex-wrap: wrap; CSS flex layout properties: the difference between align-items and align-content: center; }
效果如下所示:
结论:与CSS flex layout properties: the difference between align-items and align-content一样,CSS flex layout properties: the difference between align-items and align-content: center
并没有起作用,因为此时是以所有子项作为一个整体,而flex容器并没有指定高度(flex容器的高度即为子项整体的最大高度),所以flex容器在交叉轴上没有多余的空间,那么子项整体自然而然也就没有在交叉轴上对齐的说法了。
2.2.2 flex容器设置高度
CSS flex layout properties: the difference between align-items and align-content:
.flex { height: 500px; display: flex; flex-wrap: wrap; }
效果如下所示:
结论:由浏览器的默认值确定。
设置
align-items : center
.flex { height: 500px; display: flex; flex-wrap: wrap; align-items: center; }
效果如下所示:
结论:这里我们可以看出,子项分为2行,flex容器将交叉轴上的多余空间按行数平均分给每行,然后每行各自按自己所在的行居中对齐(此时的单行效果跟2.1.2中的例子1效果一样)
设置
CSS flex layout properties: the difference between align-items and align-content: center
.flex { height: 500px; display: flex; flex-wrap: wrap; CSS flex layout properties: the difference between align-items and align-content: center; }
效果如下所示:
结论:我们可以看到,在flex容器指定高度并且子项为多行时,CSS flex layout properties: the difference between align-items and align-content: center
是将子项作为一个整体,然后这个整体在flex容器的交叉轴上居中对齐的。
2.3 补充
以上为什么要区分flex容器是否有固定高度是因为有一种特殊的情况,即:当子项为单行,flex容器具有固定的高度并且设置了flex-wrap: wrap;
时,CSS flex layout properties: the difference between align-items and align-content: center;
对单行的子项也有作用。
<div className='flex'> <div className='i1'>1</div> <div className='i2'>2</div> <div className='i3'>3</div> <div className='i4'>4</div> </div>
.flex { height: 500px; display: flex; flex-wrap: wrap; CSS flex layout properties: the difference between align-items and align-content: center; }
效果如下所示:
结论:可以看到此时,CSS flex layout properties: the difference between align-items and align-content: center;
将单行的子项作为一个整体在交叉轴居中了。
3. 总结
如下表:
条件 | 属性(是否有效果) | ||
子项 | flex容器 | align-items | CSS flex layout properties: the difference between align-items and align-content |
单行 | 不指定高度 | 是 | 否 |
固定高度 | 是 | 否(但是有设置flex-wrap:wrap;时,有效果) | |
多行 | 不指定高度 | 是 | 否 |
固定高度 | 是 | 是 |
结论:从上表可知,对于align-items
和CSS flex layout properties: the difference between align-items and align-content
的区别,我们只需要记住以下两点,
align-items
属性是针对单独的每一个flex子项起作用,它的基本单位是每一个子项,在所有情况下都有效果(当然要看具体的属性值)。CSS flex layout properties: the difference between align-items and align-content
属性是将flex子项作为一个整体起作用,它的基本单位是子项构成的行,只在两种情况下有效果:①子项多行且flex容器高度固定 ②子项单行,flex容器高度固定且设置了flex-wrap:wrap;
这里有个flex布局的小教程,感兴趣的同学可以玩玩:http://flexboxfroggy.com/
注:这里的高度固定的意思实际上是让flex容器在交叉轴上有多余的空间。
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of CSS flex layout properties: the difference between align-items and align-content. For more information, please follow other related articles on the PHP Chinese website!

Since I first chimed in on the CSS4¹ thing, there's been tons of more discussion on it. I'm going to round up my favorite thoughts from others here. There is

Every time I start a new project, I organize the code I’m looking at into three types, or categories if you like. And I think these types can be applied to

I've been guilty of publicly bemoaning the complexity of HTTPS. In the past, I've purchased SSL certificates from third-party vendors and had trouble

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

If you haven’t worked with immutability in JavaScript before, you might find it easy to confuse it with assigning a variable to a new value, or reassignment.

It’s entirely possible to build custom checkboxes, radio buttons, and toggle switches these days, while staying semantic and accessible. We don’t even need a

There are special superset number characters that are sometimes perfect for footnotes. Here they are:

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

SublimeText3 Chinese version
Chinese version, very easy to use