当div元素没有内容时,显示提示内容

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .div_wrap {
            width: 100vw;
            height: 100vh;
            display: grid;
            /* place-items 属性是以下属性的简写:align-items、justify-items */
            place-items: center;
        }

        /* 当div元素没有内容时,.div_wrap:empty样式会生效,同时为了显示一段提示内容,使用了伪元素::before,在content写入提示内容。 */
        .div_wrap:empty::before {
            content: 'div内容为空';
            color: gray;
        }
    </style>
</head>

<body>
    <div class="div_wrap"></div>
</body>

</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容