html - 文本未包装在 flexbox 中

标签 html css flexbox word-wrap

<分区>

谁能告诉我为什么行方向 flex 容器中的 div 中的文本没有正确换行但会溢出?

如果我将方向更改为 section,文本将换行,我不明白为什么...

html,
body {
  height: 100%;
}

body {
  overflow-y: scroll;
  margin: 0;
}

div,
main,
section {
  align-items: stretch;
  display: flex;
  flex-shrink: 0;
  flex-direction: column;
  flex-wrap: nowrap;
  position: relative;
}

main {
  flex-grow: 1;
}

section {
  flex-flow: row nowrap;
  flex-grow: 1;
  justify-content: center;
  margin: 0 auto;
  max-width: 1280px;
  padding: 60px 0;
}

.content {
  flex-grow: 1;
  justify-content: start;
}
<body>
  <main>
    <section>
      <div class="content">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eget nulla sagittis sem egestas molestie. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
      </div>
    </section>
  </main>
</body>

最佳答案

让我们澄清一下结构:

  1. 您的 mainsectiondiv 元素是 flex 容器。
  2. sectionmain 中的 flex 元素。
  3. divsection 中的 flex 元素。
  4. main 不是 flex 元素,因为它的父级(body)不是 flex 容器。
  5. 所有 flex 元素都设置为 flex-shrink: 0

布局如下:

  • 注意:问题中的大部分代码都不是重现问题所必需的,因此我将其删除。
  • 注意:每个 flex 容器都已突出显示。

div,
main,
section {
  display: flex;
  flex-shrink: 0;
}

section {
  padding: 60px 0;
}

main    { background-color: lightgreen; }
section { border: 2px dashed red; }
div     { border: 1px dashed black; }
body    { margin: 0; }
<main>
  <section>
    <div class="content">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eget nulla sagittis sem egestas molestie. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
    </div>
  </section>
</main>

这是发生了什么:

  1. flex-shrink: 0div(黑色边框)上防止它收缩。
  2. flex-shrink: 0div 上展开它的父元素,section 元素(红色边框)。
  3. flex-shrink: 0section 上防止它收缩。
  4. flex-shrink: 0section扩展其父元素,main 元素(绿色背景), 因为 main 不是 flex 元素并且 flex-shrink 不适用。
  5. main 是标准的 block 级元素(即, block 格式化上下文中的元素)和 the default overflow: visible适用。
  6. 如果将 body 元素设为 flex 容器,则 main 元素变为 flex 元素,指定 flex-shrink: 0适用,main 也基于其后代进行扩展。

所以解决方案是启用收缩

div,
main,
section {
  display: flex;
  flex-shrink: 1; /* adjusted */
}

section {
  padding: 60px 0;
}

main    { background-color: lightgreen; }
section { border: 2px dashed red; }
div     { border: 1px dashed black; }
body    { margin: 0; }
<main>
  <section>
    <div class="content">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum eget nulla sagittis sem egestas molestie. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
    </div>
  </section>
</main>

关于html - 文本未包装在 flexbox 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48583312/

相关文章:

javascript - Android、WebView 和 SpeechRecognition-API

html - 无法让文本在两个 css 水平框中对齐

javascript - localStorage 在刷新之前返回未定义?

html - 使用 <li> 和 <a> 同步悬停状态?

javascript - 表格悬停边框颜色删除列线

html - 在 2 列或更多列中从上到下、从左到右堆叠可变高度、固定宽度的 div

javascript - 如何用末尾没有空白的div水平填充页面

css - 指定 Div 的优先级

html - Css 定位按钮位于 Overlay 的中心,背景为图像

html - 使用 Flexbox 在 ul 中将 4 张图像呈现为 2x2 网格