Flexbox中的第一个孩子全宽

Saj*_*jad 54 css flexbox

如何在flexbox全宽中设置first-child,将其他所有子设置为flex:1(对于拆分空间)?

像这样:

在此输入图像描述

The*_*XxE 78

您可以将:first-child宽度设置为100%,将其余的子设置:not(:first-child)flex: 1.要将它们放在多行上,请flex-wrap: wrap在容器上使用:

.container {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  background: #e2eaf4;
  padding: 10px;
}

.child {
  display: inline-block;
  font-family: "Open Sans", Arial;
  font-size: 20px;
  color: #FFF;
  text-align: center;
  background: #3794fe;
  border-radius: 6px;
  padding: 20px;
  margin: 12px;
}

.child:first-child {
  width: 100%;
}

.child:not(:first-child) {
  flex: 1;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="child">Child</div>
  <div class="child">Child</div>
  <div class="child">Child</div>
</div>
Run Code Online (Sandbox Code Playgroud)


Vad*_*kov 14

添加width: 100%;第一个项目.而flex: 1;对于其他人.

.flex {
  display: flex;
  flex-wrap: wrap;
}

.flex-item:not(:first-child) {
  flex: 1;
}

.flex-item:nth-child(1) {
  width: 100%;
}

/* Styles just for demo */
.flex-item {
  background-color: #3794fe;
  margin: 10px;
  color: #fff;
  padding: 20px;
  border-radius: 5px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex">
  <div class="flex-item">
    Child
  </div>
  <div class="flex-item">
    Child
  </div>
  <div class="flex-item">
    Child
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


vas*_*ort 6

另一种利用该flex-basis选项的解决方案(使用此格式时的第三个值flex: 1 1 100%)。

flex-basis 的 MDN 链接

.flexContainer {
  display: flex;
  flex-wrap: wrap;
}

.item:first-child {
  flex: 1 1 100%;
  margin-bottom: 20px;
}

.item {
  flex: 1 1 40%;
  height: 50px;
  background-color: red;
  margin: 0 20px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flexContainer">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
Run Code Online (Sandbox Code Playgroud)