Bootstrap - 文本截断
本章讨论 Bootstrap 提供的用于截断长文本字符串的实用工具。文本截断是一种功能,它允许截断溢出容器的长文本字符串,并在截断文本末尾显示省略号(...),以指示还有更多文本可用。
这在显示文本的空间有限的情况下非常有用,例如在表格或卡片中。
要在 Bootstrap 5 中截断文本,可以使用 .text-truncate class。
它需要 display: inline-block 或 display: block。
此 class 可以添加到任何包含文本的元素上,例如 <div> 或 <p> 元素。
以下是使用 .text-truncate class 的示例:
示例
您可以使用 Edit & Run 选项编辑并运行此代码。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap - Helper - Text truncation</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h4>Text truncation example</h4>
<!-- 块级元素 -->
<div class="row">
<div class="col-3 text-truncate">
The string of text seems to be very long, thus truncating it.
</div>
</div>
<!-- 行内元素 -->
<span class="d-inline-block text-truncate" style="max-width: 150px;">
The string of text seems to be very long, thus truncating it.
</span>
</body>
</html>