---
layout: post
title: "Improving Your CSS with Parker"
date: 2016-06-01 10:23:16
categories: Web Development
meta: "Using static analysis to learn where to improve your CSS"
lux: Post
---
[Parker](https://github.com/katiefenn/parker/) is an absolutely fantastic,
beautifully simple static analysis tool that gives you some very insightful
metrics about your CSS files. Parker is built and maintained by [Katie
Fenn](http://www.katiefenn.co.uk/), a developer from England who has since
become a friend.
I use Parker almost daily, and regularly go through it with my clients and
workshop attendees. Parker surfaces some really interesting numbers, and if you
know what they represent, you can draw some really valuable insights about your
CSS from them.
(As an aside, I have a pretty cool story about Parker:
I was delivering a workshop for a company in the UK a year or two ago, when a
woman in attendance asked me Do you ever use Parker?
My answer was an
enthusiastic Yes! I use Parker all the time! Do you?!
, to which she
replied Yeah… I built it.
That was how/when I met Katie.)
- - -
Okay. Let’s go.
If you haven’t got it installed already, you’ll want to do that first. Parker
(`parker`) is a command line tool, which can be installed (`install`) globally
(`-g`) using npm (`npm`):
```
$ npm install -g parker
```
You can then run Parker against a compiled stylesheet like so:
```
$ parker path/to/stylesheet.css
```
I’d recommend running your current project’s CSS file through Parker right now.
If you don’t have a stylesheet to hand, [this
one](https://github.com/csswizardry/discovr/blob/67d21cd6948f439462357126d1fb85ee875eb57f/css/main.css)
will work pretty well. That one will give you some output like this:
```
Total Stylesheets: 1
Total Stylesheet Size: 37178
Total Rules: 260
Total Selectors: 365
Total Identifiers: 487
Total Declarations: 522
Selectors Per Rule: 1.4038461538461537
Identifiers Per Selector: 1.6821917808219178
Specificity Per Selector: 9.293150684931506
Top Selector Specificity: 30
Top Selector Specificity Selector: .c-score[data-score^="0."]
Total Id Selectors: 0
Total Unique Colors: 25
Unique Colors: #FFFF00,#000000,#C0C0C0,#F3F3F3,#333333,#378BB5,#FFFFFF,#666666,#E4E4E4,#FF2100,#FF4200,#FF6300,#FF8400,#CC9E00,#999600,#668F00,#338700,#317CA1,#CC0000,#ABC123,#98AB1F,#999999,#CCCCCC,#FFFF88,#4099C5
Total Important Keywords: 60
Total Media Queries: 5
Media Queries: screen and (min-width: 1024px),screen and (min-width: 1280px),screen and (min-width: 720px),screen and (min-width: 480px),screen and (min-width: 1200px)
```
This is the CSS file that I will be using as an example throughout this article.
We’re not going to look at every single one of these metrics in this blog post.
Instead, we’ll look at the slightly more abstract data and work out what it
represents, and what insights it gives us. We have to dig a little deeper for
the implicit meaning in a lot of these values, but once we’ve worked out what
we’re looking for, we open up a whole new world of knowledge.
- - -
Before we get too deep into things, there are a few of things we need to be
aware of when using Parker:
0. **This article was written in context of Parker version `0.0.10`.** If any
major changes are released, I will endeavour to update this article
accordingly.
0. **Parker reports mean values.** It would be quite nice to know that ‘most of
your selectors have class-level specificity’, rather than ‘the average
specificity across all of your selectors is roughly that of a class’. A
subtle but significant distinction.
0. **Unfortunately Parker currently reports specificity incorrectly.** Katie and
I are in active discussions about how best to tackle this, but for now it’s
technically incorrect. In real terms—unless you have hellishly nested
selectors using more than 10 classes—it shouldn’t impact you too much, but
it’s certainly something to be aware of.
With these points in mind, please remember: Parker is not failsafe; it is not
infallible and nor does it claim to be. Parker is best used as a rough guide; a
finger in the air. Parker’s job is just to present you with the numbers; it’s
down to you to know what those numbers represent, and what they mean for your
CSS.
That’s what this article is for.
- - -
## Total Rules, Selectors, Identifiers, Declarations
All of these metrics are very self explanatory, and don’t need any special
mention. However, using two of these metrics we can actually polyfill a very
useful one that isn’t present (Katie and I are discussing its addition).
If we divide Total Declarations by Total Rules, we are left with the mean number
of declarations per ruleset:
**Total Declarations ÷ Total Rules = Declarations Per Ruleset**
Given our data set, we’re left with:
522 ÷ 260 = 2.007692308
We have an average of 2 declarations per ruleset.
What this tells us is how large each of our rulesets are: rulesets with lots of
declarations are probably quite monolithic, and could/should probably be broken
down into smaller composable responsibilities.
Take this example of an overly loaded ruleset:
```
.btn-login {
display: inline-block;
padding: 2em;
background-color: green;
color: white;
}
```
There are a number of problems present here. Firstly, the name `btn-login`
describes a very specific use case, making it very difficult to reuse. Secondly,
this is a pretty monolithic ruleset—it handles everything about this button,
mixing up structural and cosmetic responsibilities. Instead, we could break this
out into:
```
.btn {
display: inline-block;
}
.btn--large {
padding: 2em;
}
.btn--positive {
background-color: green;
color: white;
}
```
Three smaller and more composable rulesets, each with a smaller number of
declarations. CSS like this likely adheres to the [Single Responsibility
Principle](/2012/04/the-single-responsibility-principle-applied-to-css/),
meaning we have very well defined and encapsulated rulesets that can be combined
and composed in a very modular fashion.
This is how the Declarations Per Ruleset metric comes in handy.
- - -
## Selectors Per Rule
```
Selectors Per Rule: 1.4038461538461537
```
Selectors Per Rule is pretty simple. The following CSS has one selector per
rule:
```
.c-btn {
display: inline-block;
padding: 12px;
}
.c-btn--small {
padding: 6px;
}
.c-btn--large {
padding: 24px;
}
```
This CSS has two selectors per rule:
```
h1, .u-h1 {
font-size: 3rem;
}
h2, .u-h2 {
font-size: 2rem;
}
h3, .u-h3 {
font-size: 1.5rem;
}
```
Straightforward enough, but what does this actually tell us?
Well, we want a number as close to one as possible. Lots of selectors per rule
suggests that we’re applying the exact same declarations (styles) to a number of
different selectors. Perhaps we could create a single catch-all selector to
handle all eventualities, making our CSS smaller, and abstracting patterns out
into more reusable selectors. Let’s look at an example of some poorly written
CSS that has lots of selectors per rule:
```
input[type="text"]
input[type="email"],
input[type="password"],
textarea {
border: 1px solid #ccc;
background-color: #fff;
color: #333;
padding: 4px;
}
```
As we add more input types to this site, that list of selectors continues to
grow. A much simpler and shorter solution would be to tie all of this
information up into a single reusable class, for example:
```
.c-input-text {
border: 1px solid #ccc;
background-color: #fff;
color: #333;
padding: 4px;
}
```
So no matter if the input handles email addresses, regular text, passwords, or
an input type yet to be invented, we don’t need to maintain a very
implementation-specific list of selectors.
We often end up with lots of selectors per rule for two key reasons:
0. **Fear of using classes in our markup:** The surprisingly still-persistent
fear and avoidance of judicious use of classes in our HTML often leads to
developers creating (and subsequently maintaining) unwieldy lists of
selectors that are all chained to the exact same declarations. By adopting a
more class-based architecture, we can begin to recycle these rules in a much
more terse and practical way.
0. **Using Sass’ `@extend`:** Sass’ `@extend` functionality has long been
considered an anti-pattern for a number of reasons (please see
[Extending silent classes in
Sass](/2014/01/extending-silent-classes-in-sass/),
[When to use `@extend`; when to use a
mixin](/2014/11/when-to-use-extend-when-to-use-a-mixin/),
[Mixins Better for
Performance](/2016/02/mixins-better-for-performance/)).
`@extend` transplants selectors from one part of your project to all
converge on another. This has plenty of its own problems—detailed in the
linked articles—but it ultimately gives us a long, unwieldy list of selectors
chained to the same declarations; it increases our Selectors Per Rule.
In the extreme, it’s not uncommon to see `@extend` being abused by developers
striving to never repeat the same declaration twice, leading to CSS that looks
like this:
A confusing mess of unrelated selectors all grouped together via coincidence as opposed to reason. This fragments style information across your project, creates unusual relationships and dependencies, removes encapsulation, and makes using DevTools far more difficult:Damn! I wouldn't like to be the one who maintains this 125 lines CSS selector! pic.twitter.com/0jAEb2h8VF
— Gaël Métais (@gaelmetais) 7 February 2015