我主要从 W3Schools 学习了 HTML/CSS 几天。
您能否解释(或提供引用)为什么 div 有黑色背景,而不是橙色?谢谢。
<!DOCTYPE html>
<html>
<head>
<style>
div#hello { background-color: black; }
div { background-color: orange; }
</style>
</head>
<body>
<div id="hello" style="height: 50px; width:50px" ></div>
</body>
</html>
最佳答案
这称为选择器特异性。你的目标是 div#hello,更具体。 div#hello 下面的 div 选择器是一种更通用的样式,但您可以用更具体的 div#hello 样式覆盖 div 样式。
虽然div选择器出现在div#hello样式的下方,但由于特殊性,div#hello选择器被取而代之。
关于html - 使用#id 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30879298/