如果我有多个具有相同 ID 的 div,我对 css 的类和 ID 选择感到困惑,将 ID 更改为 Class 对结果没有任何影响
风格
<style>
#main-div
{
width:300px;
height:300px;
background-color:red;
margin:10px;
}
</style>
<div id="main-div"></div>
<div id="main-div"></div>
<div id="main-div"></div>
<div id="main-div"></div>
最佳答案
首先,多次使用同一个id是无效的。永远不要这样做。
然后,在css选择器中,你需要使用
- “任何”目标类(class)
- “#whatever”以定位 ID。
这是一个例子。
<!-- in HTML -->
<div id="identifier1"></div>
<div class="class1"></div>
<div class="class1"></div>
/* In css */
#identifier1 { /* This targets the first div */ };
.class1 { /* This targets the second and third div */ };
关于html - 具有相同 id 的多个 div 应用 css 样式与类一样工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937756/