Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are some CSS styles more "expensive" than others?

Imagine I have this setup:

<div class="dialog">
    <div class="toolbar first">Bla</div>
    <div class="toolbar">Yada</div>
</div>

and this style definition:

.toolbar { background-color: red; }

I actually want a small 2 pixel border between the 2 "toolbars", so I see 2 general possibilities,

1) add a background colour to the "dialog" div and a margin to the first "toolbar":

.dialog { background-color: #fff }
.toolbar.first { margin-bottom: 2px; }

2) add a 2px border to the first toolbar:

.toolbar.first { border-bottom: 2px solid #fff }

Is there any difference in terms of "cost" of rendering/applying? Which is more desirable to that extent?

I know this is a very small example and it might make no real difference, but imagine a page with tens of these dialogs (dialogues?).

like image 580
Sergi Papaseit Avatar asked Mar 23 '11 12:03

Sergi Papaseit


People also ask

Does order of CSS files matter?

CSS Order Matters In CSS, the order in which we specify our rules matters. If a rule from the same style sheet, with the same level of specificity exists, the rule that is declared last in the CSS document will be the one that is applied.

Does CSS affect performance?

Before we get into what inlining CSS means and how to do it, it's important to first understand how CSS can affect performance. While JavaScript and images generally play a much larger role in negatively impacting performance metrics, CSS can also play a significant role.

Is CSS just for styling?

CSS can be used for very basic document text styling — for example, for changing the color and size of headings and links. It can be used to create a layout — for example, turning a single column of text into a layout with a main content area and a sidebar for related information.

When should inline CSS be used?

Since inline styling takes precedence over all other styles, one of the best times to use it is when testing or previewing changes and performing quick fixes on your website.


2 Answers

What a timely question!

I read this excellent article a couple of days ago about performance for CSS. Granted that CSS selector performance is tiny compared to the effort expended on serving an entire page, but it's still something to keep in mind.

http://css-tricks.com/efficiently-rendering-css/

EDIT

I didn't notice that the question was about CSS rules and not selectors. Trying to answer that question now.

Like I said in my original answer, CSS performance is the area you'll be able to gain the least amount of performance in your system (generally, unless you're using things like filters), and should be addressed last if you want to micro tune your site. It's better to keep it readable and work on the other parts of your site first.

like image 179
JohnP Avatar answered Sep 23 '22 13:09

JohnP


I think we'll see real difference in performance only with tens of thousands of elements otherwise it'll stay in the millisecond margin.

So my advice it to stick with the most readable/simple way which is probably adding the "direct" border to the first toolbar. :)

like image 29
Shadow Wizard Hates Omicron Avatar answered Sep 21 '22 13:09

Shadow Wizard Hates Omicron