Skip to content

Material 3

Theme

Spacing

The spacing scale and density.

Use a step of the Material 3 spacing scale, Space 0 to Space 900, with any of Tailwind's spacing and sizing utilities, as in p-space-200 or gap-space-100. Every step has a second form that follows the density of the element, for a component that people can make more compact.

Spacing scale

The theme's spaces follow Material's spacing system: a scale of 8dp units, where Space 100 is 8dp and the number of a step is its multiple of that unit. The steps are 2dp apart up to Space 200, and 4dp or 8dp apart from there.

Use a step as space-<n> after any of Tailwind's spacing and sizing utilities: p-space-100, gap-space-300, size-space-500, -mt-space-50.

space-00dp
space-252dp
space-504dp
space-756dp
space-1008dp
space-12510dp
space-15012dp
space-17514dp
space-20016dp
space-25020dp
space-30024dp
space-40032dp
space-45036dp
space-50040dp
space-60048dp
space-70056dp
space-80064dp
space-90072dp
NameTheme variableDensity formSize
space-0--spacing-space-0--spacing-space-0-variable0
space-25--spacing-space-25--spacing-space-25-variable2dp
space-50--spacing-space-50--spacing-space-50-variable4dp
space-75--spacing-space-75--spacing-space-75-variable6dp
space-100--spacing-space-100--spacing-space-100-variable8dp
space-125--spacing-space-125--spacing-space-125-variable10dp
space-150--spacing-space-150--spacing-space-150-variable12dp
space-175--spacing-space-175--spacing-space-175-variable14dp
space-200--spacing-space-200--spacing-space-200-variable16dp
space-250--spacing-space-250--spacing-space-250-variable20dp
space-300--spacing-space-300--spacing-space-300-variable24dp
space-400--spacing-space-400--spacing-space-400-variable32dp
space-450--spacing-space-450--spacing-space-450-variable36dp
space-500--spacing-space-500--spacing-space-500-variable40dp
space-600--spacing-space-600--spacing-space-600-variable48dp
space-700--spacing-space-700--spacing-space-700-variable56dp
space-800--spacing-space-800--spacing-space-800-variable64dp
space-900--spacing-space-900--spacing-space-900-variable72dp

One dp is one px: p-space-100 is 8dp. The spaces are in px, so they keep their size when the reader's font size changes. A step's density form is the theme variable of its space-<n>-variable name: see Density.

A space is a value of Tailwind's spacing scale, and is used like any other:

  • Utilities: Use a space with every utility that takes a spacing value: padding, margin, gap, width, height and size-*, inset, translate and scroll margin.
  • Negative values: Put a dash before the utility, as in -mt-space-50, where Tailwind takes a negative value.
  • Tailwind's scale: A bare number is Tailwind's own scale: p-4 is 1rem. Use the two side by side. For a size past Space 900, use Tailwind's scale, such as h-72.
  • Window size: Add a breakpoint variant for another space in a larger window, as in p-space-200 expanded-screen:p-space-300.
  • Padding and gaps: Material recommends padding and gaps on a parent over margins on its children.
  • Your own CSS: Apply a space with Tailwind's @apply, as in @apply p-space-200, or use its theme variable in a stylesheet that Tailwind compiles, as in var(--spacing-space-200).
  • Merged classes: tailwind-merge does not know the scale, and keeps both of two classes of one utility, such as p-space-100 p-space-200. To merge them, use the tv or the twMerge that @material-web/react exports, where the later class replaces the earlier one.

Density

Density makes a component shorter by taking from its top and bottom padding, 4dp of height a step, as Material's density scale does. To make a box follow the density, pad it with space-<n>-variable, which is Space <n> less half a density step. Use it with any spacing utility, as in py-space-125-variable or mt-space-300-variable.

Set the step with density-1, density-2 or density-3, on the element or on any element around it. Without one, a -variable space equals the plain one. A density class sets the --density custom property, which every -variable space reads.

40dp36dp32dp28dp
import { twMerge } from '@material-web/react'

const pill =
  'rounded-space-125-variable bg-secondary-container px-space-200 py-space-125-variable text-label-lg text-on-secondary-container'

export default function Density() {
  return (
    <>
      <span className={pill}>40dp</span>
      <span className={twMerge(pill, 'density-1')}>36dp</span>
      <span className={twMerge(pill, 'density-2')}>32dp</span>
      <span className={twMerge(pill, 'density-3')}>28dp</span>
    </>
  )
}
Class--densityMaterial's densityHeight taken
density-000, the defaultNone
density-1-0.25rem-14dp
density-2-0.5rem-28dp
density-3-0.75rem-312dp

A step is 0.25rem, Tailwind's spacing unit: 4dp at the browser's default font size of 16px.

  • Scope: A density reaches everything inside the element it is set on. Use density-0 to return a part of a denser area to the default.
  • Your own CSS: Apply a density space with Tailwind's @apply, as in @apply py-space-125-variable. Read as a variable, as in var(--spacing-space-125-variable), it has the root's density, not the element's.
  • Padding only: A density changes the padding alone. The text keeps its size, and a padding stops at 0, so a box is never shorter than its content.
  • Round corners: Use rounded-space-<n>-variable for the round corner of a box padded with it, at any density. See Shape.
  • Components: The components whose height comes from their padding, such as a Button or a ListItem, follow the density: density-2 on a toolbar makes every button in it 8dp shorter. See Styling.
  • Targets: A denser control is a smaller target. Material advises against applying density by default where it brings a target under 48 by 48dp: let people choose a denser layout instead. To keep the target of a smaller control at 48dp, add touch-target.

Custom spacing

Every step of the scale is one of Tailwind's theme variables, named --spacing-space-<n>: the scale's table lists each with its size. Set a variable in @theme, after the import, to add a step of your own. Material names a new step by its multiple of 8dp, so 18dp is Space 225:

@import 'tailwindcss';
@import '@material-web/tailwind';

@theme {
  --spacing-space-225: 18px;
}

The step then works with every spacing utility, as in p-space-225 or -mt-space-225.

A step of your own has no -variable form. To add one, define it in @theme inline, with the step's size in place of 18px:

@theme inline {
  --spacing-space-225-variable: calc(18px + var(--density, 0px) / 2);
}

The density has two custom properties of its own:

Custom propertyDefaultSets
--densityNot set, which reads as 0pxThe height a density takes from a padded box, as a negative length. A density-<n> class sets it to <n> steps
--spacing0.25remTailwind's own spacing unit: one density step, and the base of Tailwind's numeric classes, such as p-4
  • Steps of the scale: To change a step of the scale, set its variable in @theme as well, as in --spacing-space-100: 10px. The change reaches every class of that step, and the components that use it. The step's -variable form and its round corner keep the default size.
  • One component: To change the spacing of one component instead, pass a spacing class in its className, as in <Button className="px-space-400">.