css中元素的显示与隐藏
1.display属性
display:none;【隐藏元素】
未加display属性时:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 200px;
background-color: pink;
}
.div2 {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="div1">xxx</div>
<div class="div2">xxx</div>
</body>
</html>
运行结果:
加了display:none;属性后:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 200px;
background-color: pink;
display: none;
}
.div2 {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="div1">xxx</div>
<div class="div2">xxx</div>
</body>
</html>
运行结果:
把display:none;改成display:block后:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 200px;
background-color: pink;
display: block;
}
.div2 {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="div1">xxx</div>
<div class="div2">xxx</div>
</body>
</html>
运行结果:
2.visibility属性
visibility:hidden;【元素隐藏】
visibility:visible【元素显示】
当我们给元素添加 visibility:hidden;
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 200px;
background-color: pink;
visibility: hidden;
}
.div2 {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="div1">xxx</div>
<div class="div2">xxx</div>
</body>
</html>
运行结果:
我们把display:hidden 改成 display:visible;
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 200px;
background-color: pink;
visibility: visible;
}
.div2 {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="div1">xxx</div>
<div class="div2">xxx</div>
</body>
</html>
运行结果:
3.overflow溢出
visible:元素完全显示
hidden:元素超出的部分剪切掉
scroll:元素所在盒子会一直有滚动条
auto:根据元素内容来确定是否需要滚动条
先来看看overflow:vivible;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 100px;
height: 100px;
border:1px solid black;
margin:100px auto;
overflow: visible;
}
</style>
</head>
<body>
<div class="div">为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!</div>
</body>
</html>
运行结果如下:
接下来看overflow:hidden;属性:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 100px;
height: 100px;
border:1px solid black;
margin:100px auto;
overflow: hidden;
}
</style>
</head>
<body>
<div class="div">为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!</div>
</body>
</html>
运行结果:
接下来看overflow:scroll;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 100px;
height: 100px;
border:1px solid black;
margin:100px auto;
overflow: scroll;
}
</style>
</head>
<body>
<div class="div">为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!为中华之崛起而读书!!!</div>
</body>
</html>
运行结果:
文字很少的情况滚动条会不会在呢?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 100px;
height: 100px;
border:1px solid black;
margin:100px auto;
overflow: scroll;
}
</style>
</head>
<body>
<div class="div">为中华之崛起而读书!</div>
</body>
</html>
运行结果:
最后看overflow:auto;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 100px;
height: 100px;
border:1px solid black;
margin:100px auto;
overflow: auto;
}
</style>
</head>
<body>
<div class="div">为中华之崛起而读书!</div>
</body>
</html>
运行结果如下:
文字多时:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div {
width: 100px;
height: 100px;
border:1px solid black;
margin:100px auto;
overflow: auto;
}
</style>
</head>
<body>
<div class="div">为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!为中华之崛起而读书!</div>
</body>
</html>
运行结果:
总结:
元素的显示与隐藏总共有三种属性:
- display
- visibility
- overflow
注意display:none;元素隐藏位置不占用
而
visibility:hidden;元素隐藏位置仍占用着
还没有评论,来说两句吧...