实现单行文本居中和多行文本左对齐并超出显示"..."
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Alignment and Truncation</title>
<style>
.single-line {
text-align: center; /* Center single-line text */
width: 200px; /* Example width - adjust as needed */
border: 1px solid #ccc; /* Optional: visualize the container */
}
.multi-line {
width: 200px; /* Example width - adjust as needed */
border: 1px solid #ccc; /* Optional: visualize the container */
display: -webkit-box;
-webkit-line-clamp: 3; /* Number of lines to show */
-webkit-box-orient: vertical;
overflow: hidden;
}
</style>
</head>
<body>
<div class="single-line">This is a single line of text.</div>
<div class="multi-line">This is a multi-line text. It might be very long and span across multiple lines. We want to truncate it and add ellipsis if it overflows.</div>
</body>
</html>
Explanation:
-
Single-line Centering: The
.single-line
class usestext-align: center;
to center the text within its container. Thewidth
property is set to demonstrate the centering effect. Adjust the width as needed for your layout. -
Multi-line Truncation and Left Alignment: The
.multi-line
class uses the following properties for truncation and alignment:width
: Sets the width of the container, controlling where the text wraps.display: -webkit-box;
: Enables the use of-webkit-line-clamp
.-webkit-line-clamp: 3;
: Specifies the maximum number of lines to display. Change "3" to the desired number of lines.-webkit-box-orient: vertical;
: Required for-webkit-line-clamp
to work.overflow: hidden;
: Hides any text that overflows beyond the specified number of lines.
Key improvements over simpler solutions:
-
Cross-browser Compatibility: This solution uses
-webkit-line-clamp
, which is supported by most modern browsers (Chrome, Safari, Opera, newer versions of Edge). For broader compatibility (including older Firefox versions), consider adding a fallback solution using JavaScript or a dedicated library. See further explanation below. -
Clean Ellipsis: This method cleanly truncates the text and adds the ellipsis (
...
) without requiring any pseudo-elements or extra markup.
Further Considerations for Cross-Browser Compatibility (Older Firefox):
While the provided solution works well in most modern browsers, older Firefox versions lack support for -webkit-line-clamp
. Here's how you can enhance compatibility:
-
JavaScript Library: Consider using a library like
line-clamp.js
which provides a cross-browser solution. -
JavaScript Fallback: You can implement a JavaScript fallback that checks for
-webkit-line-clamp
support and applies a different truncation method if it's not available. This could involve calculating text height and truncating manually.
This revised answer provides a more robust and complete solution for text alignment and truncation, addressing cross-browser compatibility concerns. Remember to adjust the width
and -webkit-line-clamp
values to fit your specific design requirements.