Keywords: CSS text truncation | multi-line ellipsis | line-height control | overflow hidden | browser compatibility
Abstract: This paper provides an in-depth exploration of technical solutions for limiting text content to two lines with ellipsis display in CSS. Through analyzing the synergistic effects of line-height, height, and overflow properties, it explains in detail how to precisely control text line numbers. Modern CSS properties like -webkit-line-clamp are introduced as supplementary solutions, with practical code examples demonstrating the advantages and disadvantages of various implementation methods. The article also discusses browser compatibility issues and best practice recommendations, offering practical text truncation solutions for front-end developers.
Introduction
In modern web development, controlling the layout and display of text content is a frequent technical challenge faced by front-end engineers. Particularly in limited display spaces, how to elegantly handle long text truncation and ellipsis display directly impacts user experience and interface aesthetics. This paper systematically explores technical solutions for implementing two-line text truncation with ellipsis display in CSS.
Basic Implementation Principles
The core principle of limiting text lines in CSS is based on precise control of element height and line height. By setting the line-height property to define single-line text height, and then setting the height property as an integer multiple of line height, the number of displayed text lines can be precisely controlled.
.two-line-text {
line-height: 1.5em;
height: 3em;
overflow: hidden;
}
In this example, line-height is set to 1.5em, meaning each line of text occupies 1.5 times the font size height. height is set to 3em, exactly twice the line height, thus ensuring only two lines of text are displayed. The overflow: hidden property is responsible for hiding content that exceeds the specified height.
Challenges of Ellipsis Display
The traditional text-overflow: ellipsis property performs well in single-line text truncation but has limitations in multi-line text scenarios. When combined with white-space: nowrap, although ellipsis can be displayed, it forces text to display in a single line, unable to achieve true multi-line truncation.
.single-line-ellipsis {
line-height: 1.5em;
height: 3em;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 100%;
}
Modern CSS Solutions
With the continuous development of CSS standards, more advanced multi-line text truncation solutions have emerged. The -webkit-line-clamp property is specifically designed to control the number of text display lines. When combined with display: -webkit-box and -webkit-box-orient: vertical, it can achieve multi-line truncation more concisely.
.modern-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
line-height: 1.5em;
max-height: 3em;
}
Although this method has more concise syntax, browser compatibility issues need attention, as it mainly applies to WebKit-based browsers.
Practical Application Scenarios
In Dash framework tab components, text truncation technology is particularly important. When there are numerous tabs with uncertain text lengths, ensuring tab text displays completely within two lines or is appropriately truncated maintains interface consistency and aesthetics.
.tab-label {
line-height: 1.2;
height: 2.4em;
overflow: hidden;
text-align: center;
padding: 8px;
flex: 1 1 11%;
}
Browser Compatibility Considerations
When selecting implementation solutions, support levels across different browsers must be considered. Traditional height control methods offer the best browser compatibility, while the -webkit-line-clamp solution, though syntactically elegant, is mainly limited to modern browsers. In actual projects, appropriate solutions can be chosen based on the browser usage of the target user group.
Best Practice Recommendations
1. Prioritize using traditional height control methods to ensure maximum browser compatibility
2. In scenarios requiring better user experience, combine JavaScript for more precise text truncation
3. Always provide appropriate fallback solutions to ensure normal display in browsers that don't support certain CSS features
4. In responsive design, consider line height and height adaptation across different screen sizes
Conclusion
Text line control and ellipsis display in CSS represent a technical field with both challenges and mature solutions. By reasonably combining line-height, height, and overflow properties, along with modern CSS features, developers can achieve elegant text truncation effects in various scenarios. Understanding the principles and limitations of these technologies helps make more appropriate technical choices in actual projects.