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]
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>
.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; }
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.
.flex { display: flex; }
Conclusion: There is a default attribute align-items: normal;, the effect is top alignment.
align-items: center
.flex { display: flex; align-items: center; }
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.
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; }
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
.flex { height: 500px; /* 给flex容器添加一个高度 */ display: flex; }
结论: 与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 className='flex'> <div className='i1'>1</div> <div className='i2'>2</div> <div className='i3'>3</div> <div className='i4'>4</div> <div className='i5'>5</div> <div className='i6'>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;,效果为顶部对齐。
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容器在交叉轴上没有多余的空间,那么子项整体自然而然也就没有在交叉轴上对齐的说法了。
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;
将单行的子项作为一个整体在交叉轴居中了。
如下表:
条件 | 属性(是否有效果) | ||
子项 | 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!