html - 为什么宽度为 :100% not taking the parent's width? 的元素

标签 html css flexbox

我面临的问题是我无法在 flexbox 元素中设置与其父项相同的宽度。

这是代码,theSpan 类的跨度与其父类的width 不同。

.container {
  display: flex;
}
.item1 {
  flex: 1 1 200px;
  border: 5px solid yellow;
}
.item2 {
  flex: 1 1 200px;
  border: 5px solid blue;
}
.item3 {
  flex: 1 1 200px;
  border: 5px solid red;
}
.theSpan {
  width: 100%;
  border: 2px solid black;
}
<div class='container'>
  <div class='item1'>
    <span class='theSpan'>abcdefg</span>
  </div>
  <div class='item2'>
    <span>1</span>
  </div>
  <div class='item3'>
    <span>2</span>
  </div>
</div>

最佳答案

您的 span 元素具有 theSpan 类是 flex 元素 (.item1) 的子元素。

这个 flex 元素不是 flex 容器。

因为 only the children of a flex container participate in flex layout , 跨度(孙子)被取消资格。它不存在于 flex formatting context 中.

跨度存在于标准block formatting context .

在 BFC 中,span 默认是一个内联的、不可替换的元素。

规范中提供了 width: 100% 不起作用的原因:

10.2 Content width: the width property

This property does not apply to non-replaced inline elements. The content width of a non-replaced inline element's boxes is that of the rendered content within them.

10.3.1 Inline, non-replaced elements

The width property does not apply.

关于html - 为什么宽度为 :100% not taking the parent's width? 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577892/

相关文章:

html - 使用 Ruby on Rails 编写的网页不会显示标题文本的更改

jquery - 我需要将表格行数据拖放到另一个表格,但无法实现

jquery - 可调整的 div 大小

css - 使 flexbox 列使用可用宽度

css - Flexbox 布局中的滚动面板

html - 使我的元素出现在垂直对齐的行中

javascript - 表单提交在 safari 中不起作用,但在所有其他浏览器中起作用?

html - 每页菜单相同

CSS 不适用于除 IE 以外的其他浏览器

javascript - 设置多个 style.background 值