若是實現單行文本的溢出顯示省略號同窗們應該都知道用text-overflow:ellipsis屬性來,固然還須要加寬度width屬來兼容部分瀏覽。web
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;瀏覽器
可是這個屬性只支持單行文本的溢出顯示省略號,若是咱們要實現多行文本溢出顯示省略號呢。優化
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;spa
因使用了WebKit的CSS擴展屬性,該方法適用於WebKit瀏覽器及移動端;3d
注:對象
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%);
}blog
該方法適用範圍廣,但文字未超出行的狀況下也會出現省略號,可結合js優化該方法。ip
將height設置爲line-height的整數倍,防止超出的文字露出。
給p::after添加漸變背景可避免文字只顯示一半。
因爲ie6-7不顯示content內容,因此要添加標籤兼容ie6-7(如:…);兼容ie8須要將::after替換成:after。it