响应式文字隐藏问题

存在问题布局

  • 问题:当浏览器窗口大小缩小的一定程度之后无法继续缩小,导致文字隐藏不生效,左侧文字会撑开盒子,导致盒子不能继续缩小
  • 此时仍然可以正常伸缩
    image.png
  • 无法正常伸缩
    image.png
  • 预期效果应该如下
    image.png
  • 代码如下
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .concainer {
      width: 40%;
      margin: 0 auto;
      background-color: green;
    }
    .box {
      display: flex;
      width: 100%;
    }
    .box1 {
      background-color: red;
      width: 100px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .box2 {
      background-color: blue;
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .page {
      display: flex;
    }
    .left {
      flex: 1;
      height: 500px;
      background-color: bisque;
    }
    .right {
      height: 500px;
      width: 33%;
      background-color: red;
    }
  </style>
  <body>
    <div class="page">
      <div class="left">
        左
        <div class="box">
          <div class="box1">11111111111111111111111111</div>
          <div class="box2">
            2222222222222222222222222222222222222222222222333333
          </div>
        </div>
      </div>
      <div class="right">右</div>
    </div>
  </body>
</html>

  • 解决办法
    • 左侧容器left添加min-width:一个较小值/添加overflo:hidden
    • 原因为:左侧为flex弹性布局导致内部内容可以撑开盒子,不同浏览器兼容不同,需要设置overflow:hidden或者添加一个min-width:2px (一个合适的值)
   .left {
     flex: 1;
     height: 500px;
     min-width: 20px;
     // or 设置overflow:hidden
     background-color: bisque;
   }

+++++++++++++++++++++++++++++后续

  • 其实根本原因还是使用了flex,只要使用了flex而不是一个固定宽度或者百分比宽度,那么就需要设置一个hidden,如果容器是多层嵌套,那么需要多个容器都hidden,不然 依然会撑开盒子
    文字隐藏.png
posted @ 2024-04-15 09:43  story.Write(z)  阅读(18)  评论(0)    收藏  举报