CSS 显示与隐藏
2018-12-12 本文已影响16人
roy_pub
display
- display: none; 隐藏,不保留物理空间
- display: block;显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
/* none,隐藏某一个元素,不保留位置 block,显示某一个元素*/
display: none;
}
p {
width: 300px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div></div>
<p>Hello World</p>
</body>
</html>
visibility
与display不同,隐藏对象保留占据的物理空间,如果希望对象可视,其父对象也必须是可视的。
参数 | 说明 |
---|---|
inherit | 继承一个父对象的可见性 |
visible | 对象可视 |
hidden | 对象隐藏 |
collapse | 隐藏表格的行和列 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
visibility: hidden;
}
p {
width: 300px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div></div>
<p>Hello World</p>
</body>
</html>
Demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a {
display: block;
width: 448px;
height: 252px;
position: relative;
}
.mask {
width: 100%;
height: 100%;
background: rgba(0,0,0,0.4) url("images/arr.png") no-repeat center;
position: absolute;
top: 0;
left: 0;
display: none;
}
a:hover .mask{
display: block;
}
</style>
</head>
<body>
<a href="">
<img src="images/tudou.jpg" height="252" width="448"/>
<div class="mask"></div>
</a>
</body>
</html>