In our daily development work, text overflow, truncation and omission are very common business scenario details that need to be considered. It looks "common", but there are different distinctions in implementation. Is it single-line truncation or multi-line truncation? Is the truncation judgment for multiple lines based on the number of lines or the height? What are the solutions to these problems? What are the differences between them and their adaptability to different scenarios? Generally speaking, when we do this kind of text truncation effect, we hope to:
Based on the above criteria, we will give some answers through coding practice. Single line text overflow ellipsis Core CSS Statements
advantage
Shortcomings
Applicable scenarios
Demo <div class="demo"> The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed.</div> .demo { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } Effect examples Multiline text overflow ellipsis (-webkit-line-clamp) Core CSS Statements
advantage
Shortcomings General compatibility: -webkit-line-clamp property is only supported by browsers with WebKit kernel Applicable scenarios Mostly suitable for mobile pages, because mobile device browsers are more based on the WebKit kernel Demo <div class="demo"> The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. .demo { display: -webkit-box; overflow: hidden; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } Effect examples Multi-line text overflow ellipsis (pseudo-element + positioning) Core CSS Statements
advantage
Shortcomings
Applicable scenarios There is a lot of text content, and it is certain that the text content will exceed the container Demo <div class="demo"> The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed The moonlight before the bed .demo { position: relative; line-height: 18px; height: 36px; overflow: hidden; word-break: break-all; } .demo::after { content:"..."; font-weight:bold; position:absolute; bottom:0; right:0; padding:0 20px 1px 45px; /* For better display effect */ background: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white)); background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); } Effect examples Multi-line text overflow ellipsis (Float) Core CSS Statements
advantage
Shortcomings The ellipsis may not be displayed just right, sometimes covering half of the text and not fitting closely to the text Applicable scenarios There is a lot of text content, and it is certain that the text content will exceed the container Demo <div class="demo"> <div class="text"> The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. The moonlight is bright in front of the bed. </div> .demo { height: 40px; line-height: 20px; overflow: hidden; } .demo .text { float: right; margin-left: -5px; width: 100%; word-break: break-all; } .demo::before { float: left; width: 5px; content: ""; height: 40px; } .demo::after { float: right; content: "..."; height: 20px; line-height: 20px; padding-right: 5px; text-align: right; width: 3em; margin-left: -3em; position: relative; left: 100%; top: -20px; padding-right: 5px; /* For better display effect */ background: -webkit-gradient( linear, left top, right top, from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white) ); background: -moz-linear-gradient( to right, rgba(255, 255, 255, 0), white 50%, white ); background: -o-linear-gradient( to right, rgba(255, 255, 255, 0), white 50%, white ); background: -ms-linear-gradient( to right, rgba(255, 255, 255, 0), white 50%, white ); background: linear-gradient( to right, rgba(255, 255, 255, 0), white 50%, white ); } Effect examples This concludes this article on several methods of implementing “text overflow, truncation and ellipsis” with pure CSS. For more relevant CSS text overflow, truncation and ellipsis content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope that everyone will support 123WORDPRESS.COM in the future! |
<<: Illustration-style website homepage design New trend in website design
>>: Best Practices Guide for Storing Dates in MySQL
Table of contents 1. Security issues with Docker ...
This article shares with you the installation and...
Recently, a service has an alarm, which has made ...
1. Virtual Machine Side 1. Find the mysql configu...
1. Business scenario introduction Suppose there i...
Table of contents Overview The role of reverse pr...
Prerequisite: nginx needs to have the ngx_http_li...
This article records the installation and configu...
MYSQL officially provides an Installer method to ...
A problem occurred when configuring a cluster. Or...
I divide the whole process into four steps: Downl...
Table of contents 1. Scene loading 2. Find Node 1...
Table of contents 1. Resource files 2. Installati...
Table of contents 1 What is SSH 2 Configure SSH p...
This article shares the specific code of Vue to i...