🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The text-overflow property in CSS helps you manage how overflowed text is displayed when it exceeds the container's width. This is especially useful when dealing with long lines of text that need to be clipped, truncated, or indicated with an ellipsis (...).
In this tutorial, you'll learn how to use the text-overflow property to handle overflowing text within a fixed-width container.
Problem Statement
Create a CSS code that:
- Controls how overflowing text is displayed using the
text-overflowproperty. - Demonstrates how to clip, truncate, or add an ellipsis to overflowing text.
Example:
- Input: A paragraph element with a long sentence inside a fixed-width container.
- Output: The text either gets clipped or shows an ellipsis when it overflows the container.
Solution Steps
- Use
text-overflowProperty: Apply thetext-overflowproperty to control how overflowing text is handled. - Set
white-spaceandoverflowProperties: Usewhite-spaceandoverflowin combination to properly manage the overflow effect.
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Overflow Example</title>
<style>
/* Step 1: Basic text overflow with ellipsis */
.text-ellipsis {
white-space: nowrap; /* Prevent text from wrapping to the next line */
overflow: hidden; /* Hide the overflowed text */
text-overflow: ellipsis; /* Add ellipsis to indicate overflow */
width: 200px; /* Set a fixed container width */
border: 1px solid #ccc;
}
/* Step 2: Clip overflowing text */
.text-clip {
white-space: nowrap;
overflow: hidden;
text-overflow: clip; /* Clip the overflowed text */
width: 200px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<p class="text-ellipsis">This is a long sentence that will be truncated with an ellipsis when it overflows the container width.</p>
<p class="text-clip">This is another long sentence that will be clipped when it overflows the container width.</p>
</body>
</html>
Output
Explanation
Step 1: Use text-overflow: ellipsis
- To add an ellipsis (
...) to overflowing text, use this CSS:.text-ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 200px; border: 1px solid #ccc; }white-space: nowrap: Prevents text from wrapping to the next line.overflow: hidden: Hides the overflowing part of the text.text-overflow: ellipsis: Adds an ellipsis (...) to indicate that text has been truncated.
Step 2: Use text-overflow: clip
- To clip the overflowing text without any indication, use this CSS:
.text-clip { white-space: nowrap; overflow: hidden; text-overflow: clip; width: 200px; border: 1px solid #ccc; }
Conclusion
Using the text-overflow property in CSS allows you to control how text is displayed when it exceeds the container's width. By applying ellipsis or clip, you can ensure your layout remains clean and readable, even with long text strings.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment