CSS Is Certainly Global And That’s Exactly The Point
Tags: css, webdev, frontend, learning
Published: 2021-05-31 01:03:01

---
title: CSS Is Certainly Global And That’s Exactly The Point
published: true
date: 2020-07-13 00:00:00 UTC
tags: css,webdev,frontend,learning
canonical_url: https://matthogg.fyi/css-is-certainly-global-and-thats-exactly-the-point/
description: CSS is often belittled because it’s naturally global—which we‘ve been trained to avoid—but doing so betrays a failure to understand CSS’ basic premise and greatest strength.
---

CSS is often belittled because it’s naturally global—which we‘ve been trained to avoid—but doing so betrays a failure to understand CSS’ basic premise and greatest strength.

Anybody who has struggled with Cascading Style Sheets (CSS) has seen the memes mocking the experience, like the famous [Family Guy GIF](https://gph.is/1heneJM) or this popular [Twitter joke](https://twitter.com/thomasfuchs/status/493790680397803521). When all CSS styles are globally scoped, the joke writes itself—try to do one simple thing with CSS and everything goes to hell. How can anybody work like this?!?

{% twitter 493790680397803521 %}

As [Keith J. Grant puts it](https://keithjgrant.com/posts/2017/03/css-is-not-broken/): “Just because you expect CSS to be easy, doesn’t mean the language is broken when you find it is not.”

For decades now, global variables have been recognized as difficult to reason about and a source of bugs. So, we avoid them! Today, we take this for granted—often forgetting that it’s not an absolute truth.

In the case of CSS, use of global scope isn’t a bug, it’s a feature! The reason for this is subtle and can present a learning curve to newcomers, but it’s important to understand that it was a deliberate, intentional choice.

## A Philosophical Challenge, Not A Technical One

Let’s think about what CSS truly does. Its mission is unique. On the web, how do we _universally_ design a document or an app? Not just for different browsers, but devices like mobile, laptops, and even TVs or smartwatches. Not just for screens, but maybe a printed page.

As Miriam Suzanne explains in [this enlightening video](https://www.youtube.com/watch?v=aHUtMbJw8iA), we’re designing for an “unknown infinite canvas.” Decades ago, CSS’ creators showed [remarkable foresight](https://talks.hiddedevries.nl/ai4MEp/slides) in making the language platform agnostic and _contextual_.

{% youtube aHUtMbJw8iA %}

CSS isn’t opinionated about where our styles come from. They can happily live together in global scope provided we can get _the correct visual result for the context in effect_ at any given time. This mechanism is called _the cascade_.

## Sorting Out The Cascade

The broadest interpretation of the cascade is that it’s a _sorting operation_—albeit a massive one, covering every style declaration as it applies to each and every element in our HTML. Our global stew of styles will certainly have multiple declarations that target the same given element, and such conflicts need to be resolved. We sort on multiple dimensions to decide which declaration with the highest precedence “wins” and gets applied to the element.

These dimensions, in order of precedence, are: [origin/importance](https://drafts.csswg.org/css-cascade/#cascade-origin), [selector specificity](https://drafts.csswg.org/css-cascade/#cascade-specificity), and [order of appearance](https://drafts.csswg.org/css-cascade/#cascade-order). Other CSS concepts, like [inheritance](https://drafts.csswg.org/css-cascade/#inheriting) and [initial values](https://drafts.csswg.org/css-cascade/#initial-values), are also important to understand.

Understanding the cascade is an ongoing and in depth lesson that’s beyond the scope of this essay—related concepts like specificity deserve one or more essays of their own!

Some suggested reading includes the _absolutely delightful_ overview [“The CSS Cascade”](https://wattenberger.com/blog/css-cascade), by Amelia Wattenberger, or the detailed [“Cascading Style Sheets/Inheritance Rules”](https://en.wikibooks.org/wiki/Cascading_Style_Sheets/Inheritance_Rules#Cascade_Sorting_Table) on _Wikibooks_, which elaborates on the concept of the cascade as a sorting table. There‘s also Mozilla‘s video [“Inspecting the CSS Cascade using Firefox DevTools”](https://www.youtube.com/watch?v=Sp9ZfSvpf7A), in which Miriam Suzanne explains the cascade in the context of Firefox’s DevTools.

{% youtube Sp9ZfSvpf7A %}

As we learn about the cascade two lessons should become clear:

1. The end result of the cascade is that _every element gets completely styled_, whether we wrote a dozen CSS declarations, just one, or none at all.
2. The outcome of the cascade won’t always follow intuitively from our code as it was originally written.

The consequence is that we can easily complicate and over-engineer our CSS. Liberal use of `!important`, overly complex selectors, selectors that apply too broadly, or styles defined out of order and in obscure places would indicate a developer (or team) that doesn’t understand how the cascade works.

A prudent, surgical touch is essential. Our goal is to diligently _limit the amount of CSS that might ultimately be ignored by the cascade_. The only dead code we introduce should be modest, deliberate, and conditional—styles that hibernate until “activated” by a particular context.

Now, the given context could be _anything_: a one-off button, the size of the user’s screen, a live interaction from the user, the app’s current state, an OS-level accessibility setting on the user’s device, the user’s chosen device or browser, the time of day, and so on.

When we understand this principle and leverage the cascade, we do less work. We write _less code_.

## Unique Benefits Of Global Scope And The Cascade

Writing less code is admirable enough, but there are other benefits.

First, there are many styles (e.g., typography, color, spacing) that we hope are consistent and predictable. Styling components piecemeal means we’ll inevitably diverge from the original design, bloat our codebase, and create bugs. Design patterns are easily reinforced by proper use of global scope and the cascade, which makes designers _and_ developers happy.

Second, global styles are automatic, making them easy to reuse or extend. Any new feature or page we create can be instantly styled with minimal effort, and then any specific styles can safely expect those global styles to be handled already.

Lastly, CSS obligates us to think holistically. Typical software architecture solves for complexity with encapsulation and modular code. CSS is contextual, so that kind of isolation isn’t always practical—we can’t entirely ignore how anything surrounding our component is styled. This is in fact closer to how designers approach their work. Here’s [Keith J. Grant again](https://keithjgrant.com/posts/2018/06/resilient-declarative-contextual/):

> If an engineer designs a bridge, you can’t just look at the blueprint and say, “this is all good except this one beam here; go ahead and take that out”. Removing that beam has ramifications on the structural integrity of the whole thing. Similarly, changing one part of a design can have ramifications on how other items on the screen are perceived.

## Why Do We Need To Know This?

Some readers might wonder why any of this matters, given everything that modern CSS offers. We can use convention ([BEM](http://getbem.com/)), make globals opt-in ([CSS Modules](https://github.com/css-modules/css-modules)), adopt serious tooling ([CSS-in-JS](https://jxnblk.com/blog/why-you-should-learn-css-in-js/)), or even heartily embrace global scope ([functional CSS](https://rangle.io/blog/styling-with-functional-css/)). Do we really need to think about the cascade?

Never forget that such tooling may very well be the ultimate [leaky abstraction](https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/). The user _always receives CSS_, no matter what fancy tricks we employ to avoid thinking about it ourselves.

So, just as we learn to do algebra by hand before using a calculator in high school, we ought to know how CSS works!

For one thing, every developer at some point will encounter a legacy codebase. Maybe we’re fixing a bug in CSS a stranger wrote years ago, reskinning an app’s design, or migrating to something more modern. This knowledge helps us confidently navigate the existing code and then make it better.

For another, even when our styles are locally scoped, the cascade and issues of scope are still present. Local scope doesn’t magically make our CSS safer. Moreover, we can still employ aspects of the cascade like inheritance or specificity to keep a component’s CSS clean and efficient.

Finally, for web apps, the CSS is our most direct, tangible connection to users—a good user experience will ensure they remember our app fondly. It’s our responsibility to master this technology, accommodate our users’ contexts, and provide the best experience possible.

## In Summary

CSS is far more nuanced than a typical programming language. Most developers aren’t comfortable with nuance and ambiguity, so some degree of apprehension isn’t surprising.

CSS resides somewhere in the space between design and programming. It’s contextual and unassuming, and therefore more philosophically demanding of developers who’d hope to use it wisely. The global nature of CSS isn’t something we can ridicule, grudgingly tolerate, or blunder through.

CSS has its quirks, to be sure, but understanding how the cascade works can benefit both developers and users alike! It’s a sophisticated and proven technology uniquely suited to the challenge of building user interfaces for an unpredictable web.