HTML, CSS, and Dependency Direction
Published on FebĀ 14, 2018 (updated OctĀ 3, 2023), filed under development, html, css (feed). (Share this on Mastodon orĀ Bluesky?)
Adam Wathan wrote one of the most interesting web development articles Iāve read in the last few months: CSS Utility Classes and āSeparation of Concerns.ā At least until āPhase 3ā thereās much to learn about current web development, when then, we drift into one of the two extremes and may still have other options to consider.
What I wish to do here is not critique the ending of this otherwise instructive article, but to look at one great aspect, dependency direction:
[ā¦]think about dependency direction.There are two ways you can write HTML and CSS:
āSeparation of Concerns.āCSS that depends on HTML.Naming your classes based on your content (like
.author-bio
) treats your HTML as a dependency of your CSS.The HTML is independent; it doesnāt care how you make it look, it just exposes hooks like
.author-bio
that the HTML controls.Your CSS on the other hand is not independent; it needs to know what classes your HTML has decided to expose, and it needs to target those classes to style the HTML.
In this model, your HTML is restyleable, but your CSS is not reusable.
āMixing Concerns.āHTML that depends on CSS.Naming your classes in a content-agnostic way after the repeating patterns in your UI (like
.media-card
) treats your CSS as a dependency of your HTML.The CSS is independent; it doesnāt care what content itās being applied to, it just exposes a set of building blocks that you can apply to your markup.
Your HTML is not independent; itās making use of classes that have been provided by the CSS, and it needs to know what classes exist so that it [combines] them however it needs to to [sic] achieve the desired design.
In this model, your CSS is reusable, but your HTML is not restyleable.
This is useful. Dependency direction is a great way to think about the link between HTML and CSS.
But.
(Thatās not to say dependency direction was not a great way to think about the link between HTML and CSS.)
But:
There are two issues with this.
One is that there still is no 1:1 relationship between HTML and CSS.
Two is that we still benefit from looking at the situation with an eye on long-term maintenance.
The Relationship Between HTML and CSS
The relationship between HTML and CSS has changed over time and itās contingent on the type of site or app.
Period or Type | HTML | CSS |
---|---|---|
90ās | n | 0 |
2000ās | n | 1 to n |
2010ās | n | 1 |
2020ās | 1? | 1 |
Multi-domain, multi-client sites | n | n |
One-pagers | 1 | 1 |
What this generalized, simplified, opinionated table aims to show is that yes, we seem to be heading to a 1:1 relationship between HTML and CSSāas in one template, one style sheetābut no, so far the only use case where weāve actually seen this is the classical one-pager.
What that means for dependency direction is that maintaining the āhigherā side is more expensive, and that therefore, in every case but the one-pager, more work goes into maintaining HTML.
Intuitively, we all know this: On the one-pager extreme, clearly it doesnāt matter how badly we mess up either HTML or CSS. Itās pretty simple to work with one-pagers. On the other hand, with a complex website, to this day is there a magnitude of difference between performing HTML and CSS changesāone, CSS edits, āgoes really fastā (and in this complex environment sometimes wrong), the other, HTML updates, often consists of searching the entire code base and partnering up with other teams.
We may well talk dependency direction but we wouldnāt be smart if we ignored that in one case, we need to update one style sheet, and in another, we need to update ten thousand documents or twenty template files. (That is the motivation for good maintainability, and something that drives my own maintainability research.)
The Situation With Long-Term Maintenance
In general, web design is a process. It is well possible (this site is one in a long history of cases) to extend and grow and improve a website (and also an app) just through iterations.
Then, however, a full redo and relaunch is still always on the table; in the best case because of a strategic re-orientation that requires to design and code anew; in the worst case because of severe mistakes, whether through inexperience and incompetence or through short-term thinking, like āfire and forget.ā (Iām working on my harshness.)
What does the generally useful concept of dependency direction have to do with this?
A suspicion, I admit.
The suspicion that we donāt know.
The suspicion that we donāt have any reliable data to tell what difference HTML ā CSS (HTML depending on CSS) and CSS ā HTML (CSS depending on HTML) make, in the long run.
The suspicion that the relationship between HTML and CSS is and remains n:1, and that thus, CSS ā HTML, and that thus, long-term, itās better to stick with best practices around focusing on the HTML and being careful when admitting (legitimate) exceptions.
And yet this is a suspicionāwe need more data, at least some well-documented empirical accounts (yes, I said this), in order to get a sense of direction here.
ā§ Overall, dependency direction is useful to think of. Alas, by itself it doesnāt mean anything if we donāt look closely at the nature of the dependency, the underlying code base, and the long-term prospect for development. And hence itās not the first time that we deal with a useful conceptāand not the last time that itās not enough to make good decisions. Which is not to imply Adam said anything else.
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a web developer, manager, and author. Iāve been working as a technical lead and engineering manager for companies youāve never heard of and companies you use every day, Iām an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OāReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)