单行文本溢出和多行文本溢出white-space和word-break

1.单行文本溢出要满足三个条件:

overflow: hidden;
white-space: nowrap;//文本不会换行,文本会在在同一行上继续,直到遇到 
标签为止。 text-overflow: ellipsis;

单行文本溢出没有兼容性的问题,在主流浏览器里能正常显示效果。
2.多行文本溢出需要满足4个条件:

display:-webkit-box;
overflow:hidden;
-webkit-box-orient:vertical; //子元素应该被水平或垂直排列
-webkit-line-clamp:3;  //3行后显示省略号

适用范围:
因使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端
3.多浏览器兼容,该方法适用范围广,但文字未超出行的情况下也会出现省略号,可结合js优化该方法。

p{position: relative; line-height: 20px; max-height: 40px;overflow: hidden;}
p::after{content: "..."; position: absolute; bottom: 0; right: 0; padding-left: 40px;
background: -webkit-linear-gradient(left, transparent, #fff 55%);
background: -o-linear-gradient(right, transparent, #fff 55%);
background: -moz-linear-gradient(right, transparent, #fff 55%);
background: linear-gradient(to right, transparent, #fff 55%);
}

white-space属性

white-space属性设置如何处理元素内的空白。
可能的值有:

描述
normal 默认。空白会被浏览器忽略。
pre 空白会被浏览器保留。其行为方式类似 HTML 中的
 标签。
nowrap 文本不会换行,文本会在同一行上继续,直到遇到
标签为止
pre-wrap 保留空白符序列,但是正常地进行换行。
pre-line 合并空白符序列,但是保留换行符。
inherit 规定应该从父元素继承 white-space 属性的值。

word-break属性

word-break属性规定自动换行的处理方式;
可能的值有:normal | keep-all // 只能在半角空格或连字符处换行 | break-all // 允许单词内换行

你可能感兴趣的:(单行文本溢出和多行文本溢出white-space和word-break)