Customizing Spacing Scale
What you'll learn: How to define your own spacing values in Tailwind CSS that control padding, margin, width, height, and gap utilities throughout your entire project.
Understanding the Spacing Scale
Tailwind CSS provides numbered spacing utilities like p-4, m-8, or w-16 out of the box. These numbers don't directly represent pixels—they reference a spacing scale defined in your configuration file. By default, Tailwind uses a numeric scale where each step represents 0.25rem (typically 4px), so p-4 means "padding of 1rem (16px)."
Think of the spacing scale like a ruler with preset markings. Instead of measuring custom distances every time, you pick from consistent intervals that create visual harmony across your design.
Customizing Your Spacing Values
In your tailwind.config.js file, you can override or extend the default spacing scale under the theme.spacing key:
module.exports = {
theme: {
spacing: {
'xs': '0.5rem',
'sm': '1rem',
'md': '1.5rem',
'lg': '2rem',
'xl': '3rem',
}
}
}
This replaces the default numeric scale entirely. Now you'd use classes like p-md or m-lg instead of p-4 or m-8.
To add to the existing scale without replacing it, use extend:
module.exports = {
theme: {
extend: {
spacing: {
'128': '32rem',
'custom': '42px',
}
}
}
}
Now you have access to both the default scale and your custom values like p-128 or w-custom.
Where Spacing Applies
Your customized spacing scale affects: padding, margin, width, height, maxWidth, maxHeight, gap, space, inset, and more—any utility that involves dimensional spacing.
Key Takeaway: Customizing Tailwind's spacing scale in your config file lets you define a consistent system of spatial values that automatically apply across all spacing-related utilities in your project.