具有相同id的多个div应用css风格的工作方式与类相同?

问题描述:

如果我有多个具有相同ID更改ID的div,则对css的类和id选择感到困惑。类对结果没有任何影响。

I'm confused about the class and id selection with css if i have multiple divs with same ID changeing ID to Class does not have any effect on the result

style

        <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无效。不要这样做。

First of all, using multiple times the same id is invalid. Never do it.

然后,在css选择器中,您需要使用

Then, in css selectors, you need to use



这是一个例子。

<!-- 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 */ };