我发现align-items和align-content在CSS Flexbox属性上的区别非常令人困惑。我已经在网上看了几个小时的文档和几个例子,但我仍然不能完全掌握它。
更准确地说,align-items对我来说是完全有意义的,它的行为方式也是完全清楚的。另一方面,align-content一点也不清楚。特别是,我不明白为什么我们要使用两个不同的属性,这取决于内容都适合一行还是多行。
用外行人的话来说,解释是什么?
发布于 2015-07-07 09:31:21
如6. Flex Lines中所述,
flex container中的
Flex items在flex lines中进行布局和对齐,这是通过布局算法用于分组和对齐的假想容器。flex容器可以是single-line或multi-line,具体取决于flex-wrap属性
然后,您可以设置不同的路线:
justify-content属性应用于所有flex容器,并设置flex项沿每条flex线的主轴的对齐方式。
align-items属性应用于所有flex容器,并设置flex项沿每条flex线的交叉轴的默认对齐方式。align-self适用于所有flex项,允许为单个flex项覆盖此默认对齐方式。
align-content属性仅适用于多行flex容器,并在交叉轴上有额外空间时在flex容器内对齐flex线。
这里有一个要播放的代码片段:
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>
发布于 2015-07-07 00:05:21
首先,您需要了解灵活的盒子结构是如何工作的。下图是Cheatsheet的一部分。
Flexbox Structure

Flexbox是为适应行和列而构建的。
主轴:
当flex方向为row时:图形上的x轴。当flex-direction为column时:图表上的y轴
交叉轴:
当flex-direction为column时:图形上的x轴。当flex-direction为row时:图表上的y轴
Justify-Content
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
align-content用于将flexbox内的项目沿十字轴对齐。请注意,它适用于
.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-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>
https://stackoverflow.com/questions/31250174
复制相似问题