天天看點

CSS超出省略号顯示

  • 單行文本的溢出顯示省略号應該都知道用text-overflow:ellipsis屬性來,當然還需要加寬度width屬來相容部分浏覽。
overflow:hidden; //超出的文本隐藏
	text-overflow:ellipsis; //溢出用省略号顯示
	white-space:nowrap; //溢出不換行
           
  • 多行文本溢出顯示省略号
overflow: hidden;
text-overflow: ellipsis;
display:-webkit-box; //作為彈性伸縮盒子模型顯示。
-webkit-box-orient:vertical; //設定伸縮盒子的子元素排列方式--從上到下垂直排列
-webkit-line-clamp:3; //顯示的行
           

适用範圍:

因使用了WebKit的CSS擴充屬性,該方法适用于WebKit浏覽器及移動端;

  • 多行文本溢出顯示省略号
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%);
           

适用範圍:

該方法适用範圍廣,但文字未超出行的情況下也會出現省略号,可結合js優化該方法。

Tips:【小程式雲開發】中進階前端面試題庫(源碼:小程式中聯系我喲)。

---------- 創作不易,感謝大家,請多多支援!

CSS超出省略号顯示