![picture[1]-Implementing Text Overflow Ellipsis with CSS-PANDA](https://oss.imwmi.com/file/imwmi/webp/2024/01/QQ20231228165905.webp)
When the text content exceeds the container width, you can utilize the CSS ‘text-overflow’ property to implement an ellipsis effect, allowing for better handling of lengthy text.
.container {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Implementing text overflow ellipsis with CSS is a helpful technique when dealing with text that exceeds its container width. Here’s an example:
HTML:
<div class="text-ellipsis">
This is a long text that will be truncated with an ellipsis when it overflows its container.
</div>
CSS:
/* Styling the element */
.text-ellipsis {
width: 200px; /* Adjust width as needed */
white-space: nowrap; /* Prevent text wrapping */
overflow: hidden; /* Hide overflowing content */
text-overflow: ellipsis; /* Display ellipsis for overflowed text */
border: 1px solid #ccc; /* Optional: Adding a border for visualization */
}
Explanation:
white-space: nowrap;
: Prevents text from wrapping to the next line.overflow: hidden;
: Hides any content that exceeds the container’s width.text-overflow: ellipsis;
: Adds an ellipsis (…) to indicate that text has been truncated due to overflow.
Adjust the .text-ellipsis
class properties such as width
, border
, etc., based on your design requirements. This technique helps manage long text within a defined space by indicating that the text has been cut off and providing an ellipsis to signify there’s more content.
THE END
No comments