


How to realize that css text exceeds the ellipses in a single line and multiple lines?
Sometimes when designing a web page, you may encounter text that is too long, causing the page to be unsightly. So how to solve this problem? This article will introduce to you the implementation method of css text that exceeds the ellipses in a single line and multi-line text in the ellipses.
First of all, let’s take a look at the CSS method to achieve a single line of text beyond the ellipses.
css To implement the overflow display of ellipsis in a single line of text, you should use the text-overflow:ellipsis attribute. Of course, you also need to add the width attribute to be compatible with partial browsing.
The implementation code for css single line text exceeding the ellipses:
<!DOCTYPE html> <html> <head> <meta charset=utf-8"> <title>省略号 test</title> <style type="text/css"> *{ margin:0; padding:0; } body{ padding:10px; font-family:Arial; } #test { position:relative; width:150px; height:20px; line-height:20px; text-overflow:ellipsis; white-space:nowrap; *white-space:nowrap; overflow:hidden; border:1px solid #999; } #test span{ position:absolute; top:0; right:0; display:block; float:left; } </style> </head> <body> <div id="test">php中文网php中文网php中文网php中文网php中文网php中文网php中文网</div> </body> </html>
The effect of css single line text exceeding the ellipsis is as follows:
text-overflow: The ellipsis attribute only supports the overflow display of ellipsis in a single line of text. What if we want to implement the overflow display of ellipsis in multiple lines of text? Next, we will continue to talk about the css method to implement multi-line text beyond the ellipsis . (For more information on the text-overflow:ellipsis attribute, please refer to the css manual)
1. CSS realizes that when multi-line text exceeds the ellipsis, it can be directly set with css attributes (only -webkit kernel Only then will it work)
Syntax:
overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
Most mobile browsers are based on WebKit core, so this method is suitable for mobile terminals;
-webkit -line-clamp is used to limit the number of lines of text displayed in a block element. This is an unsupported WebKit property that does not appear in the CSS draft specification.
display: -webkit-box Display the object as a flexible box model.
-webkit-box-orient sets or retrieves the arrangement of the child elements of the flex box object.
text-overflow: ellipsis is used for multi-line text, using ellipses "..." to hide text that exceeds the range.
2. Use absolute positioning and padding to implement multi-line text beyond the ellipsis using css; (cross-browser solution)
Syntax:
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%); }
The principle of this method is: first embed a ... in the element containing text, then leave a position of... (padding-right) on the right side of the element containing text, and finally Use absolute positioning to position... to the padding-right area on the right.
Note: This method has a wide range of applications, but ellipses will also appear when the text does not exceed the line. This method can be optimized with js.
Note:
Set height to an integer multiple of line-height to prevent excess text from being exposed.
Add a gradient background to p::after to prevent only half of the text from being displayed.
Since ie6-7 does not display content content, you need to add tags to be compatible with ie6-7 (such as: ...); to be compatible with ie8, you need to replace ::after with :after .
This article ends here. For more information about css text beyond the ellipses, please pay attention to the php Chinese website.
The above is the detailed content of How to realize that css text exceeds the ellipses in a single line and multiple lines?. For more information, please follow other related articles on the PHP Chinese website!

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
