天天看點

css 實作多行展開收起自動隐藏(全相容)

css 實作多行展開收起自動隐藏(全相容)

學習筆記

<!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>
    <style>
        .flex {
            display: flex;
        }
        input {
            display: none;
        }
        .box {
            /* display: -webkit-box;
            -webkit-line-clamp: 2;
            text-overflow: ellipsis;
            -webkit-box-orient: vertical; */
            max-height: 40px;
            overflow: hidden;
            line-height: 20px;
            position: relative;
            transition: .3s max-height;
        }
        .box::after {
            content: '...';
            width: 999vw;
            height: 999vw;
            position: absolute;
            box-shadow: inset calc(100px - 999vw) calc(30px - 999vw) 0 0 #fff;
            margin-left: -100px;
        }
        input:checked + .box::after {
            visibility: hidden;
        }
        .box::before {
            content: '';
            width: 0;
            height: 100%;
            margin-bottom: -20px;
            float: right;
        }
        .box .btn {
            float: right;
            background: blue;
            color: #fff;
            height: 20px;
            border: none;
            clear: both;
            position: relative;
            margin-left: 22px;
        }
        .box .btn::before {
            content: '...';
            position: absolute;
            line-height: 20px;
            left: -8px;
            color: #000;
            transform: translateX(-100%);
        }
        .box .btn::after {
            content: '展開';
            display: block;
            padding: 0 5px;
            height: 20px;
            background-color: aqua;
            line-height: 20px;
        }
        input:checked + .box {
            max-height: 100px;
        }
        input:checked + .box .btn::after {
            content: '收起';
        }
        input:checked + .box .btn::before {
            visibility: hidden;
        }
    </style>
</head>
<body>
    <div class="flex">
        <input type="checkbox" id="btn">
        <div class="box">
            <label class="btn" for="btn"></label>我是純css實作的收起展開功能我是純css實作的收起展開功能我是純css實作的收起展開功能我是純css實作的收起展開功能我是純css實作的收起展開功能我是純css實作的收起展開功能
        </div>
    </div>
</body>
</html>