html - 具有相同 id 的多个 div 应用 css 样式与类一样工作吗?

标签 html css

如果我有多个具有相同 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/

相关文章:

jquery - 如何包装除具有 .header-container-wrapper 类的一个 div 之外的所有 div?

Javascript 通过 parent ID 选择 child

javascript - 在后台播放音频 (Windows 8)

html - 图片内的文章

html - 匹配 CSS 直到发生另一个匹配

javascript - jquery根据图像的宽度将css类应用于图像

javascript - 是否可以执行异步跨域文件上传?

html - float div的宽度

html - 如何删除影子根(用户代理)

javascript - 在 javascript 中逐步显示 4 个图像