如何清除flexbox项目?

时间:2017-03-27 19:11:06

标签: html css css3 flexbox

我需要清除弹性框布局项目,以便第一个项目为100%宽度,第二个项目以自己的行为中心为50%,第三个和第四个项目在同一行上为50%宽度。

清除在简单的块项目上正常工作但我找不到使用flex布局执行此操作的方法。

https://jsfiddle.net/tzx8qyjt/

.container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.item {
  background: tomato;
  padding: 5px;
  width: 100%;
  height: 150px;
  margin-top: 10px;
  margin-bottom: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}

.container.block {
  display: block;
}

.container.block .item {
  float: left;
  box-sizing: border-box;
}

.half {
  width: 50%;
}

.container .item.centered {
  float: none;
  clear: both;
  margin-left: auto;
  margin-right: auto;
}
<ul class="container">
  <li class="item">1</li>
  <li class="item half centered">2 Clear after me</li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>
<br />
<br />
<br />
<br />
<br />
<ul class="container block">
  <li class="item">1</li>
  <li class="item half centered">2 Clear after me</li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

3 个答案:

答案 0 :(得分:2)

由于清除浮动对于弹性项目不起作用,你可以这样做,你在所有项目上设置box-sizing: border-box(使填充包含在设置的宽度中),然后为你的2:nd ,你给它一个小的余量,这将迫使其余的新线

.container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.item {
  background: tomato;
  padding: 5px;
  width: 100%;
  height: 150px;
  margin-top: 10px;
  margin-bottom: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
  box-sizing: border-box;
}

.half {
  width: 50%;
}
.container .item.centered {
  margin: 0 10px;
}
<ul class="container">
  <li class="item">1</li>
  <li class="item half centered">2 Clear after me</li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

答案 1 :(得分:1)

弹性布局的关键是使用适当的宽度,flex-wrap: wrapbox-sizing: border-box

第一项获得100%宽度。这迫使后续项目下线。

第二,第三和第四项每个都有50%的宽度。

但每个项目也有水平填充。因此,第2项将迫使第3和第4项脱离该行。

对于第3项和第4项,如果我们将其框模型从box-sizing: content-box(默认值)调整为border-box,则填充将包含在宽度计算中,并且两个项目完全匹配在一行上。< / p>

.container {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  padding: 0;
  margin: 0;
  list-style: none;
}

li:nth-child(1) { flex: 0 0 100%;} /* flex-grow, flex-shrink, flex-basis */
li:nth-child(2) { flex: 0 0 50%; }
li:nth-child(3) { flex: 0 0 50%; box-sizing: border-box; }
li:nth-child(4) { flex: 0 0 50%; box-sizing: border-box; }

.item {
  background: tomato;
  padding: 5px;
  height: 150px;
  margin-top: 10px;
  margin-bottom: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="container">
  <li class="item">1</li>
  <li class="item half centered">2 Clear after me</li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

答案 2 :(得分:1)

我希望它对你有用。我正在使用此代码。

    .clear {
      width: 100%;
      border: none;
      margin: 0;
      padding: 0;
    }

<ul class="container">
  <li class="item">1</li>
  <li class="item half">2 Clear after me</li>
  <li class='clear'><li>
  <li class="item half">3</li>
  <li class="item half">4</li>
</ul>

我们通常在流程布局中使用的内容。

.clear {
  border: 0;
  clear: both;
  height: 0;
}
相关问题