About CSS Variables Generator
CSS custom properties, commonly called CSS variables, are arguably the single most transformative feature added to the CSS language since flexbox. Introduced in 2015 and universally supported since roughly 2017, they solved a problem that had plagued stylesheet authors for two decades: the inability to define a value once and reuse it across an entire stylesheet without a preprocessor. Before custom properties, the only way to reuse a colour or a spacing value was to copy-paste it everywhere — or to use a CSS preprocessor like Sass or Less, which would compile variables into static values at build time. Both approaches had the same fatal flaw: once the CSS was shipped, the values were frozen. You could not change a brand colour on the fly in response to a user preference, a theme toggle, or a dark-mode media query.
CSS custom properties changed all that. They are live — they can be updated at runtime with JavaScript, they respond to media queries and container queries, and they participate in the CSS cascade just like any other property. This means you can define `--primary-colour: #3B82F6;` in the `:root` selector, use it in a hundred different rules across your stylesheet with `color: var(--primary-colour);`, and then override it inside a `.dark` class or a `@media (prefers-color-scheme: dark)` query to instantly recolour the entire page. No JavaScript needed, no recompilation, no page refresh.
The syntax is straightforward but has a few details that trip people up. A custom property name must start with two dashes: `--my-variable`. The name is case-sensitive (`--primary` and `--Primary` are different variables, though using mixed-case names is discouraged). The value can be any valid CSS value — a colour, a length, a percentage, a string, a full shorthand declaration, even another `var()` reference. Custom properties are defined in a CSS rule, typically on `:root` (the `<html>` element) for global scope, but they can be scoped to any selector — defining `--card-radius: 12px` on `.card` makes it available only to that class and its descendants.
The `var()` function is how you consume a custom property. Its signature is `var(--name, fallback)`, where the fallback is optional. For example, `color: var(--primary-colour, #333)` uses the value of `--primary-colour` if it is defined, or falls back to `#333` if it is not. This fallback mechanism is a built-in safety net that makes custom properties safe to use even in components that might be used in contexts where the variable is not defined.
This tool exists because understanding CSS variables intellectually is different from seeing them in action. Reading `--primary: #3B82F6` in a code snippet tells you nothing about how that blue will look on a button, in a card border, or as a heading colour. The tool bridges that gap with a live preview card — a miniature UI component (heading, paragraph, button) that consumes your variables in real time. Change `--primary` to a warm amber and the button instantly shifts from blue to gold, the heading underline changes colour, and the card border picks up the new hue. Change `--radius` from 8px to 24px and the button and card corners round in unison. This immediate feedback is how you develop an intuition for what makes a good design token.
The four variable slots in this tool were chosen to represent the most impactful design tokens in any UI system:
- **--primary (colour):** The brand colour that drives buttons, links, borders, and accent elements. Changing this one variable recolours a significant portion of the preview. In real-world design systems, there are typically multiple colour tokens (primary, secondary, accent, surface, text), but primary is the one that defines the personality of the interface.
- **--radius (size):** The border-radius applied to cards, buttons, and input fields. This single number controls how rounded or sharp the entire interface feels. A small radius (4-8px) reads as professional and serious (think Stripe or GitHub). A large radius (16-24px) reads as friendly and playful (think Duolingo or Notion).
- **--spacing (size):** The padding and gap between elements. This is the unsung hero of design consistency. When every component uses the same spacing unit, the interface feels cohesive and rhythmical. When different components use arbitrary spacing values, the result feels sloppy, no matter how beautiful the colours are.
- **--font-size (size):** The base font size for body text. This typically sits at 16px (the browser default), but some designs use 15px for a denser feel or 18px for a more spacious, reading-oriented layout. Changing this variable scales all text in the preview proportionally.
The tool\'s output formats these four variables as a `:root { }` block — the standard location for global design tokens. You can copy the block and paste it into the top of your main stylesheet, then reference the variables throughout your CSS with `var(--primary)`, `var(--radius)`, etc. Because the output is standard CSS custom properties, it works in any project regardless of framework: vanilla HTML, React, Vue, Angular, Svelte, even plain HTML emails (with limited support).
One of the most powerful patterns enabled by CSS variables is the ability to build truly themeable components. A single button component can have its styles written entirely in terms of CSS variables — `background: var(--btn-bg, var(--primary))`, `border-radius: var(--btn-radius, var(--radius))`, `padding: var(--btn-padding, var(--spacing))` — so that it adapts to whatever theme is currently active without any class-switching. This tool\'s preview demonstrates exactly this pattern: the card, heading, and button all derive their styles from the same four variables, showing how a small set of well-chosen tokens can govern a large surface area of design with minimal code.