html - 设置 flexbox 元素的初始宽度

标签 html css flexbox

<分区>

我在使用 flexbox 时遇到问题,我试图让我的第一个 .cell1 接受其中 3 个元素的宽度,所以它应该是 300px 宽,所以我设置了这到 flex: 0 0 auto 并且我已经将 .cell2 设置为 flex: 1 所以它占用了剩余的空间但不是这样做它似乎与 .cell1 重叠,我不确定为什么?

如何让 .cell1 占据其中 3 个元素的宽度,并让 .cell2 占据剩余的宽度?

.wrap {
  display:flex;
  flex-wrap:wrap;
  width: 100%;
}

.cell1{
  display:flex;
  flex: 0 0 auto;
  flex-direction: row;
}
.cell2{
  display: flex;
  flex: 1;
  background: #ff0000;
}
.item1{
  flex: 0 0 100px;
  background: #ff0000;
}

.item2{
  flex: 0 0 100px;
  background: #0000ff;
}

.item3{
  flex: 0 0 100px;
  background: #ff00ff;
}
<div class="wrap">
  <div class="cell1">
    <div class="item1">
    item 1
    </div>
    <div class="item2">
    item 2
    </div>
    <div class="item3">
    item 3
    </div>
  </div>

  <div class="cell2">
  cell2
  </div>
</div>

最佳答案

使用 width 而不是 flex-basis。这是一个已知错误,您可以在此处阅读更多信息:

What are the differences between flex-basis and width? (在@Michael_B 接受的答案的末尾)

Bug affecting all major browsers, except IE 11 & Edge:

flex-basis ignored in a nested flex container. width works. A problem has been found when sizing flex items in a nested flex container.

.wrap {
  display:flex;
  flex-wrap:wrap;
  width: 100%;
}

.cell1{
  display:flex;
  flex-direction: row;
}
.cell2{
  display: flex;
  flex: 1;
  background: #ff0000;
}
.item1{
  width:100px;
  background: #ff0000;
}

.item2{
  width:100px;
  background: #0000ff;
}

.item3{
  width:100px;
  background: #ff00ff;
}
<div class="wrap">
  <div class="cell1">
    <div class="item1">
    item 1
    </div>
    <div class="item2">
    item 2
    </div>
    <div class="item3">
    item 3
    </div>
  </div>

  <div class="cell2">
  cell2
  </div>
</div>

关于html - 设置 flexbox 元素的初始宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49066012/

相关文章:

html - 如何使用 CSS 去除图像下方多余的空白区域

html - 垂直对齐两个div,有偏移量?

html - 让 % 为 flex 元素的子元素正常工作

html - flex 元素不会拉伸(stretch)容器

javascript - DOJO 对话框第二次打不开

html - CSS Flexbox 导航和边距

html - 使用 flexbox 水平对齐图像,保持宽高比,并在居中时缩放到移动设备

html - 试图制作一个包含文本的 slider

html - 体外背景

javascript - 是否有验证器来检查运算符是否已被使用?