Skip to main content
Login

Sign in to access your account

Themes - National Design System

Dark mode, a theme menu, and your own brand colors, from a few seed colors or a stylesheet that overrides the color tokens.

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:

  • you need the token names to use in your own CSS: Tokens
  • one section needs a deep primary or dark background: Section

Markup

Preview
<button class="nds-btn nds-subtle nds-icon-only" data-theme-toggle aria-pressed="false" aria-label="Toggle dark mode" data-i18n-aria-label="topbar.toggle_dark_mode"> <i class="nds-icon nds-hgi-moon-02" aria-hidden="true"></i> </button>

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;">

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.

<button class="nds-btn nds-primary" data-theme-value="violet" data-seed-primary="#7c3aed" data-seed-secondary="#ec4899" data-seed-tint="0.4"> <span class="nds-label">Violet palette</span> </button>

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">

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.

<div class="nds-grid" style="--max-col: 2; --mid-col: 2; --min-col: 1;"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <div class="nds-card-text"> <div class="nds-card-corner"> <span class="nds-tag" data-status="success"><span class="nds-label">Approved</span></span> </div> <span class="nds-card-title">Building permit</span> <p class="nds-card-description">Request 1184, Riyadh. Valid until 30 June 2027.</p> </div> </div> <div class="nds-card-actions"> <a href="#" class="nds-btn nds-primary"><span class="nds-label">View permit</span></a> </div> </div> <div class="nds-card nds-stroke" data-theme="dark"> <div class="nds-card-content"> <div class="nds-card-text"> <div class="nds-card-corner"> <span class="nds-tag" data-status="success"><span class="nds-label">Approved</span></span> </div> <span class="nds-card-title">Building permit</span> <p class="nds-card-description">Request 1184, Riyadh. Valid until 30 June 2027.</p> </div> </div> <div class="nds-card-actions"> <a href="#" class="nds-btn nds-primary"><span class="nds-label">View permit</span></a> </div> </div> </div>

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: dark gives the area dark tokens.
  • Look: oncolor makes 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: primary paints a deep primary surface and neutral paints a dark gray one. Add gradient to primary for a gradient from deep primary to primary. gradient does nothing on neutral. Without dark, primary paints 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.

<section class="nds-content-section" data-theme="dark oncolor primary"> <div class="nds-section-wrapper"> <div class="nds-section-head"> <h2 class="nds-section-title">Track a request</h2> <p class="nds-section-description">Enter the number from your confirmation message.</p> </div> <div class="nds-section-body"> <div class="nds-block"> <div class="nds-form-container"> <div class="nds-form-header"> <label for="request-number"> <span class="nds-label">Request number</span> </label> </div> <div class="nds-form-control"> <input type="text" id="request-number" name="request-number" class="nds-input" placeholder="Enter the request number"> </div> </div> <div class="nds-form-container nds-checkbox"> <div class="nds-form-header"> <label for="request-notify"> <span class="nds-label">Send me updates</span> </label> </div> <div class="nds-form-control"> <input type="checkbox" id="request-notify" name="request-notify" value="on" class="nds-checkbox-input" checked> </div> </div> <button type="button" class="nds-btn nds-primary"> <span class="nds-label">Track</span> </button> </div> </div> </div> </section>

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, and nds-palette for 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() and toggle().

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() or toggle(), not by writing data-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, write data-theme="dark oncolor primary": the fill words paint, and oncolor sets 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.

NDS.Theme.set('dark'); // A custom palette var root = document.documentElement; root.style.setProperty('--brand-primary', '#7c3aed'); root.style.setProperty('--neutral-tint', '0.4'); root.setAttribute('data-palette', ''); // Back to the DGA palette ['--brand-primary', '--brand-secondary', '--brand-tertiary', '--neutral-tint'].forEach(function (p) { root.style.removeProperty(p); }); root.removeAttribute('data-palette');

The full API is in the banner of _js/nds-theme.js.

Last Modified Date: 20/11/2026 - 08:48 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks