Overview
The data-theme attribute on <html> sets the page’s colors. It holds a list of words: dark for dark mode and a theme name for the palette, as in data-theme="dark crimson". With no attribute, the page shows the DGA palette in light mode. A dark mode toggle and a theme menu write the attribute for the user. Your own brand colors come from a custom palette or a stylesheet theme. Dark mode is experimental: it is not checked against the DGA standards yet.
Pick another component when:
Markup
Variants
The preview controls the real page: a click changes the look of this whole page, as it would on your site.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Button toggle (default) | — | — | The dark mode button. Put it in the Top Bar, so it is on every page |
| Structure | Switch toggle | canon #theme-switch |
— | Dark mode as a setting, on a settings page |
| Structure | Theme menu | canon #theme-menu |
— | A menu of themes. Each option names its theme in data-theme-value. The empty value is the DGA default |
Behavior
Theme Menu
NDS ships three themes: crimson, corporate (Cairo font) and sunset (Readex Pro font). A click on an option writes its data-theme-value into data-theme and keeps the dark word. A second click on the chosen option goes back to the DGA default.
Each pick also writes ?theme=name in the address bar. A page that opens with that link applies the option, if a menu on the page has it.
Custom Palette
Set data-palette and the seed colors on <html>, and NDS builds the whole palette from them, dark mode included. --brand-primary is the only seed you must set. The palette needs Chrome 119, Safari 16.4 or Firefox 128. An older browser shows the DGA palette.
<html lang="ar" dir="rtl" data-palette style="--brand-primary: #7c3aed; --brand-secondary: #ec4899; --neutral-tint: 0.4;">
:root[data-palette] {
--brand-primary: #7c3aed;
--brand-secondary: #ec4899;
--neutral-tint: 0.4;
}
A theme option with data-seed-* attributes applies a custom palette on click, and saves it. Click the button to see this page in the palette. Click it again to go back to the DGA default.
Stylesheet Themes
A stylesheet theme is your own CSS file that sets the color tokens at :root. Load it with a <link> in the <head>, right after the critical CSS, so it applies before first paint.
<link rel="stylesheet" href="assets/themes/my-brand.css">
/* DGA values shown: replace them with your brand colors. The status and base colors stay DGA. */
:root {
/* Primary ramp: your brand color is the 600 step */
--colors-primary-25: #f7fdf9;
--colors-primary-50: #f3fcf6;
--colors-primary-100: #dff6e7;
--colors-primary-200: #b8eacb;
--colors-primary-300: #88d8ad;
--colors-primary-400: #54c08a;
--colors-primary-500: #25935f;
--colors-primary-600: #1b8354;
--colors-primary-700: #166a45;
--colors-primary-800: #14573a;
--colors-primary-900: #104631;
--colors-primary-950: #092a1e;
/* Primary tints: chips, table selection, footer */
--colors-primary-alpha-10: #1b835419;
--colors-primary-alpha-20: #1b835433;
--colors-primary-alpha-30: #1b83544c;
--colors-primary-alpha-40: #1b835466;
--colors-primary-alpha-50: #1b83547f;
--colors-primary-alpha-60: #1b835499;
--colors-primary-alpha-70: #1b8354b2;
--colors-primary-alpha-80: #1b8354cc;
--colors-primary-alpha-90: #1b8354e5;
/* Set the secondary, tertiary (500 step) and neutral ramps the same way. The names are on the Tokens page */
/* Deep brand surfaces: hero, footer, image overlay */
--background-primary-strong: var(--colors-primary-900);
--background-primary-light: var(--colors-primary-50);
--background-image-overlay: var(--colors-primary-950);
}
/* Dark mode fixes. :root:root wins wherever this sheet loads. The second selector reaches dark areas, and wins only after the critical CSS. */
:root:root[data-theme~="dark"],
[data-theme~="dark"]:not(:root) {
--background-card: #1f2a37;
--background-footer: #0c111b;
}
A theme option can load a stylesheet theme on click: data-theme-css names the CSS file, and data-theme-js names a script. The event themes are stylesheet themes that load from one script tag.
A stylesheet theme can also place your brand art, such as a pattern, a divider or a footer graphic. Put it on the root of a section or the footer: a background image on .nds-content-section or .nds-footer, or an image in their ::before or ::after, with pointer-events: none. NDS draws nothing in those two pseudo-elements. Never restyle a part inside a component. A banner photo goes in the hero’s own image markup. The event themes place their art this way.
Dark Areas
data-theme="dark" on any element renders it and everything inside it in dark mode. Put it on a dark surface, such as a deep primary section, the footer or a card on a photo. In the preview, the second card carries it.
Color Areas
Words in data-theme make any element a colored area: a section, a hero, a side menu, a card or a plain div. Three kinds of word combine:
- Mode:
darkgives the area dark tokens. - Look:
oncolormakes the parts take their on-color look. A primary button turns white, a field shows the surface through it, and a checked box is white with a primary mark. - Fill:
primarypaints a deep primary surface andneutralpaints a dark gray one. Addgradienttoprimaryfor a gradient from deep primary to primary.gradientdoes nothing onneutral. Withoutdark,primarypaints a light primary tint instead, and the parts keep their colors.
| Markup | Result |
|---|---|
data-theme="dark oncolor primary" |
Deep primary fill, on-color look |
data-theme="dark oncolor primary gradient" |
Primary gradient, on-color look |
data-theme="dark neutral" |
Dark gray fill, dark look |
data-theme="primary" |
Light primary tint, the parts keep their colors |
data-theme="dark oncolor" |
Your own fill, on-color look |
A fill word only paints. It does not set the look, so write oncolor yourself on a primary fill. Use dark oncolor alone on a fill NDS does not paint, such as your own color or a photo. In the preview, the section carries it.
Every area word needs dark, except primary, which paints its light tint without it. Without dark, the other words do nothing. Print drops dark, so an area prints light: a primary area prints as the tint. A menu, a tooltip or a dialog that opens in the area keeps the dark area’s colors, on its own panel. In a neutral area, a darker field takes the neutral-700 fill. --gradient-angle sets the gradient direction. The audit flags a missing dark, a gradient without primary, two fill words on one element, and a dark primary area without oncolor. High contrast flattens every fill.
On a section, .nds-primary, .nds-gradient-primary and .nds-neutral still work but are deprecated and go in v3. Use the words.
Built-in Features
-
Auto-initialization
A toggle or a theme menu works with no setup code. On a page that has none at load, call
NDS.init.refresh()after you add the first one. -
Preference Persistence
The script saves the mode and the theme in
localStorage(nds-theme, andnds-palettefor a custom palette). Every page restores them. -
Zero-Flash
A script in the head writes the saved mode and theme before first paint. A saved custom palette comes back after first paint, so it can flash on load.
-
Circular Reveal Animation
The new colors grow in a circle from the clicked control. Without View Transitions support, or with reduced motion, they change at once.
-
Light and Dark
Every theme and custom palette has a dark mode. Dark pages and dark areas print in light colors.
-
Programmatic Control
Read and change the mode with
NDS.Theme.get(),set()andtoggle().
Best Practices
- Use a custom palette for a brand color with dark mode built for you. Use a stylesheet theme for exact colors, older browsers, or styles beyond color.
- Check the contrast of a custom palette: 4.5:1 for text and 3:1 for controls, on white. NDS does not check the steps it builds.
- Change the mode with
NDS.Theme.set()ortoggle(), not by writingdata-theme. They keep the theme name, save the choice and update the toggles. - Use semantic tokens in your own CSS, such as
--background-card. A hard-coded color follows neither dark mode nor the theme. - Write your own dark rules with
[data-theme~="dark"], never[data-theme="dark"]. The attribute can hold a theme name too. - Put
data-theme="dark"on the dark surface, not on each component inside it. For a deep primary surface, writedata-theme="dark oncolor primary": the fill words paint, andoncolorsets the look.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-theme |
<html> |
The mode and the theme, as words: dark, a theme name, or both. Light mode has no word. The script writes it on each pick, and the head script writes the saved value before first paint |
data-palette |
<html> |
Builds the palette from the --brand-* seeds. The script sets it when the user picks an option with data-seed-primary, and removes it when the user picks another option |
data-theme="dark" |
any element except <html> |
Renders the element and everything inside it in dark mode. A theme name does nothing here |
data-theme="dark oncolor" |
any element except <html> |
Makes a dark area whose parts take their on-color look, on a fill you paint yourself. oncolor without dark does nothing |
data-theme="dark oncolor primary", "dark oncolor primary gradient", "dark neutral" |
any element except <html> |
The fill words paint the surface: deep primary, a primary gradient, or dark gray. They need dark, and they do not set the look. gradient needs primary. data-theme="primary" without dark paints a light primary tint |
data-theme-toggle |
a button, or a .nds-switch |
Makes it a dark mode toggle |
aria-pressed |
a dark mode toggle | Write false in the markup. The script sets true in dark mode and false in light mode. It also swaps the moon and sun icons, and checks the switch in dark mode |
data-theme-value |
a theme option: a button, or a .nds-switch |
The theme name the option applies. Leave it empty for the DGA default |
aria-current |
a theme option | The script sets true on the chosen option and false on the others. It also checks the switch of the chosen option |
data-seed-primary |
a theme option | Makes it a custom palette option, with this primary seed |
data-seed-secondary, -tertiary, -tint, -font, -weight-regular, -weight-medium, -weight-semibold, -weight-bold |
a theme option with data-seed-primary |
Set --brand-secondary, --brand-tertiary, --neutral-tint, --nds-font-brand and the four --font-weight-* values, in that order |
data-theme-css |
a theme option | A stylesheet URL. The script adds it as <link id="nds-theme-stylesheet"> when the user picks the option, and removes it when the user picks another one |
data-theme-js |
a theme option | A script URL. The script loads it once: on the first pick, or at load when it is the saved theme |
A <meta name="theme-color"> sets the color of the browser bar on a phone. The script writes the top bar’s background color into it, unless the meta has a media attribute.
CSS Custom Properties
Set the --brand-* seeds and --neutral-tint on <html> with data-palette. Set the others on <html>, or at :root in a stylesheet theme.
| Property | Default | Controls |
|---|---|---|
--brand-primary |
— | The primary seed: the 600 step of the primary colors |
--brand-secondary |
the primary hue + 150° | The secondary seed: the 600 step of the secondary colors |
--brand-tertiary |
the primary hue − 30° | The tertiary seed: the 500 step of the tertiary colors |
--neutral-tint |
1 |
How much of the primary hue the gray scale takes. 0 is a true gray |
--nds-font-brand |
'IBM Plex Sans Arabic', sans-serif |
The typeface, such as 'Cairo', sans-serif. Your page loads the font |
--font-weight-regular, -medium, -semibold, -bold |
400, 500, 600, 700 |
The four font weights. Corporate and Sunset set 300, 400, 500, 600 |
--background-primary-strong |
var(--colors-primary-900) |
The deep primary surface of the hero and the footer |
--background-primary-light |
var(--colors-primary-50) |
The light primary surface, such as the sub hero. A custom palette sets it to --background-primary-faint |
--background-image-overlay |
var(--colors-primary-950) |
The overlay color on hero images. A custom palette sets it to --colors-neutral-950 |
--colors-* |
the DGA palette | The color ramps: primary, secondary, tertiary, neutral and status. Set them in a stylesheet theme for exact colors |
JavaScript
NDS.Theme controls the mode on every page, with or without a toggle. It fires no events.
| Method | Effect |
|---|---|
NDS.Theme.get() |
Returns 'dark' or 'light' |
NDS.Theme.set(mode, el) |
Sets 'dark' or 'light', keeps the theme name, and saves the choice. The reveal grows from the center of el, or from the center of the screen without it |
NDS.Theme.toggle(el) |
Switches between dark and light, like set() |
NDS.Theme.init() |
Syncs the toggles and the theme menu, and loads the saved theme. The loader calls it when the page has a toggle or a theme option. It runs once |
There is no method to pick a theme. To set a palette from code, set the seeds and data-palette yourself. NDS does not save it.
The full API is in the banner of _js/nds-theme.js.