首页 > web前端 > css教程 > 正文

使用 CSS 将文本长度限制设置为 N 行

王林
发布: 2023-08-27 13:29:08
转载
1950人浏览过

使用 css 将文本长度限制设置为 n 行

有时候,开发者需要根据HTML元素的尺寸来截断文本。例如,div元素的宽度是100px,它只能容纳一些字符。因此,我们需要使用CSS来截断文本。

然而,我们可以使用JavaScript截断文本,但这可能会引起问题。例如,我们可以在100px中显示18个字符,但有时由于字符的大写,可能会显示更少的字符。在这种情况下,如果我们显示18个字符,可能会溢出。

所以,使用CSS截断文本是一个更好的主意。

语法

用户可以按照以下语法使用 CSS 将文本限制设置为 N 行。

立即学习前端免费学习笔记(深入)”;

overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
登录后复制

在上面的语法中,我们将溢出设置为隐藏,‘text-overflow: elipsis’来截断文本。 “-webkit-line-clamp”属性的值显示要显示的行数。

示例 1

在下面的示例中,我们仅显示一行文本。我们将截断其他文本。我们将 300px 设置为 div 元素的宽度,隐藏为溢出,省略号为 text-overflow 属性。此外,我们使用“white-space”属性和“no-wrap”值来仅在单行中显示文本。

<html>
<head>
   <style>
      div {
         height: auto;
         width: 300px;
         overflow: hidden;
         text-overflow: ellipsis;
         white-space: nowrap;
         background-color: red;
         color: white;
         padding: 10px;
      }
   </style>
</head>
<body>
   <h2>Limiting the text length to 1 line using CSS</h2>
   <div>This is a div containing multiple lines of text. The text visibility is limited to 1 line only.</div>
</body>
</html>
登录后复制

Example 2

的中文翻译为:

示例2

在下面的示例中,我们展示了N行被截断的文本。我们像第一个示例一样向div元素添加了文本。之后,我们使用了“webkit-line-clamp”CSS属性来设置行数,而不是使用“white-space: no-wrap”CSS属性。

在输出中,用户可以观察到它仅显示了三行截断的文本。用户可以更改行数并观察输出。

<html>
<head>
   <style>
      div {
         overflow: hidden;
         text-overflow: ellipsis;
         display: -webkit-box;
         line-height: 20px;
         max-height: 100px;
         padding: 4px 10px;
         max-width: 400px;
         background-color: aqua;
         -webkit-line-clamp: 3;
         -webkit-box-orient: vertical;
      }
   </style>
</head>
<body>
   <h3>Limiting the text length to N line using CSS</h3>
   <div>Git is a popular version control system used to track changes in code or other files. It allows multiple
      developers to work on the same project simultaneously, without overwriting each other's changes. Git uses a
      distributed architecture, which means that each developer has their own local copy of the repository, and
      changes can be easily merged together.</div>
</body>
</html>
登录后复制

Example 3

的中文翻译为:

示例3

在下面的示例中,我们将演示将文本截断为N行的实时用途。在这里,我们使用HTML和CSS创建了卡片组件。我们在卡片的左侧添加了图像,右侧添加了文本。此外,卡片的宽度是固定的。

我们需要在卡片的右侧显示文本,而不让文本溢出。我们将文本截断为4行,可以在输出中看到。

<html>
<head>
   <style>
      .card {
         background-color: grey;
         width: 400px;
         height: 80px;
         display: flex;
         border-radius: 12px;
         box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
         text-align: left;
         justify-content: center;
      }
      .img {
         width: 130px;
         height: 70px;
         margin-right: 30px;
         padding: 5px;
      }
      img {
         width: 100%;
         height: 100%;
      }
      .content {
         width: 450px;
         height: 70px;
         overflow: hidden;
         text-overflow: ellipsis;
         display: -webkit-box;
         -webkit-line-clamp: 4;
         -webkit-box-orient: vertical;
      }
   </style>
</head>
<body>
   <h2>Limiting the text length to N line using CSS</h3>
   <div class = "card">
      <div class = "img">
         @@##@@
      </div>
      <div class = "content">
         This is an information about the image. Whatever text will fit to the div, it will be shown. If the text is
         more than the div, then it will be hidden and the text will be shown as ellipsis.
      </div>
   </div>
</body>
</html>
登录后复制

用户学会了将文本截断为多行。我们可以使用‘overflow:hidden’和‘text-overflow:elipsis’CSS属性来截断文本。此外,我们需要使用“white-space: no-wrap”来截断单行文本,并使用“webkit-line-clamp:lines”CSS属性将文本截断为多行。

img

以上就是使用 CSS 将文本长度限制设置为 N 行的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号