CSS EllipsisDefinition and Purpose:CSS ellipsis is a text-overflow attribute that allows developers to truncate overflowing text and show an ellipsis (...) to signal that the content has been cut off. The ellipsis is a three-dot sequence, often representing the absence of additional text. CSS ellipsis' principal purpose is to handle instances where the space for displaying text is restricted, preventing content from breaking the layout or overflowing outside its container. Explanation of How CSS Ellipsis is Used to Truncate TextCSS ellipsis can display an ellipsis after visible text when text overflows its container, and there is insufficient space to display the complete content. This truncation approach is frequently employed in situations such as:
Importance of CSS Ellipsis in Maintaining a Clean and Visually Appealing LayoutCSS ellipsis is essential for maintaining a clean and visually pleasing layout, especially with limited text display space. Its importance can be found in the following aspects:
Implementing CSS EllipsisOverview of the CSS Properties Involved in Implementing EllipsisThree main CSS properties are used to achieve CSS ellipsis and truncate text that overflows its container: White space: This attribute governs how white spaces (spaces, tabs, and line breaks) are handled inside the text. It has an impact on how text wraps and breaks within a container. Values: normal, nowrap, pre, pre-wrap, pre-line. Overflow: This attribute governs how material that exceeds the container's bounds is displayed. It decides whether scrollbars are displayed, if the content is cropped, or whether it is concealed. Values: visible, hidden, scroll, auto. Text-overflow: This property determines how an ellipsis is displayed when text is truncated when it deals with text overflow. Values: clip, ellipsis. Examples of Using Each Property Individually and in Combination to Create Ellipsis EffectsExample: Implementing ellipsis with text-overflow. Example: Using white space and overflow. Example: Combining All Properties We integrate all three features in the third case. We may apply the text-overflow: ellipsis property specifically to the span by utilizing a nested span element inside the container's p element, allowing the ellipsis effect to apply exclusively to a specific section of the text within the container. This technique can be handy when truncating text within a certain element while leaving other material within the container unchanged. Handling Multi-Line TextUsing CSS ellipsis to handle multi-line text is more difficult than single-line text. There are techniques to use CSS to create ellipsis effects on multi-line text. One of the popular methods involves using -webkit-line-clamp for WebKit-based browsers (e.g., Chrome and Safari) and display: -webkit-box in combination with text-overflow: ellipsis. When the content exceeds the container's capacity, this technique permits a specified number of lines of text with an ellipsis. Example 1: Using display: -webkit-box for Multi-Line Ellipsis (Cross-Browser Approach) In this example, we set the .container element's max height to around three lines of text (assuming 1.5cm for each line). We achieve a similar multi-line ellipsis effect for other browsers by using display: -webkit-box, -webkit-box-orient, and text-overflow: ellipsis, but the outcome may differ significantly. Note: The number of lines displayed before truncation may vary significantly depending on the font, line height, and browser rendering.Example 2: Using -webkit-line-clamp for Multi-Line Ellipsis In the example above, a p element in the .container element contains multi-line text. We restrict the display to three lines of text by using -webkit-line-clamp and -webkit-box-orient, and we apply overflow: hidden to conceal any information that extends past the designated lines. If there is further material, this produces a multi-line ellipsis effect that displays an ellipsis (...) at the end of the third line. Note: Remember that -webkit-line-clamp is a 'proprietary property' that only functions in browsers that support WebKit.Customizing Ellipsis StylingThe ellipsis can be styled differently in CSS, which enables designers to produce a more aesthetically pleasing and unified design. Developers can customize the ellipsis to better fit the overall theme of the website or application by changing variables such as the ellipsis character, color, and spacing. The Following are Some Ellipsis Styling Customization Options:Example 1: Changing the color of the ellipsis: Make the ellipsis' color fit the color scheme of your website. Example 2: Changing the character in the ellipsis Three dots are frequently used as the standard ellipsis symbol (...). To better fit the typography and appearance of your website, you can swap it out for another character like "..." or the Unicode ellipsis (...). Example 3: Adding Spacing Around the Ellipsis: To improve the ellipsis' visual appearance and distinguish it from the text, put padding or margin around it. Example 4: Using Pseudo-Elements for Customising Ellipsis: More visual freedom can be achieved by styling the ellipsis with pseudo-elements like as::before or::after. Note: Always strike a balance between customization, usability, and readability.
Next TopicCSS First Child
|