Utility classes serve a single purpose.
Examples of utility classes: .align-left, .float-left, .clearfix
Responsive utility classes allow you to create very flexible responsive websites.
This post is about what makes a great responsive utility class.
I’ll talk about: naming and a useful pattern.
For a good introduction to utility classes I suggest David Clark’s excellent article here.
Naming
I don’t like namespaced or aliased utility classes.
I don’t like them because they are harder to think about.
An example of a bad utility class is: .u-mt-10@md
Why is this bad?
You must know that .u is a convention for utility classes.
You must know that mt refers to margin-top.
You must know that @md means this utility class targets “Medium” sized devices.
What’s an alternative?
Utility classes should use CSS property names and values: .margin-top-10-xs
It is clear that this utility class adds 10 pixels of top margin.
Users familiar with Bootstrap’s grid system may guess that this targets “Extra small” viewports and greater.
Pattern: Inclusives and Exclusives
Two types of responsive utility classes are needed when building layouts: inclusives and exclusives.
- Inclusive: You need a utility class to target a specific device size and everything greater than it (most common)
- Exclusive: You need a utility class to only target a specific device size
Separate responsive utility classes into two categories: inclusives and exclusives.
A simple naming convention in your utility class tells you how it will behave.
Inclusives
Inclusive classes include the named device size and device sizes greater than itself.
When you use Bootstrap’s responsive grid you only have to define an element as .col-xs-12 and you get 12 columns on mobiles, tablets and desktops.
Noticing that this simple pattern results in less classes showed me that by default all responsive utility classes should behave this way.
Responsive utility classes are inclusives by default.
Every utility class that uses the convention of .property-value-deviceSize is an inclusive class.
You may have noticed our utility classes follow a different convention to Bootstrap’s columns.
Bootstrap uses: .property-deviceSize-value
We use: .property-value-deviceSize
This was intentional because it’s easier to think about.
When creating an element I know I want to apply 10 pixels of top margin to an element. The targeted device is a detail.
In my head I’ll say “margin top 10 on mobiles and greater”, not “margin top on mobiles and greater, of 10 pixels”.
How inclusives look in code
/* Inclusives */ // Mobile and > .margin-top-10-xs { margin-top: 10px; } // Tablet and > @media screen and (min-width: $screen-sm-min) { .margin-top-10-sm { margin-top: 10px; } } // Desktop and > @media screen and (min-width: $screen-md-min) { .margin-top-10-md { margin-top: 10px; } }
Exclusives
Exclusives target the named device size only.
They follow this convention: .property-value-only-deviceSize
A margin top of 10 pixels applied to only mobiles looks like: .margin-top-10-only-xs
How exclusives look in code.
/* Exclusives */ // Mobile - max-width used @media screen and (max-width: $screen-xs-max) { .margin-top-10-only-xs { margin-top: 10px; } } // Tablet @media screen and (min-width: $screen-sm-min) and (max-width: $screen-sm-max) { .margin-top-10-only-sm { margin-top: 10px; } } // Desktop @media screen and (min-width: $screen-md-min) { .margin-top-10-only-md { margin-top: 10px; } }
Distinguishing between inclusive and exclusive classes leads to incredibly flexible responsive utility classes.
Structural responsive utility classes
Copy this pattern for other structural properties.
Responsive utility classes are almost always used on structural CSS.
– Margin
– Padding
– Line breaks
Utility classes should be imported last
Adding utility classes last helps you avoid specificity issues.
Using Sass or Less simply import your utility classes last.
By including our utilities last, I’ve not had to use !important yet.
@import base/reset.scss; @import layout/layout.scss @import module/headers.scss; @import page/homepage.scss; @import utilities/utilities.scss;
tl;dr
By naming our responsive utility classes with valid CSS property names and values we have created clear, easy to use utility classes that don’t require education – if you are familiar with CSS properties already.
By breaking responsive utility classes into inclusives and exclusives by -xs being inclusives and -only-xs being exclusives, our responsive utility classes are very flexible.