元素本身隐藏

发布于:2022-08-06 ⋅ 阅读:(339) ⋅ 点赞:(0)

元素本身隐藏

  • 场景:让某元素本身在屏幕中不可见。如:鼠标:hover之后元素隐藏
  • 常见属性:
    • visibility:hidden
    • display:none
  • 区别:
    • visibility:hidden 隐藏元素本身,并且在网页中占位置
    • display:none 隐藏元素本身,并且在网页中不占位置


  • 此图为三个div

在这里插入图片描述

  • 使用 visibility:hidden 隐藏蓝色方块
.blue    {
            height: 100px;
            width: 100px;
            background-color: blue;
            
            visibility: hidden;
        }

在这里插入图片描述

  • 使用 display:none 隐藏蓝色方块
.blue    {
            height: 100px;
            width: 100px;
            background-color: blue;
            
            display: none;
        }

在这里插入图片描述


案例:默认son元素隐藏,当鼠标移入father后让son显示

在这里插入图片描述

<!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>
        .father {
            height: 300px;
            width: 300px;
            background-color: pink;
        }
        .son {
            width: 100px;
            height: 100px;
            background-color: blue;
            /* 隐藏son */
            display: none;
        }
        /* 使用后代选择器,鼠标停留在father内 此时son显示,鼠标离开father时son隐藏 */
        .father:hover .son {
            display: block;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

网站公告

今日签到

点亮在社区的每一天
去签到