Spacing tokens are a cornerstone of modern design systems, acting as the single source of truth for defining whitespace—the margins, paddings, and gaps that structure your user interface. Implementing them effectively in both design tools like Figma and in front-end code like CSS is crucial for achieving visual consistency, improving user experience through clear hierarchy, and streamlining the collaboration between designers and developers. This guide provides a comprehensive approach to defining, applying, and managing spacing tokens across both platforms.
Spacing tokens are essentially named variables that store specific spacing values (e.g., '8px', '1rem'). Instead of hardcoding pixel values throughout your designs and stylesheets, you reference these tokens. This approach brings several advantages:
Typically, a design system defines a spacing scale based on a core unit, commonly 4px or 8px. This scale provides a range of predefined steps (e.g., 4px, 8px, 12px, 16px, 24px, 32px) used for all spatial properties like margin, padding, and gap.
Example illustrating the application of spacing tokens for consistent vertical stacking (Source: Carbon Design System).
In CSS, spacing tokens are most effectively implemented using custom properties (variables). This allows for dynamic, reusable, and easily maintainable spacing rules.
Define your tokens within the :root pseudo-class to make them globally available. Choose a base unit (e.g., 8px) and create a scale based on multiples or a consistent progression. Using rem units is often recommended for accessibility and scalability, assuming a standard base font size (e.g., 16px).
:root {
/* Base scale using 4px increments (relative to 16px root font size) */
--space-100: 0.25rem; /* 4px */
--space-200: 0.5rem; /* 8px */
--space-300: 0.75rem; /* 12px */
--space-400: 1rem; /* 16px */
--space-500: 1.5rem; /* 24px */
--space-600: 2rem; /* 32px */
--space-800: 3rem; /* 48px */
--space-1000: 4rem; /* 64px */
/* Optional Semantic Tokens (Aliases) */
--space-inset-squish: var(--space-200) var(--space-400); /* 8px 16px (e.g., for buttons) */
--space-stack-medium: var(--space-500); /* 24px (e.g., margin between sections) */
}
You can use primitive tokens (like --space-400) directly or create semantic tokens (like --space-stack-medium) that alias primitive values for specific use cases. Semantic tokens add context but can increase complexity if overused. Primitive tokens offer more flexibility.
Use the defined CSS variables with the var() function to apply spacing values to elements.
.card {
padding: var(--space-500); /* 24px padding on all sides */
margin-bottom: var(--space-stack-medium); /* Uses the semantic token for bottom margin */
background-color: #f0f0f0;
border-radius: var(--space-200); /* Can also use space tokens for border-radius */
}
.button {
padding: var(--space-inset-squish); /* Applies 8px top/bottom, 16px left/right */
margin-right: var(--space-300); /* 12px right margin */
}
.icon-group {
display: flex;
gap: var(--space-200); /* 8px gap between flex items */
}
.grid-layout {
display: grid;
gap: var(--space-600); /* 32px gap between grid items */
}
Adjust spacing for different screen sizes by redefining CSS variables within media queries.
/* Default spacing */
:root {
--space-page-padding: var(--space-500); /* 24px */
}
/* Increase padding on larger screens */
@media (min-width: 768px) {
:root {
--space-page-padding: var(--space-800); /* 48px */
}
}
.page-container {
padding: var(--space-page-padding);
}
For internationalization (supporting right-to-left languages), use logical properties (margin-inline-start, padding-block-end, etc.) with your spacing tokens. This ensures spacing adapts correctly based on writing mode.
.element {
/* Instead of padding-left */
padding-inline-start: var(--space-400); /* 16px padding at the start edge */
/* Instead of margin-bottom */
margin-block-end: var(--space-600); /* 32px margin at the end in the block direction */
}
Figma offers robust ways to manage spacing tokens, primarily through its native Variables feature and powerful community plugins like Tokens Studio.
Figma's Variables allow you to define reusable values, including numbers for spacing. You can create these in the "Local variables" panel:
spacing/400).16).spacing/primitive, spacing/semantic).Apply these number variables to relevant properties in Figma, especially within Auto Layout frames:
Figma Variables also support modes, allowing you to define different spacing values for various contexts (e.g., compact vs. comfortable density, mobile vs. desktop).
Plugins like Tokens Studio offer more advanced features for token management, including multi-value spacing support (mimicking CSS shorthand), better organization, theming, and direct export to code formats.
space.100 with value 4px or 0.25rem).8px 16px, which can be applied to padding.Select a layer or Auto Layout frame in Figma:
spacing, paddingTop, gap).This video demonstrates setting up spacing tokens using Figma variables and potentially plugins.
This mindmap illustrates the key concepts and relationships involved in implementing spacing tokens across CSS and Figma.
While both CSS and Figma enable spacing token implementation, their approaches and strengths differ. This chart provides a comparative overview based on common criteria:
This radar chart compares CSS Custom Properties, Figma's native Variables, and Figma Plugins like Tokens Studio across several implementation aspects. Higher scores indicate better performance or ease of use in that specific category. For instance, CSS excels in responsiveness handling and direct code output, while plugins offer strong multi-value support and collaboration features.
The ultimate goal is consistency between design and code. Establishing a workflow to sync tokens is key:
This process establishes your design tool as the single source of truth for spacing values, minimizing discrepancies and manual effort.
Different design systems adopt various scales and naming conventions. Here's a comparison of a few examples:
| System | Example Token Name | Typical Value (px) | Typical Value (rem @ 16px root) | Base Unit Philosophy |
|---|---|---|---|---|
| USWDS | --space-1 / units-1 |
8px | 0.5rem | 8px multiples |
| Shopify Polaris | --p-space-100 / space-100 |
4px | 0.25rem | 4px multiples |
| Atlassian Design System | space.100 |
8px | 0.5rem | ~8px scale, with finer steps |
| Carbon Design System | $spacing-03 / --cds-spacing-03 |
8px | 0.5rem | 8px multiples (steps based on Fibonacci sequence) |
| Tailwind CSS (Default) | space-2 / p-2 |
8px | 0.5rem | 4px multiples (0.25rem) |
This table shows that while the base unit (often 4px or 8px) and scale progression differ slightly, the core concept of using named tokens for predefined spacing values is consistent across major systems.
While both 4px and 8px are common, an 8px base unit (or 0.5rem) is frequently adopted by many major design systems like USWDS, Carbon, and Atlassian. It provides a good balance between granularity and distinct steps, often leading to more harmonious vertical rhythm. However, a 4px base allows for finer control if needed.
Multi-value tokens mimic CSS shorthand properties like `padding` or `margin`.
It depends on your team's needs and complexity:
Many teams start with Variables for basic tokens and adopt plugins when they need more advanced features or tighter code integration.
Yes, absolutely! While margin, padding, and gap are the primary use cases, spacing tokens can consistently define values for:
border-radius (using smaller spacing tokens for corner consistency)outline-offsettext-indenttop, right, bottom, left)Using them for these properties helps maintain the overall spatial rhythm of the design system.