CSS flexbox:align-items和align-content

时间:2015-07-06 15:48:50

标签: css css3 flexbox

我发现CSS flexbox属性中align-itemsalign-content之间的区别非常混乱。我一直在网上查看文档和几个例子,但仍然无法完全掌握它。

更确切地说,align-items对我来说是完全合理的,并且它完全清楚它的行为方式。另一方面,align-content根本不清楚。特别是,我不明白为什么我们应该使用两种不同的属性,具体取决于内容是否适合一行或多行。

任何人都可以用外行的方式帮助解释吗?

谢谢!

2 个答案:

答案 0 :(得分:31)

6. Flex Lines中所述,

  Flex items中的{p> flex container已布局并对齐   在弹性线中,用于分组的假设容器和   通过布局算法进行对齐。 Flex容器也可以是   single-linemulti-line,具体取决于flex-wrap   属性

然后,您可以设置不同的路线:

  • justify-content属性适用于所有弹性容器,并设置弹性项目沿每条柔性线主轴的对齐方式。

      

    An illustration of the five justify-content keywords and their effects on a flex container with three colored items.

  • align-items属性适用于所有Flex容器,并设置Flex项目沿每条柔性线的交叉轴的默认对齐方式。 align-self适用于所有弹性项目,允许为各个弹性项目覆盖此默认对齐方式。

      

    An illustration of the five align-items keywords and their effects on a flex container with four colored items.

  • align-content属性仅适用于多线柔性容器,并且当横轴有额外空间时,会对齐柔性容器内的柔性线。

      

    An illustration of the align-content keywords and their effects on a multi-line flex container.

这里有一个片段:

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>

答案 1 :(得分:11)

首先,您需要了解 Sleep(1000); 结构的工作原理。下面的图片是Cheatsheet

的一部分

Flexbox结构

Flexbox structure

Flexbox的构建是为了适应行和列。

主轴:

当flex-direction为行时:x轴如图表所示,当flex-direction为列时:图表上的y轴

跨轴:

当flex-direction为列时:x轴如图表所示,当flex-direction为行时:y轴为图形

<强>对齐-内容

justify-content 用于对齐Flexible box上的项目。

&#13;
&#13;
main-axis
&#13;
.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;
}
&#13;
&#13;
&#13;

<强>对齐含量

align-content 用于沿<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>对齐flexbox内的项目。请注意,它适用于Multi-line containers

&#13;
&#13;
cross-axis
&#13;
.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;
}
&#13;
&#13;
&#13;

<强>对齐-项

align-items 具有与<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>相同的功能,但区别在于它可以使每个单行容器居中,而不是将整个容器居中。检查片段中的整个容器是否分成250px高度并居中,而在align-content中它位于行的500px高度的中心。

&#13;
&#13;
align-content
&#13;
.align-content {
  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;
}
&#13;
&#13;
&#13;