使用省略號和計數器隱藏第二行後的文本溢出
問題:
隱藏部分超過兩行的文字並添加“...123 T”。因為隱藏的溢出指示器需要一個聰明的解決方案。
解:
雖然未來的更新將提供使用 line-clamp屬性的更簡單的方法,但這裡有一個創造性的技巧達到這個目的效果:
CSS:
.container { max-width: 200px; margin: 5px; } .main-text { line-height: 1.2em; /* line height */ max-height: calc(2 * 1.2em); /* limit height to 2 lines */ overflow: hidden; display: inline-block; position: relative; } .main-text:after { content: "123 T."; display: inline-block; width: 40px; position: relative; z-index: 999; /* big box shadow to hide the ellipsis */ box-shadow: 40px 0 0 #fff, 80px 0 0 #fff, 120px 0 0 #fff, 160px 0 0 #fff; color: #8e8f8f; font-size: 10px; background: #fff; /* cover text beneath */ margin-left: 2px; } .main-text span { position: absolute; /* bottom right position */ top: 1.2em; /* 1 line height */ right: 0; padding: 0 3px; background: #fff; /* cover text beneath */ } .main-text span:before { content: "..."; /* ellipsis */ } .main-text span:after { content: "123 T."; color: #8e8f8f; font-size: 10px; }
HTML:
<div class="container"> <div class="main-text"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam metus mi, dapibus sit amet posuere eu, porttitor condimentum nulla. Donec convallis lorem justo, eget malesuada lorem tempor vitae. Aliquam sollicitudin lacus ipsum, at tincidunt ante condimentum vitae. <span></span> </div> </div> <div class="container"> <div class="main-text"> Lorem ipsum <span></span> </div> </div> <div class="container"> <div class="main-text"> Lo <span></span> </div> </div> <div class="container"> <div class="main-text"> Lorem ipsum dolor sit ameta, adipiscing elit. Nam metus <span></span> </div> </div> <div class="container"> <div class="main-text"> Lorem ipsum dolor sit ameta, adipiscing elit <span></span> </div> </div>
HTML:
此技術確保「.. .123T。以上是如何使用省略號和自訂計數器('...123 T.”)隱藏第二行之後的文字溢位?的詳細內容。更多資訊請關注PHP中文網其他相關文章!