🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Styling••By Komal Nakrani

Astro CSS Variables: Design Tokens That Scale

Use Astro CSS variables as design tokens: define colors, type and spacing in :root, layer semantic tokens, theme with data attributes, and pass props with define:vars.

#Astro#CSS Variables#Design Tokens#Styling

To use CSS variables as design tokens in Astro, define them once in a global stylesheet (:root { --color-accent: #2563eb; }), import that file in your base layout, and reference them with var() in every component’s scoped <style>. Custom properties cross Astro’s style scoping, so one file controls the colors, type scale and spacing of the whole site, and swapping a theme is a matter of overriding a few variables.

This guide shows a token structure that holds up in real projects: primitive and semantic layers, a type and spacing scale, theming with data attributes, component-level overrides, define:vars, and how tokens work alongside Tailwind CSS.

Key Takeaways

  • CSS custom properties are inherited, so tokens defined on :root reach every scoped component.
  • Split tokens into primitives (raw values) and semantic tokens (what components use).
  • Components should read semantic tokens only, such as --color-text rather than --gray-900.
  • Theme by overriding semantic tokens under [data-theme='dark'] or a brand class.
  • Use define:vars for per-instance values from props, not for site-wide tokens.

Why CSS variables work so well in Astro

Astro scopes component styles by rewriting selectors. That keeps class names from colliding, but it also means a component cannot see another component’s rules. CSS custom properties are the exception: they are inherited through the DOM like color or font-family, and scoping does not touch them.

So a value set on :root is available everywhere:

/* src/styles/tokens.css */
:root {
  --color-accent: #2563eb;
  --radius-md: 0.5rem;
}
---
// src/components/Button.astro
---
<button class="button"><slot /></button>

<style>
  .button {
    background: var(--color-accent);
    border-radius: var(--radius-md);
  }
</style>

Variables are also live at runtime. Change --color-accent on any ancestor and every descendant updates, with no rebuild and no JavaScript. That is what makes them the right foundation for themes, dark mode and per-section styling. If scoping itself is unfamiliar, start with scoped vs global styles in Astro.

Two layers: primitives and semantic tokens

The most common mistake with tokens is letting components use raw values directly, like var(--blue-600). It works until you need a dark theme, and then every component needs editing.

Split tokens into two layers instead:

/* src/styles/tokens.css */

/* 1. Primitives: the raw palette. Components never use these directly. */
:root {
  --blue-50: #eff6ff;
  --blue-600: #2563eb;
  --blue-400: #60a5fa;
  --gray-0: #ffffff;
  --gray-50: #f8fafc;
  --gray-200: #e2e8f0;
  --gray-600: #475569;
  --gray-900: #0f172a;
  --gray-950: #020617;
}

/* 2. Semantic tokens: what components use. */
:root {
  --color-bg: var(--gray-0);
  --color-surface: var(--gray-50);
  --color-border: var(--gray-200);
  --color-text: var(--gray-900);
  --color-text-muted: var(--gray-600);
  --color-accent: var(--blue-600);
  --color-accent-soft: var(--blue-50);
}

Primitives describe what a value is. Semantic tokens describe what it is for. A card uses --color-surface, never --gray-50. When the design changes, you remap semantic tokens to different primitives in one file, and components follow.

Two layers are enough for most sites. Large design systems add a third, component tokens like --button-bg, but in Astro a component’s scoped styles already act as that layer. Add component tokens only for the few components you expect callers to restyle.

A type and spacing scale

Colors get the attention, but consistent typography and spacing do more for how polished a site feels. Define both as scales:

/* src/styles/tokens.css */
:root {
  --font-sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: ui-monospace, 'SFMono-Regular', Menlo, monospace;

  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: clamp(1.125rem, 1rem + 0.5vw, 1.25rem);
  --text-2xl: clamp(1.5rem, 1.2rem + 1.2vw, 2rem);
  --text-4xl: clamp(2.25rem, 1.6rem + 2.6vw, 3.5rem);

  --leading-tight: 1.2;
  --leading-normal: 1.6;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-12: 3rem;
  --space-16: 4rem;

  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 1rem;

  --shadow-sm: 0 1px 2px rgb(15 23 42 / 0.06);
  --shadow-md: 0 4px 12px rgb(15 23 42 / 0.08);
}

The clamp() values make headings fluid: they grow with the viewport between a minimum and a maximum, without a stack of media queries. Spacing follows a 4px base so every gap lines up. If you load web fonts with Astro’s Fonts API, set --font-sans to the variable it creates; the Astro fonts guide shows how.

Wire tokens into the layout

Keep tokens in their own file and import it before your base styles:

/* src/styles/global.css */
@import './tokens.css';

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
}

h1 { font-size: var(--text-4xl); line-height: var(--leading-tight); }
h2 { font-size: var(--text-2xl); line-height: var(--leading-tight); }
---
// src/layouts/BaseLayout.astro
import '../styles/global.css';
---
<html lang="en">
  <body><slot /></body>
</html>

From here, components only ever reference tokens. A good test is to search your components for hex codes and pixel values; anything you find is a candidate for a token. The styling docs show where these files sit in a typical theme.

Theming with data attributes

With semantic tokens in place, a dark theme only overrides the semantic layer:

/* src/styles/tokens.css */
:root[data-theme='dark'] {
  color-scheme: dark;
  --color-bg: var(--gray-950);
  --color-surface: var(--gray-900);
  --color-border: #1e293b;
  --color-text: var(--gray-50);
  --color-text-muted: #94a3b8;
  --color-accent: var(--blue-400);
  --color-accent-soft: #172554;
}

No component changes. Every var(--color-surface) resolves to the new value as soon as data-theme="dark" is on <html>. Setting that attribute before the first paint is the tricky part, and dark mode in Astro without a flash covers the inline script that does it.

The same technique works for any variation, not just light and dark. A marketing section can use a different accent by overriding tokens on a wrapper:

---
// src/components/PromoBand.astro
---
<section class="promo"><slot /></section>

<style>
  .promo {
    --color-accent: #db2777;
    --color-accent-soft: #fdf2f8;
    background: var(--color-accent-soft);
    padding: var(--space-12) var(--space-6);
  }
</style>

Every button inside that section now uses pink, because custom properties cascade to descendants. The button component did not change.

Per-instance values with define:vars

Sometimes a value comes from a prop: a card’s accent color from the CMS, a progress bar’s percentage. Astro’s define:vars directive turns frontmatter values into custom properties for a component’s style block:

---
// src/components/Progress.astro
interface Props {
  value: number; // 0 to 100
  color?: string;
}
const { value, color = 'var(--color-accent)' } = Astro.props;
const width = `${Math.min(100, Math.max(0, value))}%`;
---
<div class="track" role="progressbar" aria-valuenow={value} aria-valuemin="0" aria-valuemax="100">
  <div class="bar"></div>
</div>

<style define:vars={{ width, color }}>
  .track { background: var(--color-surface); border-radius: var(--radius-sm); height: var(--space-2); }
  .bar { width: var(--width); background: var(--color); height: 100%; border-radius: inherit; }
</style>

The default falls back to the site token, so the component still follows the theme unless a caller overrides it. Use define:vars for these one-off values only; site-wide tokens belong in the stylesheet. The define:vars tip covers its quirks.

A plain style attribute works too and is sometimes clearer: <div style={`--width: ${width}`}>. Either way, the CSS reads the variable, and the markup supplies it.

Tokens and Tailwind CSS

Tailwind CSS 4 is built on CSS variables. Its @theme block defines tokens and generates utilities from them, so you can keep one source of truth for both:

/* src/styles/global.css */
@import "tailwindcss";
@import './tokens.css';

@theme inline {
  --color-page: var(--color-bg);
  --color-panel: var(--color-surface);
  --color-ink: var(--color-text);
  --color-brand: var(--color-accent);
  --radius-card: var(--radius-lg);
}

Now bg-panel, text-brand and rounded-card exist as utilities, and they follow your dark theme automatically because they resolve to the semantic variables. The inline keyword makes Tailwind write the var() reference into each utility instead of resolving it once, which is what keeps theme overrides working. Give the Tailwind names (--color-panel) different names from your own tokens (--color-surface); a variable that references itself is invalid CSS. The Tailwind CSS in Astro guide covers the rest of the setup.

Naming conventions that last

Token names outlive designs, so pick a pattern and stick to it:

  • Category first: --color-, --space-, --text-, --radius-, --shadow-. Autocomplete groups them, and you can tell what a variable holds at a glance.
  • Purpose over appearance for semantic tokens: --color-danger, not --color-red. Red might become orange.
  • Numbers for scales: --space-4 or --text-2xl, so adding a step in between does not force renames.
  • No component names in global tokens: --color-surface, not --card-bg, unless it is truly a component token.

Document the tokens in a comment at the top of tokens.css, or render them on a hidden /styleguide/ page so designers can check values in the browser. The broader trade-offs between styling approaches are in the Astro styling guide, and MDN’s custom properties reference is the best spec-level explanation of inheritance and fallbacks.

Starting from a finished design is often faster than building a token set from nothing. Each of the Astro themes in the catalog comes with its own palette and type scale that you can remap to your brand.

FAQ

Do CSS variables work with Astro scoped styles?

Yes. Custom properties are inherited through the DOM, and Astro’s scoping only rewrites selectors, so a variable defined on :root is available in every component’s scoped <style> block.

Where should I define global CSS variables in Astro?

In a stylesheet such as src/styles/tokens.css, imported by global.css or directly in your base layout’s frontmatter. Every page using that layout gets the tokens.

What is the difference between define:vars and CSS variables in Astro?

define:vars is an Astro directive that converts frontmatter values into CSS variables for one component’s style block. Regular CSS variables are defined in your stylesheets and are usually site-wide.

How do I change CSS variables with JavaScript in Astro?

Call document.documentElement.style.setProperty('--color-accent', '#db2777'), or set a data attribute like data-theme and let CSS swap the token values. The attribute approach keeps the values in CSS where they are easier to maintain.

Are CSS variables bad for performance?

No. Browsers resolve custom properties very quickly, and using them does not add JavaScript. The only cost to watch is changing a variable on :root during animations, which forces the whole page to recalculate styles.