Is there a difference between using inline CSS or IDs for a large number of different elements?

1

In the page I'm doing I'm using a Spritesheet with more than 50 different images, however all are in the same .png file and I need to pass the position where each element is in that background.

Seeing a lot of elements with unique styles, what I'd rather do:

A) Have the style of each element with inline CSS <div style="background-position: 30px 20px;"> direct in HTML. or B) Have an ID for each element <div id="imagem_47"> and add the style of each element in the% css% CSS file.

Is there any difference in performance or compatibility between the two options? Or is it just a matter of taste and organization?

    
asked by anonymous 22.10.2015 / 04:18

2 answers

3

As the Weslley Tavares mentioned, the performance turns out to be better when using inline CSS (which is set the style attribute directly in the tag). But, unless you're using a framework that manages these attributes for you (more or less what the react.js does), or that you've written a server-side application that makes it easier, I very much doubt that this performance gain is worth it, even though it's practically nil.

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 gives you a huge gain in performance, and the handling of those classes - and HTML itself - for maintenance and scalability will be much easier once you have it in a separate file. This is the famous decoupling . Here you can read a rather interesting article on this.

Another thing that is nice to mention, although you have not asked, is the difference in performance between the use of IDs and classes. I already gave an answer about it here , where, I show that there is no difference (in terms of gain, you see), unless its application is extremely large. So when to that, you do not have to worry.

Another concern you should, yes, have, is about specificity. Here's an example:

div{
  width: 100px;
  height: 100px;
}

#testeID{
  background: red;  
}

.testeClasse{
  background: green;  
}
<div id="testeID" class="testeClasse"></div>

See 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>

Notice that the inline rule now overrides the ID rule. Quite simply, specificity works like this.

  

tag rule < class < ID < style inline

In that other answer , I'll say more a bit about specificity, which can bring you a light when choosing how to build your code.

    
22.10.2015 / 16:00
2

Well, when using the option to create a CSS file, what prevents you from creating classes instead of going directly to the component via ID? If your application changes (whether due to scalability or maintenance) this process would be much less painful.

Another thing, however different the components may be, in the general context, they may have some compatibility between one attribute and another, which could already be reused through the classes defined in CSS. Anyway, this is a project management thing (and good practices).

Returning to the issue of performance, inline is better, since the page will not have to perform the GET to fetch the CSS file, however, this inline gain to CSS is still not enough to compare with the gain obtained in its coding when using CSS.

I hope I have helped.

    
22.10.2015 / 04:39