


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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
