Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 413
Hugo Avatar asked Feb 29 '16 10:02

Hugo


People also ask

What happens if id is not unique HTML?

IDs must be unique - the same ID should not be used more than once in the same page. Otherwise, they can interfere with the user agent's (e.g. web browser, assistive technology, search engine) ability to properly parse and interpret the page.

Does CSS id need to be unique?

The id global attribute defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

Do HTML IDs need to be unique?

The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.

Should you use IDs in CSS?

IDs have a much higher specificity than classes. A class on its own can't overwrite styles belonging to an ID. To "beat" the ID, you would need either more IDs or to use ! important , which can begin specificity wars in your stylesheets.


1 Answers

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>
like image 182
Alexis Avatar answered Oct 17 '22 19:10

Alexis