CSS轻松实现Tedesign dashbord的自适应块样式

January 20,2022

用Tdesign写管理站的时候需要几个block并列排列,中间有间隙,并且自适应屏幕宽度。一排可以是2个、4个...n个。

样式如下:

image.png

间隙用blockWapperpadding和外部wappermargin:[负值]解决,

自适应用flex的flex: 0 0 50%;解决,50%可以是任意值,看你想要一行排几个。

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>dashbord</title>
    <style>
        body,
        html {
            padding: 0;
            margin: 0;
        }
        section,
        div {
            box-sizing: border-box;
        }
        section {
            padding: 24px;
            background-color: #eeeeee;
        }
        .wapper {
            display: flex;
            flex-wrap: wrap;
            margin: -8px;
        }
        .block-wapper-4 {
            width: 25%;
            flex: 0 0 25%;
            padding: 8px;
        }
        .block-wapper-2 {
            width: 50%;
            flex: 0 0 50%;
            padding: 8px;
        }
        .block {
            border-radius: 3px;
            height: 70px;
            background-color: #0052d9;
        }
    </style>
</head>
<body>
    <section>
        <div class="wapper">
            <div class="block-wapper-4">
                <div class="block"></div>
            </div>
            <div class="block-wapper-4">
                <div class="block"></div>
            </div>
            <div class="block-wapper-4">
                <div class="block"></div>
            </div>
            <div class="block-wapper-4">
                <div class="block"></div>
            </div>
        </div>
    </section>
    <div class="gap" style="height: 100px;"></div>
    <section>
        <div class="wapper">
            <div class="block-wapper-2">
                <div class="block"></div>
            </div>
            <div class="block-wapper-2">
                <div class="block"></div>
            </div>
            <div class="block-wapper-2">
                <div class="block"></div>
            </div>
            <div class="block-wapper-2">
                <div class="block"></div>
            </div>
        </div>
    </section>
</body>
</html>

image.png

请点击查看演示:https://codesandbox.io/s/nervous-maxwell-2c2t4?file=/index.html

上一篇 下一篇
Comments
Write a Comment