前端css 文本超出就隐藏并且显示省略号

单行文本溢出显示省略号

overflow:hidden; //超出的文本隐藏
text-overflow:ellipsis; //溢出用省略号显示
white-space:nowrap; //溢出不换行

多行超出隐藏

div{
    
    
  overflow:hidden; 
  text-overflow:ellipsis;
  display:-webkit-box;    //将对象作为弹性伸缩盒子模型显示。
  -webkit-box-orient:vertical;  // 从上到下垂直排列子元素
  -webkit-line-clamp:2; //显示的行数
}

跨浏览器兼容的方案

注意:IE6-7不显示content内容,所以要兼容IE6-7可以是在内容中加入一个标签,比如…去模拟;要支持IE8,需要将::after替换为:after

p{
    
    
    position:relative;
    line-height:1.4em;
    /*设置容器高度为3倍行高就是显示3行*/
    height:4.2em;
    overflow:hidden;
}
p::after{
    
    
    content:'...';
    font-weight:bold;
    position:absolute;
    bottom:0;
    right:0;
    padding:0 20px 1px 45px;
    background:#fff;
}

表格中单行超出隐藏

table{
    
    width:100%;table-layout:fixed;/* 只有定义了表格的布局算法为fixed,下面td的定义才能起作用。 */}
td{
    
    
  width:100%;
  word-break:keep-all;   // 不换行
  white-space:nowrap;   // 不换行
  overflow:hidden;
  text-overflow:ellipsis;
}

感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

猜你喜欢

转载自blog.csdn.net/m0_49714202/article/details/124997798