The present answers already address the subject well, I only complement those with some notes.
Summary
The priority of CSS properties in an element is treated as follows:
properties with the expression !important
, then those that are assigned in id
that have not been subscribed by the expression, and finally those in class
(s) that have not been subscribed by the expression or by the id
.
To exemplify
In CSS, the rule is that id
is a selector that has to be unique on the page, largely because of its JavaScript applications, which give it a precedence over any class
present in the same element as well as styles applied directly to the element's tag.
However, it should be noted that !important
exists to make a value of its 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
bad practice:
Using !important
is a bad practice because it makes debugging difficult because you break the natural cascade in your style sheets.
Combinations
We can also complete the answer with the question of selector combinations.
When we assign CSS properties to an element, we can assign the same via class
, id
, tag
, but also with a combination.
Here is an example of a combination with id
+ class
that prevails over the above rules, but does not prevail over 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 using id
I've got the idea that the general concept is that " id
should not be used for styles", this concept should be considered incorrect and I'll prove:
The use of id
for CSS styles is perfectly valid and has, for example, the same utility that JavaScript gives you, that is, to reach a specific element on the page.
If styles in id
were not valid, why would W3C have wasted time implementing hundreds of rules that bring numerous jobs to the browsers that implement them and the programmers who have to know them?
And the answer can be found in the documentation:
The id
attribute has several HTML roles:
As a selector for style sheet .
As a anchor for hypertext links.
As a way of referring to a particular element of a script >.
As the name of a declared OBJECT
element.
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.)