In creating CSS styles one approach seems to be a fully qualified style such as
#pnlImage div.ipd-imageInfo div.ipd-tags span.ipd-tag
compared to a shorter definition such as
div.ipd-tags span.ipd-tag
which would uniquely identify the style as well. However, if the site is expanded or changed the 2nd style runs the risk of not uniquely identifying the element.
Is there a performance hit from fully qualifying a style, i.e., it is longer? Are there some guidelines/best practice references for style naming?
Thanks
CSSSelector Locator CSS Selector is best option if web element has no ID and name. CSS is faster than XPath.
In other words, an important rule can be used to override other styling rules in CSS. Although easier than following the many rules of specificity when combining property values, using the ! important property is considered a bad practice.
Inline styles have the highest priority over the external style and external styles. Inline styles take priority over embedded styles and external styles. Inline-styles are applied directly to an element via the style attribute.
Google (not a search, actually them) seems to think that it does cause a performance hit.
Also, the Mozilla foundation has an article "Writing Efficient CSS for use in the Mozilla UI" that outlines the possible performance pitfalls of CSS selectors in their browser(s), and how to optimize your style rules for their rendering engine. Their article has tons of examples of what's good and what's bad. Please keep in mind this is only relevant to their browsers, though.
There are also some benchmarks publicly available, about CSS selectors affect on rendering speeds:
I, however, think this is, for the most part, horse manure. You can effect FAR greater change on your page's loading/rendering speed by using some other simple optimizations. I believe that your sanity, and a well-organized code base should come first. If this were as big of a deal as some make it out to be, we'd all be back using HTML < 4 attributes (bgcolor=, border=, etc) to style our web pages.
#id
is fast.tag
is a bit slower..class
is the slowest.Starting your selectors with a faster lookup will reduce the number of lookups required for then next part. That is, if I wrote p.myClass
, then the browser can very quickly find all the p
tags and then it only has to loop through those to check for the class name.
That said, it would rate the maintainability of your CSS file higher than its rendering speed. Blah blah blah premature optimisation blah blah.
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