CSS的幽灵节点和!important的权重
1、正常操作(传说的幽灵节点)
<!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>
* {
margin: 0;
padding: 0;
}
span {
display: inline-block;
}
div {
background: red;
}
</style>
</head>
<body>
<div>
<span></span>
</div>
</body>
</html>
易错点:
以上代码大家都会以为div没有宽高的,其实并不是
解决办法:
div {
background: red;
font-size: 0;
line-height: 0;
}
二、!important权重不是最大
正常操作:
<!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: 500px !important;
height: 500px !important;
background-color: aquamarine;
}
</style>
</head>
<body>
<div>a</div>
</body>
</html>
但加入min-width和min-height后
<!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: 500px !important;
height: 500px !important;
min-width: 1000px;
min-height: 1000px;
background-color: aquamarine;
}
</style>
</head>
<body>
<div>a</div>
</body>
</html>