Skip to main content

Sizes in code

September 11, 2025

HTML data attributes and css custom properties (variables) can be used to define the size mode of a component. This works for all components and regular HTML elements, with a few exceptions noted below.

The size mode affects our size variables (--ds-size-*) and font-size variables (--ds-font-size-* and --ds-*-font-size-*).

Setting size mode with data-size

This data attribute is used to change the size mode, which affects the element itself and all descendant elements. We support three size modes out of the box: sm, md and lg. By default, md will be used.

Setting size mode with css custom properties

Instead of setting a specific size mode in the markup, you can set the css custom property --ds-size. The advantage of this is it allows you to set the size mode based on a media query or container query.

This is done by defining --ds-size: var(--ds-size--<mode>) where <mode> is either sm, md or lg. Note that for an element to be affected, the data-size attribute also needs to be set, but it can be blank or set to anything other than "sm", "md" or "lg".

Example: Adding an "auto" size mode

Exceptions

For certain components, data-size doesn't set the size mode, but instead only sets the component size. This is the case for

  • Avatar
  • Heading
  • Paragraph
  • Spinner

These components support other sizes, such as xs and xl. Refer to the component's documentation to see exactly which sizes are supported.

They are still affected by the surrounding size mode, e.g. in this example the first paragraph will be smaller than the second:

It is confusing that data-size means size mode in certain contexts, and component size in other contexts. This will likely be remedied in the next major version as a breaking change.

Defining custom size modes

Under the hood, our variables change based on the local value of --ds-size-mode-font-size, and use rem for scaling based on the user's browser settings. data-size="<mode>" and --ds-size: var(--ds-size--<mode>) work by setting --ds-size-mode-font-size to values which correspond to the same size modes in Figma, but you can also set this variable directly for custom sizing.

Note that for an element to be affected, the data-size attribute also needs to be set, but it can be blank or set to anything other than "sm", "md" or "lg".

Edit this page on github.com (opens in a new tab)