Like the Weslley Tavares mentioned, the performance turns out to be better when using CSS inline (which is to define the attribute style
directly in the tag). But, unless you are using a framework that manages these attributes for you (more or less than the React.js does), or that you have written a server-side application that facilitates this, I doubt very much that this performance gain is worth, even because it is practically null.
Based on the assumption that you do this manually, I believe that the most advantageous is the use of classes. The use of sprites only already brings you a huge performance gain, and the handling of these classes - and the HTML itself - in view of maintenance and scalability will be much easier, once you have it in a separate file. This is the famous decoupling. Here you can read an article (in English) quite interesting about it.
Another nice thing to mention, although you haven’t asked, is the difference in performance between using Ids and classes. I have already given an answer on that here, where, basically, I show that there is no difference (in terms of gain, see well), unless its application is extremely large. So when to that, you don’t have to worry.
Another concern you should, yes, have is with regard to specificity. Take this example:
div{
width: 100px;
height: 100px;
}
#testeID{
background: red;
}
.testeClasse{
background: green;
}
<div id="testeID" class="testeClasse"></div>
Note that the ID is more specific than the class, that is, its rules will be those that will be applied in case of conflict. If I do
div{
width: 100px;
height: 100px;
}
#testeID{
background: red;
}
.testeClasse{
background: green;
}
<div id="testeID" class="testeClasse" style="background: blue"></div>
see that, now, the rule inline overwritten the ID rule. Quite softly, the specificity works like this
tag rule < class < ID < style inline
In that other answer, i talk a little more about specificity, which can bring you a light when choosing how to build your code.
Very enlightening answer, Caio Pereira. I believe that following your advice, the author of the question can have a more general view of the context.
– Weslley Tavares
Thanks Caio, great explanation and examples. I’m already seeing several ways to keep my code cleaner and readable, and how doing these styles in a CSS file looks the best option in this case will greatly help in optimizing the code for maintenance. And if you end up helping someone, I found a very good article about good practices for CSS: http://www.innofied.com/25-css-best-practices-we-follow-at-innofied/
– André Ferraz