We Write CSS Like We Did within the 90s, and Sure, It’s Foolish – A Checklist Aside

As internet builders, we marvel at expertise. We benefit from the many instruments that assist with our work: multipurpose editors, frameworks, libraries, polyfills and shims, content material administration techniques, preprocessors, construct and deployment instruments, growth consoles, manufacturing displays—the checklist goes on.

Article Continues Under

Our enjoyment of these instruments is so sturdy that nobody questions whether or not a small web site truly requires any of them. Software weight problems is the brand new WYSIWYG—the online builders who can’t do with out their frameworks and preprocessors are not any higher than our friends from the Nineteen Nineties who couldn’t do with out FrontPage or Dreamweaver. It’s true that these instruments have improved our lives as builders in some ways. On the similar time, they’ve maybe additionally prevented us from enhancing our primary expertise.

I wish to discuss a type of expertise: the craft of writing CSS. Not of utilizing CSS preprocessors or postprocessors, however of writing CSS itself. Why? As a result of CSS is second in significance solely to HTML in internet growth, and since nobody wants processors to construct a website or app.

Most of all, I wish to discuss this as a result of in the case of writing CSS, it typically appears that we have now realized nothing for the reason that Nineteen Nineties. We nonetheless write CSS the pure approach, with no advances in sorting declarations or selectors and no enhancements in writing DRY CSS.

As a substitute, many builders argue fiercely about every of those matters. Others merely dig of their heels and refuse to vary. And a 3rd cohort protests even the dialogue of those matters.

I don’t care that builders do that. However I do care about our craft. And I care that we, as a career, are ignoring easy methods to enhance our work.

Let’s discuss this extra after the code break.

Right here’s unsorted, unoptimized CSS from Amazon in 2003.

