user5895938
user5895938

Reputation:

If IDs in HTML should be unique, why sometimes I see in Css see something like div#nav-blue?

Since an ID should be unique in HTML, why sometimes i see in Css selectors formatted like (div#nav-blue), since it's obvious that there will be no other element having this ID exccept for that div, so aint writting #nav-blue makes more sense?

Upvotes: 7

Views: 473

Answers (1)

Alexis
Alexis

Reputation: 5831

It does no change or a little.

You can do this for some reason : more visibility when you maintain your code. Easier to find, and remember for each kind of element is the style.

The second reason is the priority of selector.

There is some different order of priority :

!important > #id > .class > element

you can consider that

 element = 1
 .class = 10
 #id = 100
 !important= 1000

And then div#id = 101 > #id = 100

div#myid{
  color:red;
}

#myid{
  color:blue;
}

.myclass{
  color:yellow;
}

div{
  color:green;
}
<div class="myclass" id="myid">
Some text
</div>

Upvotes: 4

Related Questions