解密Flex布局:为什么flex:1仍会导致内容溢出

前言

今天在解决别的bug时发现了个很奇怪的东西,我的容器明明设置了flex:1为什么还会导致内容溢出,导致比我设想的flex:1尺寸来得大。

后来才知道

浏览器默认为flex容器的子元素设置了 min-width: auto;min-height: auto,这意味着子元素的最小宽度和高度不能小于其内容的宽度和高度。

这就是为什么明明是flex:1,但是为什么比预想的要大了。

正文

单单文字说明还是难以理解,来看图和代码吧

有问题的代码,父元素设置100vw,子元素设置flex:1;

tsx 复制代码
function App() {
  return (
    <div
      style={{
        display: "flex",
        height: "100vh",
        width: "100vw",
        gap: "10px",
      }}
    >
      <div
        style={{
          width: "200px",
          textAlign: "left",
          backgroundColor: "red",
        }}
      >
        Left
      </div>
      <div
        style={{
          flex: 1,
          textAlign: "left",
          backgroundColor: "blue",
        }}
      >
        <section
          style={{
            height: "100%",
            width: "100%",
            overflow: "auto",
            backgroundColor: "lightcyan",
          }}
        >
          <div>
            <div>asdasd</div>
            <div>
              asdasdasdasdasdsadsadasdsadasdiuoasdgiasgdfasghdiopasdyiuskafdgiuashdasgvdsadpjnasildgsaodiasoudgasiugdasyfdgasudiyfasdhoiashdgiouasgdiu
            </div>
            <div>asdasd</div>
          </div>
        </section>
      </div>
    </div>
  );
}

预计是滚动条只在右边区域,总页面没有滚动条。但是实际的总页面有滚动条,右边区域没有滚动条。这就是 浏览器默认为flex容器的子元素设置了 min-width: auto;min-height: auto,这意味着子元素的最小宽度和高度不能小于其内容的宽度和高度,把右侧的内容撑大了,导致内容溢出了。

如何解决呢

  1. 设置 overflow,利用BFC
  2. 设置 min-width: 0

任意使用一种都可以,是我们预计的情况了。

修改后代码

tsx 复制代码
function App() {
  return (
    <div
      style={{
        display: "flex",
        height: "100vh",
        width: "100vw",
        gap: "10px",
      }}
    >
      <div
        style={{
          width: "200px",
          textAlign: "left",
          backgroundColor: "red",
        }}
      >
        Left
      </div>
      <div
        style={{
          flex: 1,
          textAlign: "left",
          backgroundColor: "blue",
          minWidth: 0, //关键
        }}
      >
        <section
          style={{
            height: "100%",
            width: "100%",
            overflow: "auto",
            backgroundColor: "lightcyan",
          }}
        >
          <div>
            <div>asdasd</div>
            <div>
              asdasdasdasdasdsadsadasdsadasdiuoasdgiasgdfasghdiopasdyiuskafdgiuashdasgvdsadpjnasildgsaodiasoudgasiugdasyfdgasudiyfasdhoiashdgiouasgdiu
            </div>
            <div>asdasd</div>
          </div>
        </section>
      </div>
    </div>
  );
}

结语

有兴趣的可以去试试

相关推荐
weixin_427771618 小时前
前端调试隐藏元素
前端
爱上好庆祝9 小时前
学习js的第五天
前端·css·学习·html·css3·js
C澒9 小时前
IntelliPro 产研协作平台:基于 AI Agent 的低代码智能化配置方案设计与实现
前端·低代码·ai编程
一袋米扛几楼989 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
网络点点滴10 小时前
前端与后端的区别与联系
前端
EnCi Zheng10 小时前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python
kyriewen10 小时前
你的网页慢,用户不说直接走——前端性能监控教你“读心术”
前端·性能优化·监控
广州华水科技10 小时前
北斗GNSS变形监测在大坝安全监测中的应用与优势分析
前端
前端老石人10 小时前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
CAE虚拟与现实10 小时前
五一假期闲来无事,来个前段、后端的说明吧
前端·后端·vtk·three.js·前后端