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?
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.
Chaining CSS classes means composing the desired look by adding granular modifiers together onto an HTML selector.
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).
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.
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/
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 */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With