Ashiqullah Sahibzai
Ashiqullah Sahibzai

Reputation: 166

Multiple divs with the same id applying css style works same like class?

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>

Upvotes: 1

Views: 2115

Answers (1)

Eloims
Eloims

Reputation: 5224

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

Then, in css selectors, you need to use

  • ".whatever" to target classes
  • "#whatever" to target ids.

Here is an example.

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

Upvotes: 1

Related Questions