元素本身隐藏
- 场景:让某元素本身在屏幕中不可见。如:鼠标:hover之后元素隐藏
- 常见属性:
- visibility:hidden
- display:none
- 区别:
- visibility:hidden 隐藏元素本身,并且在网页中占位置
- display:none 隐藏元素本身,并且在网页中不占位置
- 使用 visibility:hidden 隐藏蓝色方块
.blue {
height: 100px;
width: 100px;
background-color: blue;
visibility: hidden;
}
.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;
display: none;
}
.father:hover .son {
display: block;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>