Getting started
CSS
Get started with CSS including layers, data attributes and helpers.
Layers
We use css layers to get the lowest possible specificity in our css. This means that you can easily override it with your own styling.
All our layers are under the layer group ds.
Import @digdir/designsystemet-css before your theme, so the order of our layers (@layer ds) is set first.
For example, if you have a css reset, this will override parts of what we have done. We recommend putting this in a layer and sorting this first. You do this by first putting your reset in a layer, and then sorting this layer first:
@layer my-reset, ds;
Data attributes
Designsystemet offers several data attributes to control component themes and CSS custom property values. These attributes can be applied to any HTML element; the CSS custom property values they set are inherited by descendant elements.
data-size
Corresponds with Appearance › Size in Figma.
Sets the size of the components.
data-color
Corresponds with Appearance › Color in Figma.
Used to change the color of the components. Available values are defined by your theme.
You can read more about this under "Colors in code".
data-color-scheme
Corresponds with Appearance › Color scheme in Figma.
Used to change the color mode of the components. Possible values are light, dark and auto.
If auto is selected, the color mode will change based on the user's preferences using
prefers-color-scheme.
You can read more about this at "Colors in code".
data-typography
Used to change the typography of the components. Possible values are primary and secondary.
A common use for this attribute is when you want a monospace font on tables.
Which typography is associated with primary or secondary must be defined in the team, using Tokens Studio
(with the Designsystemet design-tokens) or Theme Builder.
If you are going to use this attribute, you must remember to define font-family to use the --ds-font-family CSS variable.
This can be solved by, for example, using attribute selector in CSS.
<style>
[data-typography='secondary'] {
font-family: var(--ds-font-family), monospace;
}
</style>
<table data-typography="secondary">
…
</table>
Helper classes
We use some helper classes that can be used more generally across components.
ds-sr-only
Visually hides an element while still being accessible to screen readers.
ds-print-preserve
Preserves appearance, including background color, in print.
ds-focus / ds-focus--inset
Adds Designsystemet's focus ring around the element when it gains focus with keyboard navigation (:focus-visible).
ds-focus--within
Adds Designsystemet's focus ring around the element when it or any of its descendants have focus (:focus-within). Focus rings on descendants are hidden, so only the outer ring is shown.
ds-focus--visible / ds-focus--visible--inset
Static "always-on" classes that add Designsystemet's focus ring around the element. Example.
Tailwind CSS
Designsystemet's command line tool can generate a Tailwind theme file next to your regular theme CSS, so Tailwind utilities use Designsystemet's design tokens instead of Tailwind's default scales.
The theme file is not generated by default. Set tailwind on the css output in your config file to the Tailwind version you use, "v4" or "v3".
For every <theme>.css you will then also get a <theme>.tailwind.css.
{
"output": [{ "type": "css", "tailwind": "v4" }, "types"]
}
Add the theme to Tailwind
Import the Tailwind theme file first, then Designsystemet's CSS, and tailwindcss last.
The Tailwind theme file starts with @layer base, tailwind-base, ds;, which puts Preflight before our components, so it does not override them.
Use @import '…' and not @import url('…'), as Tailwind only bundles plain string imports.
@import "designsystemet/<your-theme>.tailwind.css";
@import "@digdir/designsystemet-css";
@import "designsystemet/<your-theme>.css";
@import "tailwindcss";
@import "@digdir/designsystemet-css/theme/designsystemet.tailwind.css";
@import "@digdir/designsystemet-css";
@import "@digdir/designsystemet-css/theme.css";
@import "tailwindcss";
What you get
The theme file maps design tokens onto Tailwind's theme variable namespaces:
| Tokens | Namespace | Example |
|---|---|---|
--ds-color-* | --color-* | bg-accent-base-default, text-text-subtle, border-border-default |
--ds-size-* | --spacing-* | p-4, gap-2, mt-6 |
--ds-border-radius-* | --radius-* | rounded-sm, rounded-md, rounded-lg, rounded-xl |
--ds-shadow-* | --shadow-* | shadow-xs, shadow-sm, shadow-md, shadow-lg, shadow-xl |
--ds-font-weight-* | --font-weight-* | font-regular, font-medium, font-semibold |
--ds-body-(sm|md|lg)-font-size | --text-* | text-sm, text-md, text-lg |
--ds-opacity-* | --opacity-* | opacity-disabled |
--ds-font-family | --font-sans | font-sans |
Focus colours are not included, use the ds-focus helper class instead.
Values outside our scale still fall back to Tailwind's own, so p-4 uses --ds-size-4 while p-16 uses Tailwind's default spacing scale.
Data attributes
With "v4", the theme file uses @theme inline, which makes utilities
reference the --ds-* variables directly. That means our data attributes also change the Tailwind utilities:
<div data-color="brand1" data-size="sm">
<!-- background-color is brand1's base colour, and padding follows the small size mode -->
<div class="bg-base-default p-4">…</div>
</div>
This works for data-color, data-color-scheme and data-size, on any element and at any depth.
With "v3", only the colours set by data-color follow the data attributes.