Home / Articles / Blog

Design Tokens Explained: Colors, Spacing and Type with Style Dictionary
Design TokensStyle DictionaryCSS VariablesDark ModeDesign SystemsTutorial

Design Tokens Explained: Colors, Spacing and Type with Style Dictionary

Learn primitive and semantic design tokens, then build a working light and dark theme with Style Dictionary 5.8.0. Includes JSON source files, a build script, generated CSS variables and a browser demo.

DesignShrey Vijayvargiya

Your brand yellow is #ffd21f.

It's in the button, the banner, the focus ring, the pricing badge and about thirty other places. Then someone says, "Make it a bit warmer," and you spend an afternoon searching through CSS files, changing hex values and hoping you didn't miss one.

Design tokens fix that.

A token is a named design decision: color.yellow.500, space.4, radius.md. You define the value once, then other parts of your interface refer to it by name.

Change the underlying value and every place that uses it gets the update.

This tutorial explains the two token types that matter most, then builds a small working project with Style Dictionary 5.8.0. The project uses JSON as its source, generates CSS variables for light and dark themes, and also produces JavaScript constants.

The commands and output below are from the project run described in this tutorial. The goal is to give you something you can copy, run and adapt.

The short version

  • A design token is a named value for a design decision: a colour, spacing step, radius or font size.
  • Primitive tokens hold raw values, such as color.ink.900.
  • Semantic tokens explain what a value is used for, such as text.primary or surface.page.
  • Components should use semantic tokens. They shouldn't need to know which exact hex value represents the page background.
  • Style Dictionary transforms token files into CSS variables, JavaScript constants and other platform formats.
  • Dark mode can use the same semantic names with different values. Your components don't need separate light and dark styles.
  • The Design Tokens Community Group published the first stable version of its specification in October 2025. Read the announcement.

What a token is, in one picture

