关于前端页面设置省略号ellipsis和flex同时使用会无效的问题

问题:当内容超过宽度时,使用省略号...代替溢出的内容,并内容居中显示

使用css的text-overflow: ellipsis属性,可设置省略号,需要注意的是: 如果设置了display: flex,则换行无效。. 一定要设置宽度

 

方式一:不确定宽度居中,父级使用display: flex,那么子级.text设置了flex: 1就是设置了一个宽度,换行有效。

<div class="name">
  <div class="label">姓名</div>
  <div class="text">文本文本文本文本文本文本文本文本文本文本文本文本</div>
</div>
 
// css部分
.name {
  display: flex;
   justify-content: center;
   align-items: center;
  .label {
    width: 140px;
  }
  .text {
      flex: 1;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden; 
  }
}

方式二:若确定了宽度,则设置父级.text的宽度,如:width: calc(100% - 140px); 

// html部分
<div class="name">
  <div class="label">姓名</view>
  <div class="text"><div class="text1">文本文本文本文本文本文本文本文本文本文本</div></div>
</div>
 
// css部分
.name {
  display: flex;
   justify-content: center;
   align-items: center;
  .label {
    width: 140px;
  }
  .text {
   width: calc(100% - 140px);
    .text1 {     
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
    }
  }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 22:32:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 22:32:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 22:32:04       58 阅读
  4. Python语言-面向对象

    2024-07-11 22:32:04       69 阅读

热门阅读

  1. 使用Ultralytics YOLO进行模型验证

    2024-07-11 22:32:04       23 阅读
  2. 数据库(mysql)忘记密码解决办法

    2024-07-11 22:32:04       21 阅读
  3. GET正常,POST获取不到数据

    2024-07-11 22:32:04       20 阅读
  4. scoop安装在D盘

    2024-07-11 22:32:04       22 阅读
  5. 华为机考真题 -- 密码解密

    2024-07-11 22:32:04       19 阅读
  6. 设计模式——单例模式

    2024-07-11 22:32:04       21 阅读
  7. C# 反射

    2024-07-11 22:32:04       19 阅读
  8. Ubuntu 软件源404not found原因及解决办法

    2024-07-11 22:32:04       17 阅读
  9. 拓扑排序(算法篇)

    2024-07-11 22:32:04       23 阅读
  10. SQL 存储过程

    2024-07-11 22:32:04       24 阅读
  11. 大数据面试题之数据湖

    2024-07-11 22:32:04       21 阅读
  12. MySQL常用命令

    2024-07-11 22:32:04       17 阅读