Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS chaining performance

I don't know if chaining is the right term.
I talk about coding like this:

    html > body > div > ul > li > a{
        /*css*/
    }

I talked with a friend of mine who told me that coding css like this is giving low perfomance on the browser when rendering the css.
So is this true or I really should declarate only classes/id's and specify on them?

like image 827
Paul Reed Avatar asked Apr 16 '13 08:04

Paul Reed


People also ask

Are attribute selectors slow?

The table outlines that attribute selectors are approximately 3x slower compared to standard classes. Relying on attribute selectors also requires more characters to target an element. It's better to define short and concise class names to keep your stylesheet small.

What is CSS chaining?

Chaining CSS classes means composing the desired look by adding granular modifiers together onto an HTML selector.

What is @extend in CSS?

The @extend directive lets you share a set of CSS properties from one selector to another. The @extend directive is useful if you have almost identically styled elements that only differ in some small details. The following Sass example first creates a basic style for buttons (this style will be used for most buttons).

Can I extend a CSS class?

Extending. If you don't want to chain classes, you can extend them. We still have the same separate blocks, but instead of chaining them in the HTML, we inherit the properties of the base class to its modifiers. This way, we can use them all at once.


2 Answers

You should be only as specific as you have to be in order to target your elements. Using id is preferrable.

Ultimately though -this sort of micro optimisation is fairly irrelevant. Only worth looking at once you have optimised pretty much everything else (compressing images, serving of static content, using css sprites, minimising assets etc.)

http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/

like image 142
calumbrodie Avatar answered Oct 31 '22 00:10

calumbrodie


In my opinion you should use the least number of selection rules required. The more rules the longer it takes to process.

So yes it is better practice to use classes and IDs.

Also the example you gave above, if you are trying to apply the CSS to all tags it will work fine with just

a {
/* css */
}
like image 27
Nick Barrett Avatar answered Oct 30 '22 22:10

Nick Barrett