text
  primitive tokens             semantic tokens             components
  (what exists)                (what it's for)             (what uses it)

  color.cream.50    ───────►   surface.page      ───────►  <body>
  color.ink.900     ───────►   text.primary      ───────►  <h1>, <p>
  color.yellow.500  ───────►   surface.accent    ───────►  <button>
  color.ink.300     ───────►   border.subtle     ───────►  <card>
  space.2, space.4  ───────►   button.padding    ───────►  <button>
  radius.md         ───────►   button.radius     ───────►  <button>

The primitive column describes the values available in your palette.

The semantic column describes the job each value performs.

The component column is where those decisions are used.

Dark mode changes the values behind the semantic tokens, not the components on the right. In light mode, surface.page points to cream. In dark mode, it points to near-black.

The page doesn't need to know why.

What we'll build

We'll create a small project with separate files for raw values, theme-specific values and the build script.

FileWhat it holds
tokens/base.jsonPrimitive colours, spacing, radius and font sizes
tokens/semantic.light.jsonSemantic tokens for light mode
tokens/semantic.dark.jsonDark-mode overrides for semantic tokens
build.mjsStyle Dictionary configuration and two builds
dist/tokens.cssPrimitive and light-theme CSS variables
dist/tokens.dark.cssDark-theme CSS overrides
dist/tokens.jsJavaScript constants
index.htmlA card and button that use the generated variables

Stack: Node 22 and Style Dictionary 5.8.0 (Apache-2.0). No framework or additional build tool is required.

The project uses multiple source files deliberately. Keeping primitives separate from theme-specific semantic values makes it easier to add another theme or output format later.

Step 1: Install

Create the project and install Style Dictionary:

bash
mkdir design-tokens-starter
cd design-tokens-starter

npm init -y
npm i style-dictionary@5.8.0

In package.json, add "type": "module" so the build script can use ES module imports.

Your package file should include something like:

json
{
  "type": "module",
  "scripts": {
    "build": "node build.mjs"
  },
  "dependencies": {
    "style-dictionary": "5.8.0"
  }
}

You can now run the build later with:

bash
npm run build

Step 2: Define primitive tokens

Primitive tokens are the raw ingredients of your design system.

They don't describe where a value will be used. They simply give the value a name.

Create tokens/base.json:

json
{
  "color": {
    "$type": "color",
    "ink": {
      "900": { "$value": "#0d0d0c" },
      "600": { "$value": "#4a4945" },
      "300": { "$value": "#a09e97" }
    },
    "cream": {
      "50": { "$value": "#f4f1ea" },
      "100": { "$value": "#e9e4d8" }
    },
    "yellow": {
      "500": { "$value": "#ffd21f" }
    }
  },
  "space": {
    "$type": "dimension",
    "1": { "$value": "4px" },
    "2": { "$value": "8px" },
    "4": { "$value": "16px" },
    "6": { "$value": "24px" },
    "8": { "$value": "32px" }
  },
  "radius": {
    "$type": "dimension",
    "sm": { "$value": "6px" },
    "md": { "$value": "12px" }
  },
  "font": {
    "size": {
      "$type": "dimension",
      "body": { "$value": "16px" },
      "h2": { "$value": "28px" },
      "h1": { "$value": "44px" }
    }
  }
}

A few things to notice:

  • color.ink.900 is a primitive colour.
  • space.4 is a spacing value of 16px.
  • radius.md is a radius value of 12px.
  • $type defines the kind of token.
  • $value contains the actual value.

The $type and $value properties follow the Design Tokens Community Group format. The values here use simple strings such as "16px" and "#ffd21f".

Step 3: Define semantic tokens for light mode

Now give those values a purpose.

Create tokens/semantic.light.json:

json
{
  "surface": {
    "$type": "color",
    "page": {
      "$value": "{color.cream.50}"
    },
    "card": {
      "$value": "#ffffff"
    },
    "accent": {
      "$value": "{color.yellow.500}"
    }
  },
  "text": {
    "$type": "color",
    "primary": {
      "$value": "{color.ink.900}"
    },
    "secondary": {
      "$value": "{color.ink.600}"
    },
    "on-accent": {
      "$value": "{color.ink.900}"
    }
  },
  "border": {
    "$type": "color",
    "subtle": {
      "$value": "{color.ink.300}"
    }
  },
  "button": {
    "padding-y": {
      "$type": "dimension",
      "$value": "{space.2}"
    },
    "padding-x": {
      "$type": "dimension",
      "$value": "{space.4}"
    },
    "radius": {
      "$type": "dimension",
      "$value": "{radius.md}"
    }
  }
}

The curly braces are references.

For example:

json
{
  "$value": "{color.cream.50}"
}

means: use the value defined by color.cream.50.

This is the important distinction:

  • color.cream.50 says which colour exists.
  • surface.page says what that colour is being used for.

Why does that matter?

Imagine you decide that the page background should be slightly darker. You can change the value behind surface.page without searching through every component that uses the page background.

The same applies to text.primary, border.subtle and surface.accent.

Components can use those semantic names without knowing the primitive palette.

Step 4: Add the dark theme

Create tokens/semantic.dark.json:

json
{
  "surface": {
    "$type": "color",
    "page": {
      "$value": "{color.ink.900}"
    },
    "card": {
      "$value": "#1b1b19"
    },
    "accent": {
      "$value": "{color.yellow.500}"
    }
  },
  "text": {
    "$type": "color",
    "primary": {
      "$value": "{color.cream.50}"
    },
    "secondary": {
      "$value": "{color.ink.300}"
    },
    "on-accent": {
      "$value": "{color.ink.900}"
    }
  },
  "border": {
    "$type": "color",
    "subtle": {
      "$value": "#4a4945"
    }
  }
}

Notice what changed:

  • The page background is now near-black.
  • The card has a dark background.
  • Primary text is now cream.
  • Secondary text is lighter.
  • The border uses a darker value.
  • The yellow accent stays the same.

The button padding and radius don't change between themes, so they don't need dark-mode overrides.

The important part is that the semantic names remain consistent. Your components still ask for surface.page, text.primary and border.subtle.

Only the values behind those names change.

Step 5: Write the build script

Now we need to turn the JSON into files that the browser and JavaScript can use.

Create build.mjs:

js
import StyleDictionary from 'style-dictionary';

const base = ['tokens/base.json'];

// Light theme + primitives, written to :root
const light = new StyleDictionary({
  source: [...base, 'tokens/semantic.light.json'],
  platforms: {
    css: {
      transformGroup: 'css',
      buildPath: 'dist/',
      files: [
        {
          destination: 'tokens.css',
          format: 'css/variables',
          options: {
            outputReferences: true,
            selector: ':root'
          }
        }
      ]
    },
    js: {
      transformGroup: 'js',
      buildPath: 'dist/',
      files: [
        {
          destination: 'tokens.js',
          format: 'javascript/es6'
        }
      ]
    }
  }
});

// Dark theme: semantic overrides only.
const dark = new StyleDictionary({
  source: [...base, 'tokens/semantic.dark.json'],
  platforms: {
    css: {
      transformGroup: 'css',
      buildPath: 'dist/',
      files: [
        {
          destination: 'tokens.dark.css',
          format: 'css/variables',
          filter: (token) =>
            ['surface', 'text', 'border'].includes(token.path[0]),
          options: {
            outputReferences: true,
            selector: '[data-theme="dark"]'
          }
        }
      ]
    }
  }
});

await light.buildAllPlatforms();
await dark.buildAllPlatforms();

There are two builds here.

The light build

  • Loads the primitive tokens and light semantic tokens.
  • Generates CSS variables under :root.
  • Generates JavaScript constants in tokens.js.

The dark build

  • Loads the same primitives plus dark semantic tokens.
  • Generates only the theme-specific semantic overrides.
  • Scopes those overrides to [data-theme="dark"].

The primitives are included in the dark build's source so that references can resolve. The filter keeps them out of the generated dark CSS file because they're already available from tokens.css.

One important option is:

js
outputReferences: true

This preserves references in the generated CSS.

Instead of copying a primitive value into every semantic token, the output can retain the relationship:

css
--surface-page: var(--color-cream-50);

That means the connection between the semantic token and the primitive remains visible in the CSS.

Step 6: Run the build

Run:

bash
npm run build

The output from the project run described here (Node 22.23.5 and npm 10.9.9) was:

text
css
✔︎ dist/tokens.css

js
✔︎ dist/tokens.js

css
⚠️ dist/tokens.dark.css
While building tokens.dark.css, filtered out token references were found;
output may be unexpected. Ignore this warning if intentional.

The dark-theme warning is related to the way the files are split.

The dark CSS references primitives such as:

css
var(--color-ink-900)

Those primitive variables aren't repeated in tokens.dark.css. They already exist in tokens.css, which is loaded on the page.

So the warning is expected for this setup, provided the light and dark CSS files are both loaded.

If you want each theme file to work independently, include the required primitive variables in the dark output too. That creates a more self-contained file, but also duplicates some CSS.

The generated light CSS

dist/tokens.css:

css
:root {
  --color-ink-300: #a09e97;
  --color-ink-600: #4a4945;
  --color-ink-900: #0d0d0c;
  --color-cream-50: #f4f1ea;
  --color-cream-100: #e9e4d8;
  --color-yellow-500: #ffd21f;

  --space-1: 4px;
  --space-2: 8px;
  --space-4: 16px;
  --space-6: 24px;
  --space-8: 32px;

  --radius-sm: 6px;
  --radius-md: 12px;

  --font-size-body: 16px;
  --font-size-h2: 28px;
  --font-size-h1: 44px;

  --surface-card: #ffffff;
  --surface-page: var(--color-cream-50);
  --surface-accent: var(--color-yellow-500);

  --text-primary: var(--color-ink-900);
  --text-secondary: var(--color-ink-600);
  --text-on-accent: var(--color-ink-900);

  --border-subtle: var(--color-ink-300);

  --button-padding-y: var(--space-2);
  --button-padding-x: var(--space-4);
  --button-radius: var(--radius-md);
}

The generated dark CSS

dist/tokens.dark.css:

css
[data-theme="dark"] {
  --surface-card: #1b1b19;
  --surface-page: var(--color-ink-900);
  --surface-accent: var(--color-yellow-500);

  --text-primary: var(--color-cream-50);
  --text-secondary: var(--color-ink-300);
  --text-on-accent: var(--color-ink-900);

  --border-subtle: #4a4945;
}

The generated JavaScript

dist/tokens.js contains JavaScript constants with resolved values.

The first lines look like this:

js
export const ColorInk300 = "#a09e97";
export const ColorInk600 = "#4a4945";
export const ColorInk900 = "#0d0d0c";
export const SurfacePage = "#f4f1ea";
export const ButtonRadius = "12px";

This is useful for values that CSS can't reach directly.

For example, a chart library might need a JavaScript colour value rather than a CSS variable. You can import the generated constants instead of maintaining a second colour palette.

Step 7: Use the tokens in a page

Now let's use the generated files.

Create index.html:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Design Tokens Demo</title>

  <link rel="stylesheet" href="dist/tokens.css">
  <link rel="stylesheet" href="dist/tokens.dark.css">

  <style>
    body {
      margin: 0;
      padding: var(--space-8);
      background: var(--surface-page);
      color: var(--text-primary);
      font-family: Arial, sans-serif;
    }

    .card {
      max-width: 420px;
      padding: var(--space-6);
      background: var(--surface-card);
      border: 1px solid var(--border-subtle);
      border-radius: var(--radius-md);
    }

    p {
      color: var(--text-secondary);
    }

    button {
      padding: var(--button-padding-y) var(--button-padding-x);
      border: 0;
      border-radius: var(--button-radius);
      background: var(--surface-accent);
      color: var(--text-on-accent);
      cursor: pointer;
    }
  </style>
</head>

<body>
  <main class="card">
    <h1>Design tokens are working</h1>
    <p>
      This card and button use generated CSS variables.
      Switch themes without changing the component styles.
    </p>
    <button>Get started</button>
  </main>

  <script>
    function setTheme(theme) {
      document.documentElement.dataset.theme = theme;
    }

    // Try setTheme('dark') or setTheme('light') in the console.
  </script>
</body>
</html>

Switch themes by setting the attribute:

js
document.documentElement.dataset.theme = 'dark';

And switch back:

js
document.documentElement.dataset.theme = 'light';

The same HTML and component CSS work in both themes.

Only the semantic variables change.

For a production application, you can connect this to a theme toggle, save the user's preference and decide whether to use the operating system's preferred colour scheme by default.

What the browser demo should show

Light theme

  • Cream page background.
  • White card.
  • Dark primary text.
  • Yellow button.

Dark theme

  • Near-black page background.
  • Dark card.
  • Cream primary text.
  • The same yellow button.

Add your actual rendered screenshots here once they're hosted:

The important part isn't the visual difference. It's that the same component markup works in both themes.

Check the contrast before you trust a palette

A token system makes it easy to change a colour everywhere.

That also makes it easy to ship a bad colour everywhere.

Before using a palette, check the text and background combinations against the WCAG 2.2 contrast requirements.

For WCAG AA:

  • Normal text needs at least 4.5:1.
  • Large text needs at least 3:1.

Large text means at least 18 pt regular or 14 pt bold.

I computed the contrast ratios for the colour pairs in this demo:

PairRatioPasses 4.5:1?
Light: primary text on page17.24:1Yes
Light: secondary text on page7.99:1Yes
Light: secondary text on card9.01:1Yes
Dark: primary text on page17.24:1Yes
Dark: secondary text on page7.25:1Yes
Dark: secondary text on card6.43:1Yes
Button text on yellow13.42:1Yes

The contrast calculation uses the WCAG relative-luminance formula.

The project includes a contrast.py script with the hex values used by the tokens. If you keep that script in your repository, you can use it to check the palette again when values change.

A useful next step is to make contrast checks part of your build process. A failing text/background pair should be caught before the interface ships, not after someone notices it.

One qualification: this table checks the listed text and background pairs. It isn't a complete accessibility audit of the page. Focus indicators, borders, controls and other interface states need their own checks.

How to make the token system better

The example is intentionally small. A real product will probably need more than colours, spacing and radii.

Here are a few sensible extensions.

1. Add more component-level tokens

The example already has button.padding-y, button.padding-x and button.radius.

You can add:

text
card.padding
input.border
input.radius
input.background
button.background
button.text
navigation.item-gap

Keep these connected to semantic or primitive tokens instead of repeating raw values.

2. Respect the user's theme preference

You can use @media (prefers-color-scheme: dark) to apply dark tokens when the operating system prefers dark mode.

Keep [data-theme="dark"] for a manual override.

If you support both, define a clear precedence so a saved user preference doesn't unexpectedly fight the operating system setting.

3. Generate more outputs

Style Dictionary can generate token files for other platforms and formats.

This example only builds CSS and JavaScript. A larger design system might also need outputs for mobile applications or other tools.

4. Name tokens by their job, not just their value

surface.accent can survive a rebrand.

yellow describes a colour, not what that colour means in the interface.

Keep primitive names tied to the palette and semantic names tied to usage.

5. Add validation

As the token collection grows, add checks for:

  • Unknown references.
  • Missing semantic values.
  • Duplicate or unused tokens.
  • Invalid colour values.
  • Contrast failures.

A token file is part of your application's source code. Treat it like code.

When to use a hosted token tool instead

You don't need a hosted tool for every project.

Keeping JSON in your repository works well when developers own the design system and changes happen through code reviews.

A different workflow makes sense when designers edit tokens visually and expect those changes to reach the codebase.

A few tools worth looking at:

  • Style Dictionary: Transform token files into platform-specific outputs.
  • Tokens Studio: Manage design tokens in a design-tool workflow.
  • Terrazzo: Another option for working with design tokens and generating outputs.

The Design Tokens Community Group lists reference implementations and related resources in its specification materials.

If your design work lives in Figma, you may also want to read Figma MCP: Connect Design Files to Claude Code.

For a comparison of component libraries and design systems that developers can build on, see 12 Open-Source Design Systems and UI Libraries Developers Should Know in 2026.

What I ran, and what I didn't

It's useful to be clear about what this tutorial demonstrates.

Ran:

  • Installation of Style Dictionary 5.8.0.
  • The build script using Node 22.23.5.
  • Generation of the CSS and JavaScript files shown above.
  • The browser demo using the generated light and dark CSS variables.
  • The contrast calculation for the listed colour pairs.

Not covered in this project:

  • A Figma-to-tokens sync.
  • iOS or Android outputs.
  • A complete accessibility audit.
  • Every newer value format in the stable 2025.10 specification.

The token values here use plain strings such as "16px" and "#ffd21f". If you're migrating an existing token collection, check how your installed version of Style Dictionary handles the formats you plan to use.

Project repository

The project folder is design-tokens-starter.

It should contain:

text
design-tokens-starter/
├── tokens/
│   ├── base.json
│   ├── semantic.light.json
│   └── semantic.dark.json
├── dist/
│   ├── tokens.css
│   ├── tokens.dark.css
│   └── tokens.js
├── build.mjs
├── index.html
├── contrast.py
└── package.json

Repository: [ADD REPOSITORY URL AFTER PUSHING]

If you publish it, include the generated output as well as the source files. Readers should be able to compare the JSON with the CSS that Style Dictionary produces.

FAQ

What are design tokens?

Design tokens are named values for design decisions, such as colours, spacing, radii and font sizes. You define a value once and reference it by name wherever it's needed.

What's the difference between primitive and semantic tokens?

Primitive tokens are raw values, such as color.ink.900. Semantic tokens describe what a value is used for, such as text.primary. Components should use semantic tokens so a theme change doesn't require rewriting component styles.

Do I need Style Dictionary?

No. You can write CSS variables by hand.

A build tool becomes useful when you want the same source tokens to generate CSS and JavaScript, support multiple platforms or keep a growing token collection consistent.

How do I implement dark mode with design tokens?

Keep the semantic names the same and give them different values in a dark theme file. Generate CSS overrides under a selector such as [data-theme="dark"]. Your components can keep using the same variables.

Is there a standard format for design tokens?

The

External Links (3)

Explore topics

More in

Weekly letter

Subscribe to iHateReading

Our once-a-week newsletter on programming, jobs, AI, and building products online.

Our once a week newsletter on Programming, Jobs, AI, and Business