The answers already address the subject well, I just complement the existing ones with some notes.
Summary
The priority of CSS properties in an element is treated as follows:
Properties prevail with the expression !important
, after those assigned to the id
which have not been subscribed by the expression and finally those assigned in class
(s) which have not been subscribed by the id
.
To exemplify
In CSS, the rule is that id
is a selector that has to be unique on the page, much due to its Javascript applications, reasons which provide the same a precedence over any class
present in the same element as well as styles applied directly to the tag of that element.
However, it should be noted that !important
exists to make a value of the respective CSS property more important than all others, regardless of id
or class
:
div{ /* estilo na tag */
color:green;
}
.blue{ /* estilo via class */
color:blue;
}
#red{ /* estilo via id */
color:red;
}
.grey{ /* estilo expresso como IMPORTANTE */
color:grey!important;
}
<div>A minha cor via TAG</div>
<div class="blue">A minha cor via CLASS</div>
<div id="red">A minha cor via ID</div>
<div id="red" class="blue">A minha cor via ID + CLASS</div>
<div id="red" class="blue grey">A minha cor via ID + CLASS + !important</div>
<div id="red" class="grey blue">A minha cor via ID + !important + CLASS</div>
Note that the Mozilla Developer Network considers the use of !important
malpractice:
Using !important
is a bad practice because it makes debugging difficult as you break the natural cascade in your style sheets.
Combinations
We can also complete the answer with the question of selector combinations.
By assigning CSS properties to an element, we can assign them via class
, id
, tag
, but also with a combination.
Here’s an example of a combination with id
+ class
which prevails over the above-mentioned rules, but does not take precedence over the expression !important
:
div{ /* estilo na tag */
color:green;
}
.blue{ /* estilo via class */
color:blue;
}
#red{ /* estilo via id */
color:red;
}
.grey{ /* estilo expresso como IMPORTANTE */
color:grey!important;
}
#red.bubu { /* combinação prevalece sobre as regras de ID */
color:black; /* ou class com excepção do que tem a expressão IMPORTANTE */
}
<div>A minha cor via TAG</div>
<div class="blue">A minha cor via CLASS</div>
<div id="red">A minha cor via ID</div>
<div id="red" class="blue">A minha cor via ID + CLASS</div>
<div id="red" class="blue grey">A minha cor via ID + CLASS + !important</div>
<div id="red" class="grey blue">A minha cor via ID + !important + CLASS</div>
<div id="red" class="blue bubu">
<small>A minha cor via ID + CLASS deveria dar vermelho, mas foi subscrito pela combinação "#red.bubu" para preto.</small>
</div>
Disambiguation about the use of id
I got the idea that the general concept is: "a id
should not be used for styles", such concept should be considered incorrect and step to prove:
The use of id
for styles in CSS is perfectly valid and has for example the same utility that Javascript gives you, ie achieve a specific element on the page.
If the styles in the id
were not valid, because W3C would have wasted time implementing hundreds of rules that bring numerous jobs to the browsers that implement them and to the programmers who have to know them?
And the answer can be found in the documentation:
The attribute id
has several roles in HTML:
As a selector of style sheet.
As a anchor target for hypertext links.
As a way of referring to a particular element of a script.
As the name of an element OBJECT
avowed.
For general purpose processing by user agents (for example, for identifying fields when extracting data from HTML pages in a database, translating HTML documents into other formats, etc.).
You ask what was declared first in CSS?
– Maniero
No. I wonder what HTML will accept first.
– Felipe Viero Goulart
I do not know if I understand, HTML does not have to accept anything, even because it accepts both. I will try to answer what I think is possible to answer. If you don’t understand, you comment and try to improve.
– Maniero