skip to content

Why does a runtime CSS-in-JS seat map inject thousands of rules when each seat's colour comes from props, and how do you fix it?

level: middleimportance: should knowfreq 13%

answer

  1. the class name comes from the CSS
  2. one distinct value, one new rule
  3. rules are rarely removed
  4. one static rule, value set inline
  5. fixed sets become variants

basics

~20 s

Each distinct interpolated colour is a distinct CSS string, so the library hashes and inserts a new class per value. Keep one static rule reading a custom property, set the colour inline per seat, and make small fixed sets variants.

solid answer

~50 s

A runtime library names a class after a hash of its CSS, so `background: ${p => p.colour}` produces a new CSS string, a new class and a new inserted rule for every distinct colour. A seat map with two thousand seats on a continuous demand scale therefore injects up to two thousand near-identical rules, with the insertions forcing extra style recalculation, and long-running pages keep accumulating them. The fix is to separate the static from the dynamic: write **one static rule**, `background: var(--seat-colour)`, and set `--seat-colour` per seat through the element's inline `style`, so the stylesheet stops growing. For a value from a **small fixed set**, such as seat status, write each variant as its own rule in advance and select it with a data attribute or a class. Both forms also survive a later move to build-time extraction.

code

tsx · 18 lines
tsx
import type { CSSProperties } from 'react';

// One static class written once in your styling tool:
//   .seat { background: var(--seat-colour, grey); }
//   .seat[data-status='sold'] { opacity: 0.4; cursor: not-allowed; }
declare const seatClass: string;

type SeatProps = {
  colour: string;
  status: 'open' | 'held' | 'sold' | 'accessible';
};

export function Seat({ colour, status }: SeatProps) {
  const style = { '--seat-colour': colour } as CSSProperties;
  return (
    <button className={seatClass} data-status={status} style={style} />
  );
}

go deeper

for a junior

Remember that a runtime library names classes after their CSS, so a value that changes per element produces a new class per element.

for a middle

Explain the hash-per-string mechanism, then show the two fixes: one static rule reading a custom property set inline, and predefined variants for small fixed sets.

for a senior

Spot unbounded interpolations in review, connect them to style recalculation and stylesheet growth in long sessions, and plan the refactor so the result can later be extracted at build time.

for a principal

Set a codebase rule for dynamic styling, custom properties for continuous values and variants for fixed sets, so components stay compatible with whichever styling family the platform adopts.

## Why one prop becomes thousands of rules A **runtime CSS-in-JS library** turns each element's style template into a CSS string and derives the class name from a hash of that string. That is what makes identical styles cheap: same string, same class, one rule. It is also why a **continuous value** interpolated from props is expensive. On a concert-ticketing seat map, suppose each seat's background shows live demand on a colour scale: - 2,000 seats, each with a slightly different computed colour; - each colour changes the CSS string, so each seat gets its own hash; - each new hash means a new class and a newly inserted rule. The result is a stylesheet with thousands of rules that differ only in one value. Inserted rules make the browser recalculate styles more often, and when demand updates and colours shift, a fresh batch of classes is generated while the old ones usually stay in the sheet. Emotion's best-practices guide shows the same effect with avatars whose `background-image` differs per user: each one becomes a separate near-duplicate class. styled-components 6.4.0's changelog even records a fix for a memory leak in long-running apps whose interpolations took free-form values from unbounded user input. ## Separate the static from the dynamic The fix is to stop putting the changing value into the CSS text: 1. Write **one static rule** for every seat that reads a custom property: `background: var(--seat-colour, grey)`. 2. Set the property's value per element through the inline `style`, for example `style={{ '--seat-colour': colour }}`. 3. Let the cascade do the rest: the rule's text never changes, so the library hashes it once and inserts it once. Emotion's guide recommends exactly this for dynamic values ("CSS variables with `style`"), and styled-components 6.3.0's notes advise preferring static styles over dynamic interpolations and setting custom properties through inline `style`. How a custom property resolves on each element is ordinary CSS and not specific to any styling tool. ## Fixed sets become variants Not every prop is continuous. A seat's **status** comes from a small, known set: open, held, sold, accessible. For those: - write one rule per status in advance, selected by a data attribute such as `[data-status="sold"]` or by a class chosen in code; - the number of rules is fixed by the set, not by the number of seats. styled-components 6.3.0's notes recommend data attributes for discrete variants for the same reason. ## Comparing the three forms | Form | Rules generated | Works with build-time extraction? | Typical use | |---|---|---|---| | Interpolating the prop into the CSS | One per distinct value | No, the value is unknown at build time | Prototypes, rarely changing values | | Static rule plus inline custom property | One | Yes | Continuous values: colours, positions, sizes | | Predefined variants | One per variant | Yes | Small fixed sets: status, size, tone | ## Side effects worth knowing - **Rapidly changing values** such as a dragged seat-selection box belong in inline style anyway; Emotion's guide gives a drag `transform` as its example. - **Content security policies**: a `style` attribute written into server-rendered HTML counts as an inline style, so a strict `style-src` policy can refuse it. Check how your stack applies inline styles before relying on this pattern under such a policy. - **Migration path**: once dynamic values are custom properties and discrete ones are variants, the remaining styles are static, which is precisely what a zero-runtime library can extract. ## Refactoring the seat map step by step 1. **Find the unbounded interpolations**: any template expression whose input is continuous, fetched or user-supplied. 2. **Replace each with a `var()` read** in the static rule, giving it a fallback such as `grey` so a seat with no value still renders visibly. 3. **Set the property inline** on the element from the same prop that used to feed the interpolation. 4. **Collapse discrete props into variants**: status, size or selection state become data attributes or predefined classes. 5. **Verify in the browser's developer tools**: count the rules in the library's `<style>` element before and after; once the refactor is complete, scrolling the map or receiving a demand update should no longer add rules.

  • Would memoising the seat component stop the rules from multiplying?
    No. Memoisation only skips re-renders when props are unchanged; two thousand seats still carry two thousand distinct colours, and each distinct CSS string still needs its own class and rule. The count depends on how many distinct values exist, not on how often components render.
  • When is interpolating a prop into the CSS still acceptable?
    When the value comes from a small set and rarely changes, such as a component's size or a theme mode, the number of generated classes stays bounded. The problem appears with continuous or user-supplied values, where the set of distinct CSS strings is effectively unbounded.

  • The library reuses one class and patches the colour in place for each seat
  • Memoising the component stops the stylesheet from growing
  • A custom property set inline still generates a new rule per value
  • Every dynamic value should be a variant written in advance
  • Rules for colours no longer in use are always removed automatically