.serif {
  font-family: instances, serif;
  font-size: small;

.sans {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: small;

.small {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: x-small;

.h1 {
  font-family: verdana, arial, helvetica, sans-serif;
  shade: #CC6600;
  font-size: small;

.h3color {
  font-family: verdana, arial, helvetica, sans-serif;
  shade: #CC6600;
  font-size: x-small;

.tiny {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: xx-small;

.listprice {
  font-family: arial, verdana, sans-serif;
  text-decoration: line-through;
  font-size: x-small;

.value {
  font-family: verdana, arial, helvetica, sans-serif;
  shade: #990000;
  font-size: x-small;

.consideration {
  background-color: #FFFFD5;

And right here’s CSS from up to date Amazon:

.a-box {
  show: block;
  border-radius: 4px;
  border: 1px #ddd strong;
  background-color: #fff;

.a-box .a-box-inner {
  border-radius: 4px;
  place: relative;
  padding: 14px 18px;

.a-box-thumbnail {
  show: inline-block;

.a-box-thumbnail .a-box-inner {
  padding: 0 !necessary;

.a-box-thumbnail .a-box-inner img {
  border-radius: 4px;

.a-box-title {
  overflow: hidden;

.a-box-title .a-box-inner {
  overflow: hidden;
  padding: 12px 18px 11px;
  background: #f0f0f0;

Simply as in 2003, the CSS is unsorted and unoptimized. Did we study something over the previous 15 years? Is that this actually the most effective CSS we will write?

Let’s have a look at three areas the place I imagine we will simply enhance the way in which we do our work: declaration sorting, selector sorting, and declaration repetition.

Declaration sorting#section2

The 90s internet developer, if she or he wrote CSS, wrote CSS because it occurred to them. With out sense or order—with no path by any means. The identical was true of final decade’s developer. The identical is true of right this moment’s developer, whether or not novice or knowledgeable.

.foo {
  font: arial, sans-serif;
  background: #abc;
  margin: 1em;
  text-align: middle;
  letter-spacing: 1px;
  -x-yaddayadda: sure;

The one distinction between at times: right this moment’s knowledgeable developer makes use of eight variables, as a result of “that’s the way you do it” (even with one-pagers) and since in some unspecified time in the future of their life they could want them. In twenty-something years of internet growth we have now in some way not managed to make our CSS constant and simpler to work on by establishing the (and even a) frequent sense customary to type declarations.

(If this sounds harsh, it’s as a result of it’s true. Builders condemn selectors, shorthands, !necessary, and different helpful points of CSS slightly than concede that they don’t even know how one can type their declarations.)

In actuality, the problem is lifeless easy: Declarations ought to be sorted alphabetically. Interval.


For one, sorting makes collaborating simpler.

Untrained builders can do it. Non-English audio system (reminiscent of this writer) can do it. I wouldn’t be shocked to study that even houseplants can do it.

For an additional cause, alphabetical sorting will be automated. What’s that? Sure, one can use or write little scripts (reminiscent of CSS Declaration Sorter) to type declarations.

Given the convenience of sorting, and its advantages, the present state of affairs borders on the ridiculous, making it tempting to disregard our friends who don’t type declarations, and to ban from our lives those that argue that it’s simpler—and even logical—not to type alphabetically however as a substitute to type based mostly on 1) field dimensions, 2) colours, 3) grid- or flexbox-iness, 4) temper, 5) what they ate for breakfast, or some equally random foundation.

With this challenge settled (if considerably provocatively), on to our second downside from the 90s.

The state of affairs regarding selectors is sort of related. Nearly since 1994, builders have written selectors and guidelines as they occurred to them. Maybe they’ve moved them round (“Oh, that belongs with the nav”). Maybe they’ve refactored their model sheets (“Oh, unusual that website types seem amidst notification types”). However standardizing the order—no.

Let’s take a step again and assume that order does matter, not only for aesthetics as one would possibly assume, however for collaboration. For example, consider the letters beneath as selectors. Which checklist can be best to work with?

c, b · a · a, b · c, d · d, c, a · e · a

c · b · a, b · a · c, d · a, c, d · a · e

a, b · a, c, d · a · b, c · c, d · e

The truth that one selector (a) was a replica that solely bought found and merged within the final row maybe offers away my choice. However then, in case you wished so as to add d, e to the checklist, wouldn’t the order of the third row make putting the brand new selector simpler than putting it in both of the primary two rows?

This instance will get on the two points brought on by not sorting selectors:

  • Nobody is aware of the place so as to add new selectors, making a black gap within the workflow.
  • There’s the next probability of each selector repetition and duplication of guidelines with the identical selectors.

Each issues get compounded in bigger initiatives and bigger groups. Each issues have haunted us for the reason that 90s. Each issues get mounted by standardizing—by coding pointers—how selectors ought to be ordered.

The reply on this case is just not as trivial as sorting alphabetically (though we may play with the thought—the cognitive ease of alphabetical selector sorting might make it value making an attempt). However we will take a path much like how the HTML spec roughly teams components, in order that we first outline sections, after which grouping components, textual content components, and so forth. (That’s additionally the method of at the very least one draft, the writer’s.)

The purpose is that ultimate selector sorting doesn’t simply happen naturally and robotically. We are able to profit from placing extra thought into this downside.

Declaration repetition#section4

Our third hangover from the 90s is that there’s and has at all times been an insane quantity of repetition in our model sheets. In accordance with one evaluation of greater than 200 web sites, a median of 66% of all declarations are redundant, and the repetition price goes as excessive as 92%—that means that, on this research at the very least, the everyday web site makes use of every declaration at the very least 3 times and a few as much as ten instances.

As proven by an inventory of some pattern websites I compiled, declaration repetition has certainly been unhealthy from the beginning and has even elevated barely through the years.

Sure, there are causes for repetition: notably for various goal media (we might repeat ourselves for display screen, print, or totally different viewport sizes) and, sometimes, for the cascade. That’s the reason a repetition price of 10–20% appears to be acceptable. However the diploma of repetition we observe proper now is just not acceptable—it’s an unoptimized mess that goes principally unnoticed.

What’s the answer right here? One risk is to use declarations simply as soon as. We’ve seen with a pattern optimization of Yandex’s large-scale website that this will result in barely extra unwieldy model sheets, however we additionally know that in lots of different instances it does make them smaller and extra compact.

This method of utilizing declarations simply as soon as has at the very least three advantages:

  • It reduces repetition to a extra acceptable quantity.
  • It reduces the pseudo want for variables.
  • Excluding outliers like Yandex, it reduces file measurement and payload (10–20% in accordance with my very own expertise—we regarded on the results years in the past at Google).

It doesn’t matter what apply we as a area provide you with—whether or not to make use of declarations simply as soon as or comply with a special path—the present stage of “pure repetition” we face on pattern web sites is simply too excessive. We shouldn’t have to remind ourselves to not repeat ourselves if we repeat code as much as 9 instances, and it’s getting outright pathetic—once more excuse the sturdy language—if then we’re additionally those to scream for constants and variables and different options solely as a result of we’ve by no means stopped to query this 90s-style coding.

The unnatural, extra fashionable approach of writing CSS#section5

Concentrating on these three areas would assist us transfer to a extra fashionable approach of writing model sheets, one which has a simple however highly effective option to type declarations, features a plan for ordering selectors, and minimizes declaration repetition.

On this article, we’ve outlined some choices for us to stick to this extra fashionable approach:

  • Kind declarations alphabetically.
  • Use an current order system or standardize and comply with a brand new selector order system.
  • Attempt to use declarations simply as soon as.
  • Get help by instruments.

And but there’s nonetheless nice potential to enhance in all of those areas. The potential, then, is what we should always shut with. Whereas I’ve emphasised our “no modifications for the reason that 90s” approach of writing CSS, and confused the necessity for sturdy practices, we’d like extra proposals, research, and conversations round what practices are most useful. Helpful by way of writing higher, extra constant CSS, but additionally by way of balancing our sense of craft (our mastery of our career) with a excessive diploma of effectivity (automating when it’s acceptable). Striving to attain this steadiness will assist make sure that builders twenty years from now received’t have to put in writing rants about hangovers from the 2010s.

Leave a Comment