---
comments: true
date: 2012-05-15 19:35:38
layout: post
slug: keep-your-css-selectors-short
title: Keep your CSS selectors short
wordpress_id: 3649
categories:
- Web Development
tag:
- CSS
- CSS Selectors
- Front-end architecture
- OOCSS
- Performance
---
One thing I believe, as a very, _very_ general rule of thumb, is that as sites get bigger, selectors should get shorter.
By this I mean that if you want to create extensible and maintainable, flexible and predictable websites, you should really take care to make your CSS selectors as dev-friendly as possible; i.e. short.
Keeping CSS selectors short helps with a lot of things:
* Increases selector efficiency
* Reduces location dependency
* Increases portability
* Reduces chances of selector breakage
* Decreases specificity
* Can make code more forgiving
This is a very vague list, so I’m going to address each in order. You will find that there is a lot of crossover between each point (e.g. reducing location dependency inherently means your selectors are more portable) but I feel they are all points in their own right.
## Increases Selector Efficiency
I have written before about CSS selector efficiency. I’m going to gloss over a lot of the intricacies in this post so for a full background understanding I recommend you read [Writing efficient CSS selectors](/2011/09/writing-efficient-css-selectors/) first.
If we ignore actual types of selector (`* {}` is typically the slowest, depending on how it’s being used, IDs are the fastest followed by classes, descendants are comparably quite slow followed by pseudo-selectors) then _in general_ it is safe to say that shorter selectors are faster.
This stands to reason, if we compare these two selectors:
html body .header .nav {}
.nav {}
There we can see pretty clearly that in the first example, the browser has to look out for four things, the `.nav` class, then the `.header` class, then the `body` element and then, finally, the `html` element (browsers read selectors right-to-left).
With the second example the browser only needs to look for one thing; the `.nav` class. The browser has _four times_ less work to do to match that selector. Every time you write a selector try and trim as much losable stuff from it as possible. Instead of `ul.nav {}` (two checks) write `.nav {}` (one check). Instead of `.nav li a {}` (three) write `.nav a {}` (two).
Now, [CSS selector performance is—by-and-large—not something we _really_ need to worry about any more](http://calendar.perfplanet.com/2011/css-selector-performance-has-changed-for-the-better/), but that doesn’t mean we should be wasteful. I’m sure none of us would miss a lost £5 but that doesn’t mean we go slipping banknotes into paper shredders… Selector efficiency _does_ exist and you might as well improve it where you **very easily** can.
## Reduces Location Dependency
By keeping selectors short you are likely to be reducing the amount of descendant (e.g. `.sidebar .promo {}`) and child (e.g. `.sidebar > .promo {}`) selectors. By removing these descending types of selectors you are reducing the necessity for an element to live inside another one. Let’s reuse the `.sidebar .promo {}` example…
By having a selector like `.sidebar .promo {}` we are saying we want to target any promotional item that lives in an element with the class of `.sidebar`. This means that we are tied to always using that styling inside a certain element; we have a dependency on location.
By replacing `.sidebar .promo {}` with something like `.secondary-promo {}` we can now place the element in question _anywhere_ we wish. In the sidebar—as before—but now also in the footer, or in the header, or after an article.
By reducing descendants we can really reduce dependency and make things a lot more portable…
## Increases Portability
So now that we’re not tied to locationally dependant selectors, we find that our components are a lot more portable. We can move things a lot more easily because our CSS doesn’t care where a thing lives, it just cares that it exists. Awesome!
Another way to increase portability is to not qualify selectors. A qualified selector is one like `ul.nav {}` or `a.button {}` or `div.content {}`.
Qualified selectors are bad because they reduce efficiency (more checks than we really need) but—more importantly—because they tie us to specific elements. We can’t now use that `.button` class on an `` or a `