Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

1.3 MB of class names in our HTML, and 69 per cent of the stylesheet they point at never reaches a public page

Atomic CSS makes a trade that everybody describes and almost nobody measures: the stylesheet stops growing with your app, and the class names move into every document instead. I wanted the actual figures for that trade o

Atomic CSS makes a trade that everybody describes and almost nobody measures: the stylesheet stops growing with your app, and the class names move into every document instead. I wanted the actual figures for that trade on a real site, so I counted the class attributes on all 76 public pages of Nakodo, and then counted what the stylesheet contains that those pages never use.

The two sides of the trade

elements on 76 pages                      64,980
elements carrying a class attribute        24,118
bytes of class attributes                1,321,945
class tokens                                97,725
distinct class tokens, site-wide               540
distinct tokens on one page, median            265
stylesheet, two files                      154,378 bytes

1.3 MB of class names against a 154 KB stylesheet, which is 8.6 to 1 in favour of the markup. Per page it is about 17.4 KB of class attributes, repeated in every document, against a stylesheet downloaded once and then cached for a year because its URL contains its own hash.

Then the other direction. Parsing class selectors out of both CSS files gives 1,739 distinct ones, and only 540 of them appear anywhere on a public page:

class selectors in the CSS                   1,739
ever used on a public page                     540
never used on a public page                  1,206   (69%)

That 69 per cent is not dead code. It is the app behind the login, which shares one stylesheet with the marketing site, so a visitor who never signs in downloads the rules for the campaign table, the brief editor, the dialogs and the dropdowns. The sample reads exactly like the app you cannot see from outside:

**:data-[slot=kbd]:rounded-sm
*:data-[slot=select-value]:items-center
*:[svg:not([class*='size-'])]:size-4
*:data-[slot=alert-description]:text-danger

Splitting the stylesheet by route group would move perhaps 100 KB off the public pages, and it would also mean two cache entries and a second file on the critical path. Since the file is immutable and cached, and since an earlier measurement showed first paint on this site is bounded by font preloads rather than CSS, I am leaving it as one file. But "69 per cent of our CSS is for logged-in screens" is a much better thing to know than "our CSS is 154 KB".

The 714-character button

The longest class attribute on every single page is the same one, to the character:

longest class attribute per page: 714 characters (median across 76 pages: 714)
element: <a>

It is the button component rendered as a link. ui/button.tsx is 65 lines and holds a 482-character base string, and variants and sizes take the rendered attribute to 714:

group/button inline-flex shrink-0 items-center justify-center rounded-md border
border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap
transition-colors outline-none select-none focus-visible:outline-2 ...

Seven hundred characters of attribute for one button. Across a sample of twelve pages there are 63 elements carrying that component's classes, 40,480 bytes between them, about 3.4 KB a page, and exactly one of them per page is the full 714. That is the clearest example of where atomic CSS puts its weight: not in the stylesheet, in every copy of the document. It is also why the compression ratio matters so much here: the glossary is 526,929 bytes of HTML and 47,715 bytes on the wire, an 11 to 1 saving, because a document full of repeated class strings is the easiest thing brotli will ever be handed.

540 tokens, and seven with nothing behind them

The most-used utilities are the boring ones, which is reassuring:

hover:text-foreground   3,016
text-muted-foreground   2,779
text-ink-700            2,661
dark:text-muted-foreground 2,661
shrink-0                2,350
flex                    2,333

58 tokens appear exactly once in the whole site, which is the long tail you would expect from one-off layout fixes.

Seven tokens in use have no rule anywhere in the CSS, and both reasons are worth knowing:

group/button
lucide-check  lucide-copy  lucide-mail  lucide-menu  lucide-plus  lucide-x

group/button is a named group marker. It never produces a declaration itself; it exists so that other selectors can say group-hover/button: and mean "when that specific ancestor is hovered". The six lucide-* classes are added by the icon library to every icon it renders, as a styling hook for people who want one. We do not, so they are 76 pages of bytes buying nothing, and the library has no option to turn them off.

Every inline style on the site is a coordinate

1,167 style attributes across the 76 pages, and only 42 distinct values. The frequencies give it away:

76  left:43.735%;right:45.776%
76  left:2%
76  left:9%
76  left:17%
76  left:24%

Exactly 76 occurrences each, which means one per page, which means they are in the footer. This site's footer is a drawn landscape, and the things standing in it are positioned as percentages that no utility class could express without generating a class per value. That is precisely the case the style attribute is for: a number that belongs to one element and will never be reused.

So the audit of inline styles came out better than I expected. Not one of the 1,167 is a colour, a font or a spacing value sneaking past the design system. They are all drawing coordinates.

1,178 comments, and we wrote none of them

The last count was the one I had never looked at:

comment nodes across 76 pages   1,178
  <!-- -->                        872
  <!--$-->                        153
  <!--/$-->                       153

The 872 empty ones are React's text separators, which it emits so that hydration can tell two adjacent text nodes apart. The median page has 10 and the terms has 82, which tracks exactly with how much interpolated prose a page contains: legal pages are full of sentences stitched together from constants.

The 153 pairs are Suspense boundaries, and the distribution is the interesting part: exactly two on 75 pages, three on the home page. The only <Suspense> written into a public page in this codebase is the one on the home page, around a notice that reads a search parameter. So two boundaries per page belong to the router itself, inserted around its own segments, and they are in the HTML of a page that streams nothing and has no fallback to show.

That is the whole shape of a modern document in three numbers. Of 1,178 comment nodes, one is mine.

What I am changing

Nothing in the markup, and one thing in the review checklist. The measurements that would have worried me came out clean: no inline styles carrying design values, no runaway one-off utilities, no duplicate ids. The numbers worth keeping are the two that contradict the usual framing of atomic CSS: the stylesheet is not the cost, the documents are, and most of the stylesheet is for screens the public never sees. Both are fine here because of compression and immutable caching, and both would stop being fine the moment this site started serving uncached HTML.

If you want to check any of it, every page is public: the glossary is the biggest document, the terms has the most React comments, and any guide will show you the 714-character button in its header.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.