Notes on CSS That Has Aged Well

I have been writing style sheets long enough to have been sure, several times, that the latest technique would replace everything that came before it. A few of those techniques did become part of the language. Many more survive in old projects as a kind of geology. When I look at CSS that is still a pleasure to work with after five or ten years, it is usually not because it was fashionable. It is because it was modest and close to the document.

A website layout being designed on a laptop
Image: Shixart1985, CC BY 2.0

What tends to last

A website open in a browser
Image: Related names: Strickland, William; Jacobs, Jamie, transmitter; Tulloch, Marg…, Public domain

Plain, predictable selectors last. A small number of classes that describe a role, rather than a look, can be restyled later without a hunt through the HTML. Layout that uses the ordinary flow of the page, with flex or grid applied where a real two-dimensional arrangement is needed, ages better than a pile of absolute positions. Relative units, a typographic scale that works on a narrow screen, and colours defined in one place will still make sense when a new colleague opens the file.

I am also kinder than I used to be to CSS that is a little repetitive, if it is obvious. A clever mixin that generates half the site can be a gift or a trap, depending on whether anyone still knows how to run it. The same is true of build steps. I do not object to them, but I like to be able to read the shipped style sheet and understand the rules that are actually applying. That is the same standard I use when reading other people’s code.

What I now avoid as a default

I avoid tying behaviour and appearance so tightly that a visual change requires a tour through JavaScript. I avoid class names that are really a diary of the design process. I avoid resetting so aggressively that I then have to re-teach the browser what a paragraph is. And I am slower to pull in a large utility framework for a site that could have been a few dozen thoughtful rules, especially if I am the one who will maintain it.

When I do need a tiny helper, I still sometimes write a small command-line tool to list unused classes or to flatten a set of variables. The notes from those jobs go into the same unfancy store I describe in how I keep a personal knowledge base. The style sheet, like the notebook, should still be readable when I have forgotten the week I wrote it.

Good CSS, in the end, is less a bag of tricks than a habit of leaving the next person a document they can change without fear. If a rule would still be clear to me on a Monday morning, it has probably aged well enough.