Skip to content

Flexbox 内容 Overflow

快速摘要

你的 flex 项是 shrinking 也 much or overflowing 容器因为 flex defaults cause unexpected sizing behavior.

为什么发生

  • flex-shrink defaults 到 1, causing 项到 shrink below 它们的内容大小

  • 长文本 or URLs 不使用 overflow handling break 布局

  • 最小值-宽度 defaults 到 auto in flexbox, preventing shrinking below 内容大小

最小示例

✗ 错误代码
.container { display: flex; }
.item {
  /* flex-shrink: 1 by default */
  /* min-width: auto prevents shrinking below content */
  /* Long text overflows the container */
}
✓ 修复代码
.container { display: flex; }
.item {
  min-width: 0; /* Allow shrinking below content size */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

设置最小值-宽度: 0 overrides 默认 auto 值, 允许 flex 项到 shrink. overflow: 隐藏 and 文本-overflow: ellipsis handle 长文本 gracefully.

如何诊断

  • Inspect flex 项 in DevTools — 检查它们的 computed 宽度

  • Look for 内容那个是 wider than available 空间

  • 检查 flex-shrink and 最小值-宽度值 on 项

如何修复

  • 设置最小值-宽度: 0 on flex 项到 允许它们到 shrink below 内容大小

  • 添加 overflow: 隐藏 and 文本-overflow: ellipsis for 长文本

  • 设置 flex-shrink: 0 on 项那个应该不 shrink

  • Use flex: 0 0 auto for fixed-大小项

如何预防

  • 设置最小值-宽度: 0 or 最小值-高度: 0 on flex 项那个包含长 内容

  • Use word-break: break-word or overflow-wrap: break-word for 文本

相关资源

相关术语

相关课程

← 返回语言