天天看點

單行和多行文本省略号如何實作

<!DOCTYPE html>
<html >

<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>單行文本省略号如何實作</title>
    <style>
        .box1 {
            width: 200px;
            height: 23px;
            margin: 100px auto;
            color: red;
            background-color: green;
            /* 強制文本一行顯示 white-space: normal;預設文本自動換行*/
            white-space: nowrap;
            /* 溢出隐藏 */
            overflow: hidden;
            /* 文本溢出省略号 */
            text-overflow: ellipsis;
        }

        .box2 {
            width: 200px;
            height: 45px;
            margin: 0 auto;
            color: red;
            background-color: green;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            /* 第二行開始省略,這個可以随意設定,第三行四行開始出現省略号都可以 */
            -webkit-line-clamp: 2;
            /* 溢出部分隐藏 */
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="box1">牆角數枝梅,淩寒獨自開。春江花月夜</div>
    <div class="box2">
        牆角數枝梅,淩寒獨自開。春江花月夜牆角數枝梅,wa他
    </div>
</body>

</html>