html - 为什么显示: flex push the following element onto a new line?

标签 html css flexbox

在我的代码中,第一个示例运行良好。第二个将第二个元素推到一个新行。

为什么?以及如何避免?

see this codepen

.one {
  background-color: cornflowerblue;
  width: 50%;
}
.one,
.two {
  height: 100%;
  font-size: 30px;
}
.two {
  width: 50%;
  background-color: salmon;
}
.ib {
  display: inline-block;
}
.container {
  font-size: 0;
  height: 200px;
}
.fb {
  display: flex;
}
<div class="container">
  <div class="one ib">one</div>
  <div class="two ib">two</div>
</div>

<div class="container">
  <div class="one fb">one</div>
  <div class="two ib">two</div>
</div>

最佳答案

您有三个 div,它们的 display 值设置为 inline-block。这些 div 中的每一个都有一个类 ib

你有一个带有 display: flex 的 div。这个 div 有一个类 fb

当您将 display: flex 应用到一个元素时,它变成了一个 block 级容器。作为 block 级元素,它会占用行中的所有可用空间,将任何后续元素推到下一行。

解决这个问题的方法是使用display: inline-flex代替display: flex

Revised Codepen

来自规范:

3. Flex Containers: the flex and inline-flex display values

flex

This value causes an element to generate a block-level flex container box.

inline-flex

This value causes an element to generate an inline-level flex container box.

关于html - 为什么显示: flex push the following element onto a new line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070828/

相关文章:

javascript - 3 个 DIV 高度相同。使用窗口缩小最左边的宽度和所有高度

html - 我怎样才能让这个页脚最终做我想做的事?会喜欢一些建议

css - 移动一个 div 会在原来的位置留下一个空间?

css - 在右侧显示嵌套组件,在左侧显示父组件

android - 多行 React-Native Text 组件的宽度不会调整为 Android 上最长文本行的长度

jquery - 使用 CSS 或 jQuery 仅水平扩展容器以防止垂直溢出

html - 用花车而不是 table 来排列东西?

javascript - 将半径 slider 添加到 Google map JavaScript API

css - Drupal 站点上的 IE 问题 CCS div

html - 为什么证明内容 : right only work in firefox?