CSS Flexbox:align-items 和 align-content 的区别
CSS Flexbox: difference between align-items and align-content
我发现 align-items
和 align-content
在 CSS Flexbox 属性中的区别非常令人困惑。我已经看了几个小时的文档和几个在线示例,但我仍然不能完全理解它。
更准确地说,align-items
对我来说完全有意义,而且它的行为方式完全清楚。另一方面,align-content
一点也不清楚。特别是,我不明白为什么我们应该根据内容是否适合一行或多行来使用两个不同的属性。
通俗的解释是什么?
首先您需要了解灵活框结构是如何工作的。下图是 Cheatsheet.
的一部分
Flexbox 结构
Flexbox 被构建为适应行和列。
主轴:
当 flex-direction 为 row 时:图表上的 x 轴。当 flex-direction 为 column 时:图形上的 y 轴
横轴:
当 flex-direction 为 column 时:图表上的 x 轴。当 flex-direction 为 row 时:图形上的 y 轴
调整内容
justify-content
用于将项目与 主轴.
对齐
.justify-content {
display: flex;
justify-content: center;
flex-wrap: wrap;
height: 500px;
width: 500px;
background: lightgray;
padding: 5px;
}
.box {
background: tomato;
width: 100px;
height: 100px;
margin: 10px;
}
<div class="justify-content">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
对齐内容
align-content
用于沿 横轴 对齐 flexbox 内的项目。请注意,它适用于 Multi-line containers
.align-content {
display: flex;
align-content: center;
flex-wrap: wrap;
height: 500px;
width: 500px;
background: lightgray;
padding: 5px;
}
.box {
background: tomato;
width: 100px;
height: 100px;
margin: 10px;
}
<div class="align-content">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
对齐项
align-items
与 align-content
具有相同的功能,但不同之处在于它使每个单行容器居中而不是使整个容器居中。检查在代码片段中,整个容器被分成 250 像素高度,并在其中居中,而在 align-content
中,它在行的 500 像素高度范围内居中。
.align-items {
display: flex;
align-items: center;
flex-wrap: wrap;
height: 500px;
width: 500px;
background: lightgray;
padding: 5px;
}
.box {
background: tomato;
width: 100px;
height: 100px;
margin: 10px;
}
<div class="align-items">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
所述
Flex items in a flex container are laid out and aligned
within flex lines, hypothetical containers used for grouping and
alignment by the layout algorithm. A flex container can be either
single-line or multi-line, depending on the flex-wrap
property
然后,您可以设置不同的对齐方式:
justify-content
属性适用于所有弹性容器,并设置弹性项目沿每条弹性线的主轴对齐。
align-items
property applies to all flex containers, and sets the default alignment of the flex items along the cross axis of each flex line. The align-self
适用于所有弹性项目,允许为单个弹性项目覆盖此默认对齐方式。
align-content
属性只适用于multi-line弹性容器,当有额外的space时对齐弹性容器内的弹性行在 cross-axis.
这里有一个片段可以播放:
var form = document.forms[0],
flex = document.getElementById('flex');
form.addEventListener('change', function() {
flex.style.flexDirection = form.elements.fd.value;
flex.style.justifyContent = form.elements.jc.value;
flex.style.alignItems = form.elements.ai.value;
flex.style.alignContent = form.elements.ac.value;
});
ul {
display: flex;
flex-flow: row wrap;
padding: 0;
list-style: none;
}
li {
padding: 0 15px;
}
label {
display: block;
}
#flex {
display: flex;
flex-wrap: wrap;
height: 240px;
width: 240px;
border: 1px solid #000;
background: yellow;
}
#flex > div {
min-width: 60px;
min-height: 60px;
border: 1px solid #000;
background: blue;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
}
#flex > .big {
font-size: 1.5em;
min-width: 70px;
min-height: 70px;
}
<form>
<ul>
<li>flex-direction
<label><input type="radio" name="fd" value="row" checked /> row</label>
<label><input type="radio" name="fd" value="row-reverse" /> row-reverse</label>
<label><input type="radio" name="fd" value="column" /> column</label>
<label><input type="radio" name="fd" value="column-reverse" /> column-reverse</label>
</li>
<li>justify-content
<label><input type="radio" name="jc" value="flex-start" checked /> flex-start</label>
<label><input type="radio" name="jc" value="flex-end" /> flex-end</label>
<label><input type="radio" name="jc" value="center" /> center</label>
<label><input type="radio" name="jc" value="space-between" /> space-between</label>
<label><input type="radio" name="jc" value="space-around" /> space-around</label>
</li>
<li>align-items
<label><input type="radio" name="ai" value="flex-start" /> flex-start</label>
<label><input type="radio" name="ai" value="flex-end" /> flex-end</label>
<label><input type="radio" name="ai" value="center" /> center</label>
<label><input type="radio" name="ai" value="baseline" /> baseline</label>
<label><input type="radio" name="ai" value="stretch" checked /> stretch</label>
</li>
<li>align-content
<label><input type="radio" name="ac" value="flex-start" /> flex-start</label>
<label><input type="radio" name="ac" value="flex-end" /> flex-end</label>
<label><input type="radio" name="ac" value="center" /> center</label>
<label><input type="radio" name="ac" value="space-between" /> space-between</label>
<label><input type="radio" name="ac" value="space-around" /> space-around</label>
<label><input type="radio" name="ac" value="stretch" checked /> stretch</label>
</li>
</ul>
</form>
<div id="flex">
<div>1</div>
<div class="big">2</div>
<div>3</div>
<div>4</div>
<div class="big">5</div>
<div>6</div>
</div>
我发现 align-items
和 align-content
在 CSS Flexbox 属性中的区别非常令人困惑。我已经看了几个小时的文档和几个在线示例,但我仍然不能完全理解它。
更准确地说,align-items
对我来说完全有意义,而且它的行为方式完全清楚。另一方面,align-content
一点也不清楚。特别是,我不明白为什么我们应该根据内容是否适合一行或多行来使用两个不同的属性。
通俗的解释是什么?
首先您需要了解灵活框结构是如何工作的。下图是 Cheatsheet.
的一部分Flexbox 结构
Flexbox 被构建为适应行和列。
主轴:
当 flex-direction 为 row 时:图表上的 x 轴。当 flex-direction 为 column 时:图形上的 y 轴
横轴:
当 flex-direction 为 column 时:图表上的 x 轴。当 flex-direction 为 row 时:图形上的 y 轴
调整内容
justify-content
用于将项目与 主轴.
.justify-content {
display: flex;
justify-content: center;
flex-wrap: wrap;
height: 500px;
width: 500px;
background: lightgray;
padding: 5px;
}
.box {
background: tomato;
width: 100px;
height: 100px;
margin: 10px;
}
<div class="justify-content">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
对齐内容
align-content
用于沿 横轴 对齐 flexbox 内的项目。请注意,它适用于 Multi-line containers
.align-content {
display: flex;
align-content: center;
flex-wrap: wrap;
height: 500px;
width: 500px;
background: lightgray;
padding: 5px;
}
.box {
background: tomato;
width: 100px;
height: 100px;
margin: 10px;
}
<div class="align-content">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
对齐项
align-items
与 align-content
具有相同的功能,但不同之处在于它使每个单行容器居中而不是使整个容器居中。检查在代码片段中,整个容器被分成 250 像素高度,并在其中居中,而在 align-content
中,它在行的 500 像素高度范围内居中。
.align-items {
display: flex;
align-items: center;
flex-wrap: wrap;
height: 500px;
width: 500px;
background: lightgray;
padding: 5px;
}
.box {
background: tomato;
width: 100px;
height: 100px;
margin: 10px;
}
<div class="align-items">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
Flex items in a flex container are laid out and aligned within flex lines, hypothetical containers used for grouping and alignment by the layout algorithm. A flex container can be either single-line or multi-line, depending on the flex-wrap property
然后,您可以设置不同的对齐方式:
justify-content
属性适用于所有弹性容器,并设置弹性项目沿每条弹性线的主轴对齐。align-items
property applies to all flex containers, and sets the default alignment of the flex items along the cross axis of each flex line. Thealign-self
适用于所有弹性项目,允许为单个弹性项目覆盖此默认对齐方式。align-content
属性只适用于multi-line弹性容器,当有额外的space时对齐弹性容器内的弹性行在 cross-axis.
这里有一个片段可以播放:
var form = document.forms[0],
flex = document.getElementById('flex');
form.addEventListener('change', function() {
flex.style.flexDirection = form.elements.fd.value;
flex.style.justifyContent = form.elements.jc.value;
flex.style.alignItems = form.elements.ai.value;
flex.style.alignContent = form.elements.ac.value;
});
ul {
display: flex;
flex-flow: row wrap;
padding: 0;
list-style: none;
}
li {
padding: 0 15px;
}
label {
display: block;
}
#flex {
display: flex;
flex-wrap: wrap;
height: 240px;
width: 240px;
border: 1px solid #000;
background: yellow;
}
#flex > div {
min-width: 60px;
min-height: 60px;
border: 1px solid #000;
background: blue;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
}
#flex > .big {
font-size: 1.5em;
min-width: 70px;
min-height: 70px;
}
<form>
<ul>
<li>flex-direction
<label><input type="radio" name="fd" value="row" checked /> row</label>
<label><input type="radio" name="fd" value="row-reverse" /> row-reverse</label>
<label><input type="radio" name="fd" value="column" /> column</label>
<label><input type="radio" name="fd" value="column-reverse" /> column-reverse</label>
</li>
<li>justify-content
<label><input type="radio" name="jc" value="flex-start" checked /> flex-start</label>
<label><input type="radio" name="jc" value="flex-end" /> flex-end</label>
<label><input type="radio" name="jc" value="center" /> center</label>
<label><input type="radio" name="jc" value="space-between" /> space-between</label>
<label><input type="radio" name="jc" value="space-around" /> space-around</label>
</li>
<li>align-items
<label><input type="radio" name="ai" value="flex-start" /> flex-start</label>
<label><input type="radio" name="ai" value="flex-end" /> flex-end</label>
<label><input type="radio" name="ai" value="center" /> center</label>
<label><input type="radio" name="ai" value="baseline" /> baseline</label>
<label><input type="radio" name="ai" value="stretch" checked /> stretch</label>
</li>
<li>align-content
<label><input type="radio" name="ac" value="flex-start" /> flex-start</label>
<label><input type="radio" name="ac" value="flex-end" /> flex-end</label>
<label><input type="radio" name="ac" value="center" /> center</label>
<label><input type="radio" name="ac" value="space-between" /> space-between</label>
<label><input type="radio" name="ac" value="space-around" /> space-around</label>
<label><input type="radio" name="ac" value="stretch" checked /> stretch</label>
</li>
</ul>
</form>
<div id="flex">
<div>1</div>
<div class="big">2</div>
<div>3</div>
<div>4</div>
<div class="big">5</div>
<div>6</div>
</div>