在 CSS 中使用 Display None
cssweb developmentfront end technology更新于 2025/1/14 1:48:17
CSS Display None 帮助开发者隐藏 display 属性设置为 none 的元素。对于 display 属性设置为 none 的元素,不会为其生成任何框,即使其子元素的 display 属性设置为 none 以外的值。
语法
以下是 CSS display none 的语法 −
Selector {
display: none;
}
示例
让我们看一个 CSS display none 的例子 −
<!DOCTYPE html>
<html>
<head>
<title>CSS Display None</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
box-sizing: border-box;
}
input[type="button"] {
border-radius: 10px;
}
.child{
display: inline-block;
height: 40px;
width: 40px;
color: white;
border: 4px solid black;
}
.child:nth-of-type(1){
background-color: #FF8A00;
}
.child:nth-of-type(2){
background-color: #F44336;
}
.child:nth-of-type(3){
background-color: #C303C3;
}
.child:nth-of-type(4){
background-color: #4CAF50;
}
.child:nth-of-type(5){
background-color: #03A9F4;
}
.child:nth-of-type(6){
background-color: #FEDC11;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS-Display-None</legend>
<div id="container">
<div class="child"></div><div class="child primary"></div><div class="child"></div><div class="child"></div><div class="child primary"></div><div class="child primary"></div>
</div><br>
<input type="button" value="Hide Primary Colors" onclick="displayNone()">
</fieldset>
</form>
<script>
var primaryColor = document.getElementsByClassName('primary');
function displayNone(){
for(var i=0; i<3; i++)
primaryColor[i].style.display = 'none';
}
</script>
</body>
</html>
输出
这将产生以下输出 −
点击"隐藏原色"按钮前 −

点击"隐藏原色"按钮后 −

示例
我们来看另一个 CSS Display None 的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
#flex {
display: flex;
}
#none {
display: none;
}
.inline-block {
display: inline-block;
background-color: mintcream;
}
.grid {
display: grid;
background-color: orange;
}
div {
margin: 50px;
padding: 10px;
height: 10px;
line-height: 5px;
text-align: center;
background-color: red;
border: 2px solid blue;
}
div > div {
background-color: yellow;
border: 2px solid green;
}
div > div > div {
background-color: sandybrown;
border: 4px solid darkred;
}
</style>
</head>
<body>
<div><span id="flex">AAAAAA</span>
<div><span id="none">BBBBBB</span>
<div>
<span class="inline-block">CCCCC</span>
<span class="inline-block">DDDDD</span>
<div>
<span class="grid">EEEE FFFF</span>
<span class="grid">GGGG HHHH</span>
</div>
</div>
</div>
</div>
</body>
</html>
输出
这将产生以下输出 −

相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

