css 是否可以使flex列的子列的宽度等于其flex容器的宽度?

eaf3rand  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(117)

我试图使红色框与黑色框线中的flex容器宽度相同。

.container {
  display: flex;
  width: 80%;
  margin: auto;
  column-gap: 1em;
  outline: 1px solid black;
}

.a {
  background: #f002;
  flex: 3;
}
.b {
  background: #00f2;
  flex: 2;
}
.box {
  background: #f0f2;
  margin: 1em 0;
  padding: 1em;
}
.fw {
  background: red;
}
<div class="container">
  <div class="a">
    <div class="box">
      A
    </div>
    <div class="box fw">
      B
    </div>
    <div class="box">
      C
    </div>
  </div>
  <div class="b">
    <p>Some text here</p>
  </div>
</div>
oxf4rvwz

oxf4rvwz1#

这就是你在这个例子中可以做的。如果第一列和第二列之间的比例改变了,代码可能需要改变。

.container {
  display: flex;
  width: 80%;
  margin: auto;
  column-gap: 1em;
  outline: 1px solid black;
}

.a {
overflow: visible;
  background: #f002;
  flex: 3;
}
.b {
  background: #00f2;
  flex: 2;
}
.box {
  background: #f0f2;
  margin: 1em 0;
  padding: 1em;
}
.fw {
  width: calc(166.666666% - 1em);
  background: red;
}
<div class="container">
  <div class="a">
    <div class="box">
      A
    </div>
    <div class="box fw">
      B
    </div>
    <div class="box">
      C
    </div>
  </div>
  <div class="b">
    <p>Some text here</p>
  </div>
</div>

相关问题