Flexbox和包装物品的对齐方式

时间:2017-03-13 05:49:48

标签: html css flexbox

我有一个带有包装物品的弹箱:



.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  border: 1px solid silver;
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
  display: flex;
  width: 520px;
  height: 520px;
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
}

.flex-item {
  background: gold;
  padding: 5px;
  width: 100px;
  height: 100px;
  margin: 10px;
  line-height: 100px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}

<ul class="flex-container wrap">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
  <li class="flex-item">7</li>
  <li class="flex-item">8</li>
</ul>
&#13;
&#13;
&#13;

https://jsfiddle.net/0v54s2Lz/1/

我希望5,6,7和8在1,2,3和4之下。但是存在差距。据推测,Flexbox试图平衡&#34;行&#34;高度。

2 个答案:

答案 0 :(得分:2)

你必须添加

align-content: flex-start;

到.flex-container。

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  border: 1px solid silver;
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
  display: flex;
  width: 520px;
  height: 520px;
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
  align-content: flex-start;
}

.flex-item {
  background: gold;
  padding: 5px;
  width: 100px;
  height: 100px;
  margin: 10px;
  
  line-height: 100px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}
<ul class="flex-container wrap">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
  <li class="flex-item">7</li>
  <li class="flex-item">8</li>
</ul>

https://jsfiddle.net/7LeL9mcd/

答案 1 :(得分:-1)

height: 520px;移除.flex-container,此处已更新fiddle