html - flexbox 内的文本正在换行?

标签 html css flexbox

JS Fiddle

我有两个父盒并排放置,带有 flex 盒,在它们内部我有另一个 flex 盒,因此 h2p 可以并排放置。

我没有设置父框的宽度,理想情况下我也不想设置。

为什么当每个 flexbox 都有扩展空间时我的文本内容会换行?在示例中,标题在 Is 之后换行 - 为什么 h2 不能展开,所以 This Is Title A 全部出现在一行中?

.container {
  background: grey;
  display: flex;
}

.container>div {
  align-items: center;
  display: flex;
}

h2 {
  flex-basis: 40%;
}

p {
  flex-basis: 60%;
}
<div class="container">
  <div>
    <h2>This Is Title A</h2>
    <p>Content.</p>
  </div>
  <div>
    <h2>This Is Title B</h2>
    <p>Content.</p>
  </div>
</div>

最佳答案

问题是百分比长度使用父容器作为引用,而您的代码中缺少这一点。 h2 的父级和 p元素没有定义的宽度。

一旦您在父级上定义了宽度(我知道您不想这样做),问题就解决了。否则,使用另一个长度单位,white-space: nowrap ,或其他一些妥协。

10.2 Content width: the width property

<percentage>

Specifies a percentage width. The percentage is calculated with respect to the width of the generated box's containing block. If the containing block's width depends on this element's width, then the resulting layout is undefined in CSS 2.2 (and 2.1).

关于html - flexbox 内的文本正在换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990008/

相关文章:

html - 导航栏文本向后对齐 (CSS)

reactjs - 打开子页面(弹出页面)时如何禁用滚动父组件? - CSS

javascript - 如何创建一组依赖选择列表,其中连续列表取决于前面列表选项的选择?

html - 在 flex 元素换行时移除边距

javascript - 将表格数据转换为数组(格式化)

html - 将 CSS 放入正文并维护有效的 HTML5 代码?

javascript - 我需要重复多少 javascript jquery 代码?

php - 内容离开页面与页脚重叠

html - 使用不同行上的内容控制 Flexbox 列宽

html - Firefox 中的 Flexbox 溢出问题