Home > Article > Web Front-end > How to achieve the ellipsis effect beyond the part in html
Omitting the excess part in HTML is a common design requirement. When text content overflows the bounds, we usually truncate it and replace it with ellipses. Not only does this make the content look cleaner, but it also provides a better user experience. This article will introduce how to use CSS to achieve the effect of omitting excess parts of HTML.
When realizing the effect of omitting the excess part of HTML, we usually use the text-overflow property of CSS. Its function is to specify how the excess part should be handled when the text content in an element overflows its container.
This attribute has 3 values:
clip
: Default value, the excess part will be clipped and not displayed. ellipsis
: The excess part is replaced with ellipses. string
: The excess part is replaced with the given string. Among them, the most commonly used one is ellipsis
, because it can achieve the ellipsis effect simply and quickly.
When using the text-overflow attribute, we also need to set the white-space attribute. This property specifies how whitespace within the element is handled. The default value is normal
, which means to merge consecutive whitespace characters and convert newlines to spaces.
If we want to preserve consecutive space characters in the text, we need to set the white-space attribute to pre-wrap
. Also, text-overflow will only take effect if the white-space property is set to nowrap
and pre-wrap
.
The following is an example of using CSS to achieve the ellipsis effect:
<style> .ellipsis { width: 200px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> <div class="ellipsis"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consequat magna vel leo sollicitudin convallis. Sed vestibulum auctor purus a tristique. </div>
In the above example, we created a block level with width Element to hold text content. Next, we set the white-space property to nowrap so that consecutive spaces in the text are not merged. We use the overflow attribute to hide the overflow part and use text-overflow: ellipsis to replace it with ellipses.
Although using ellipsis effects can make the content tidier, we also need to use ellipses appropriately to ensure user experience. Here are some suggestions for proper use of HTML ellipses:
In this article, we introduced how to use CSS to achieve the effect of omitting excess parts in HTML. We mainly use text-overflow and white-space properties to achieve this purpose. Finally, we also give some suggestions for the appropriate use of ellipses to help you optimize the user experience.
The above is the detailed content of How to achieve the ellipsis effect beyond the part in html. For more information, please follow other related articles on the PHP Chinese website!