文本内容超出容器,省略号隐藏

单行文本隐藏

用text-overflow:ellipsis属性来实现单行文本的溢出显示省略号(…),部分浏览器还需要加宽度width属性。
父容器设置如下属性,实现单行省略号隐藏
overflow:hidden;
text-overf:ellipsis;
white-space: nowrap;

多行文本隐藏

  • 在WebKit浏览器或移动端(绝大部分是WebKit内核的浏览器)的页面实现比较简单,可以直接使用WebKit的CSS扩展属性(WebKit是私有属性)-webkit-line-clamp
    ;注意:这是一个 不规范的属性,它没有出现在 CSS 规范草案中。
    父容器中设置如下属性,实现多行省略号隐藏
    overflow : hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;/文本行数/
    -webkit-box-orient: vertical;
  • 比较靠谱简单的做法就是设置相对定位的容器高度,用包含省略号(…)的元素模拟实现;
    p {
    position:relative;
    line-height:1.4em;
    /* 3 times the line-height to show 3 lines */
    height:4.2em;
    overflow:hidden;
    }
    p:after {
    content:"...";
    font-weight:bold;
    position:absolute;
    bottom:0;
    right:0;
    padding:0 20px 1px 45px; background:url(http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;
    }
    这里注意几点:

height高度是line-height的3倍;
结束的省略好用了半透明的png做了减淡的效果,或者设置背景颜色;
IE6-7不显示content内容,所以要兼容IE6-7可以是在内容中加入一个标签,比如用...去模拟;

  • js方法以后再写

你可能感兴趣的:(文本内容超出容器,省略号隐藏)