首页 前端知识 CSS实现文本显示两行(或者多行),超过部分用省略号隐藏

CSS实现文本显示两行(或者多行),超过部分用省略号隐藏

2024-06-10 11:06:57 前端知识 前端哥 216 192 我要收藏

 文本过长只显示两行,最后一行超出隐藏并显示省略号

 效果如图:

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</div>
</body>
<style>
    div{
        width: 200px;
        overflow:hidden;
        word-wrap: break-word;
        text-overflow:ellipsis;
        display:-webkit-box;
        -webkit-box-orient:vertical;
        -webkit-line-clamp:2;   
    }
</style>
</html>

 -webkit-line-clamp:2;  可以控制显示多少行。

如果要求显示一行有简单的写法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</div>
</body>
<style>
    div{
        width: 200px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap; 
    }
</style>
</html>

转载请注明出处或者链接地址:https://www.qianduange.cn//article/11762.html
标签
评论
发布的文章

第一次实验补充

2024-06-18 09:06:14

html5shiv,从入门到深入

2024-05-09 10:05:07

html5基础入门

2024-06-18 09:06:07

HTML5学习简记

2024-06-18 09:06:20

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!