search
HomeWeb Front-endCSS TutorialHow to make the overflow part of text display ellipsis in css? (code example)

When we are doing web front-end development, we usually get the article title and then display it line by line. But when the title is too long, it will cause line breaks. Also, when displaying part of the text information, it would be too cumbersome to display all of it, which would lead to a poor web experience. Although we can use overflow:hidden to hide characters that exceed the width. But the ending always seems a bit stiff. It is also not conducive to letting the user know that there are still undisplayed characters later. The best way is to replace the extra characters with ellipses.

In this chapter, we will give you a detailed introduction to how CSS makes the overflow part of the text display ellipses. I hope to be helpful.

1: A single line of text overflows and displays an ellipsis... (mostly the excess part of the title displays an ellipsis...)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>CSS如何使文本溢出部分显示省略号?单行超出</title>
		<style>
			*{margin: 0px;padding: 0px;}
			.box{width: 300px;height: 500px;margin: 50px auto;}
			.overflow{
				width:220px;
				overflow:hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				-o-text-overflow:ellipsis;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<p>
				css 实现单行文本超出长度显示省略号
			</p>
			<p class="overflow">
				css 实现单行文本超出长度显示省略号
			</p>
		</div>
	</body>
</html>

Rendering of the above code As follows:

How to make the overflow part of text display ellipsis in css? (code example)

Among them, white-space: nowrap; means that the text will not wrap and continue on the same line until it encounters a label;

overflow: hidden; Do not display content that exceeds the object size, that is, hide the excess part;

text-overflow: ellipsis; When the text object overflows, it is displayed..., of course, you can also set the attribute to clip Dots are not displayed;

-o-text-overflow: In order to be compatible with the opera browser;

2: Multi-line text overflow displays ellipses...

1. Directly use css attribute settings (only the -webkit kernel has an effect)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>CSS如何使文本溢出部分显示省略号?多行超出</title>
		<style>
			*{margin: 0px;padding: 0px;}
			.box{
				width: 280px;
				height: 62px;
				margin: 50px auto;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 3;
				-webkit-box-orient: vertical;
			}
		</style>	
	</head>
	<body>
		<div class="box">
			css 实现多行文本超出长度显示省略号,css 实现多行文本超出长度显示省略号,
			css 实现多行文本超出长度显示省略号
		</div>
	</body>
</html>

The rendering of the above code is as follows:

How to make the overflow part of text display ellipsis in css? (code example)

Among them, 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 a different Unsupported WebKit property, which does not appear in the CSS draft specification;

display: -webkit-box displays 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 the ellipsis "..." to hide the text that exceeds the range.

2. Using pseudo-classes

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>CSS如何使文本溢出部分显示省略号?多行超出</title>
		<style>
			*{margin: 0px;padding: 0px;}
			.box{
				width: 280px;
				height: 62px;
				margin: 50px auto;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 3;
				-webkit-box-orient: vertical;
			}
			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%);
			}
		</style>	
	</head>
	<body>
		<div class="box">
		  <p>
		      css 实现多行文本超出长度显示省略号,
		      css 实现多行文本超出长度显示省略号,
		      css 实现多行文本超出长度显示省略号
		 </p>
		</div>
	</body>
</html>

The rendering of the above code is as follows:

How to make the overflow part of text display ellipsis in css? (code example)

This method has a wide range of applications, but the text does not exceed Ellipses will also appear in the case of rows, and this method can be optimized with js.

Set height to an integer multiple of line-height to prevent excess text from being exposed. Adding a gradient background to p::after prevents only half of the text from being displayed. Since ie6-7 does not display 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.

3. Use absolute positioning and padding; (cross-browser solution)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>CSS如何使文本溢出部分显示省略号?多行超出</title>
		<style>
			*{margin: 0px;padding: 0px;}
			.box{width: 300px;height: 500px;margin: 50px auto;}
			.con{
			  position: relative;
			  height: 40px;
			  width: 250px;
			  line-height: 20px;
			  overflow: hidden;
			  padding-right: 12px;
			}
			.t{
			  position: absolute;
			  right: 0;
			  bottom: 0;
			}
		</style>	
	</head>
	<body>
		<div class="box">
			 <p class="con">
			 	css 实现多行文本超出长度显示省略号,
			 	css 实现多行文本超出长度显示省略号,
			 	css 实现多行文本超出长度显示省略号。
			 	<span class="t">...</span>
			 </p>
		</div>
	</body>
</html>

Rendering of the above code:

How to make the overflow part of text display ellipsis in css? (code example)

This The principle of the method is: first embed a ... in the element containing text, and then leave an ellipsis... position (padding-right) on the right side of the element containing text, Finally, use absolute positioning to position the ellipsis... to the padding-right area on the right (lower right corner).

The above is the detailed content of How to make the overflow part of text display ellipsis in css? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

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.