Skip to main content
Login

Sign in to access your account

Document Head - National Design System

The stylesheets and scripts every NDS page loads, so the page paints fast with no flash of unstyled content and no layout shift

Overview

Every page carries the same <head>: an inline style block (the gate), two stylesheet preloads and one inline script, and the main script goes before </body>. The gate draws the shell of the page and hides its content until the main CSS applies. The inline script turns the preloads into stylesheets. The main script starts the components and loads every other file.

The code below is the head this site serves.

The body of the page is on Page Layout.

Usage

Copy the head as one block. The JS Library tab holds the main script: put it just before </body>. Change only the lines the Parts table marks as per page.

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Page Title</title> <style> /* Colors: the first-paint fills. Edit the fallbacks to match a brand. */ html[data-theme~=dark] :where(.nds-main-nav .nds-brand.nds-oncolor :is(img,svg)){filter:brightness(0) invert(1)} html{background-color:var(--background-body, #f9fafb)} html[data-theme~=dark]{background-color:var(--background-body, #111927)} :where(.nds-topbar){background-color:var(--background-topbar, #f3f4f6)} html[data-theme~=dark] :where(.nds-topbar){background-color:var(--background-topbar, #111927)} :where(.nds-main-nav){background-color:var(--background-nav, #fff)} html[data-theme~=dark] :where(.nds-main-nav){background-color:var(--background-nav, #1f2a37)} :where(.nds-hero-image-wrapper)::before{content:"";position:absolute;inset:0;background:color-mix(in srgb, var(--hero-overlay-color, var(--background-image-overlay, #092a1e)) calc(var(--hero-overlay-opacity, 0.7) * 100%), transparent);pointer-events:none} /* Structure: reserved heights, and the rules that hide the content until the main CSS applies. */ *,*::before,*::after{box-sizing:border-box;margin:0;padding:0} html{overflow-anchor:none} html :where(header){display:contents} html :where(.nds-topbar){height:40px} html :where(.nds-main-nav){height:var(--nds-nav-height, 72px)} html .nds-swiper.nds-hero:not([data-nds-swiper-initialized],[data-swiper-preset]) .nds-swiper-slide:not(:first-child){display:none} :where(.nds-topbar>*,.nds-main-nav>*,.nds-hero-section .nds-section-action,.nds-content-layout,.nds-user-feedback-section,.nds-footer){visibility:hidden} html:not([data-nds-loaded]) main{overflow-x:clip} .nds-skip-link:not(:focus){position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border:0} :root{--nds-icons-opacity: 0} :is(.nds-hidden,[hidden],[data-state~=hidden],[data-filtered]){display:none !important} :where(.nds-hero-section){position:relative;height:550px} :where(.nds-hero-section.nds-sub){height:auto;min-height:220px} :where(.nds-hero-image-wrapper){position:absolute;inset:0} :where(.nds-hero-image){width:100%;height:100%;object-fit:cover;display:block} :where(.nds-hero-section :is(.nds-section-body,.nds-section-wrapper,.nds-breadcrumb-nav)){visibility:hidden} </style> <!-- Hero photo only: preload the first slide, with its <source> breakpoints --> <link rel="preload" as="image" href="assets/img/riyadhcenter_IQ_sm.webp" media="(max-width: 768px)" fetchpriority="high"> <link rel="preload" as="image" href="assets/img/riyadhcenter_IQ_md.webp" media="(min-width: 769px) and (max-width: 1646px)" fetchpriority="high"> <link rel="preload" as="image" href="assets/img/riyadhcenter_IQ.webp" media="(min-width: 1647px)" fetchpriority="high"> <link rel="preload" href="assets/css/nds.critical.min.css?ver=2.1.1.b783f58" as="style" fetchpriority="high" data-nds-defer> <link rel="preload" href="assets/css/nds-main.min.css?ver=2.1.1.b783f58" as="style" fetchpriority="low" data-nds-defer="main"> <link rel="icon" type="image/svg+xml" href="assets/img/favicon.svg" fetchpriority="low"> <script> (function() { var saved; try { saved = localStorage.getItem('nds-theme'); } catch (e) {} if (saved) { var root = document.documentElement; var themes = ((root.getAttribute('data-theme') || '') + ' ' + saved).split(/\s+/) .filter(function (theme, i, all) { return theme && all.indexOf(theme) === i; }); root.setAttribute('data-theme', themes.join(' ')); } })(); (function () { // The side menu rail's stored fold, so the first paint is already folded. var fold; try { fold = localStorage.getItem('nds-sidemenu-collapsed'); } catch (e) {} if (fold) document.documentElement.setAttribute('data-nds-sidemenu-fold', fold); })(); (function () { document.querySelectorAll('link[rel="preload"][data-nds-defer]').forEach(function (preload) { preload.rel = 'stylesheet'; }); })(); </script> </head>

Parts

Part Holds Required
<title> The page title. Per page Yes
<style> (the gate) The shell’s first-paint colors, its reserved heights, and the rules that hide the content. Edit the fallback colors in its Colors group to match a brand Yes, unless you load the critical CSS as a blocking stylesheet
Hero image preloads The first hero slide, one link per <source> in its <picture>, with the same file and media query. Per page Only on a page whose hero has a photo
Critical CSS preload nds.critical.min.css. data-nds-defer marks it for the inline script Yes
Main CSS preload nds-main.min.css. data-nds-defer="main" marks it as the sheet the page waits for Yes
Favicon A placeholder icon. Replace the file with your own Yes
Inline script The saved theme, applied before the first paint, and the loop that turns each data-nds-defer preload into a stylesheet Yes
Main script nds-main.min.js, in the JS Library tab Yes

Behavior

Gated First Paint

The head above is the gated setup. The browser paints the gate’s shell at once, while the critical CSS downloads without blocking. Once the main CSS applies, the loader sets data-nds-loaded on <html>, and the page shows in its final layout. NDS needs JavaScript: without it, no NDS style applies, and the gate keeps the content hidden.

Blocking Critical CSS

The last choice under a strict Content Security Policy, after a nonce and a hash (see Content Security Policy). Use it only when the policy can grant neither to the inline style block. Delete the <style> block. Replace the critical CSS preload with <link rel="stylesheet" href="assets/css/nds.critical.min.css?ver=…">. The first paint waits for that file, and it holds the same gate. Never delete the style block alone: the page then paints raw HTML first, and jumps when the critical CSS lands.

Framework Navigation

The inline script runs once, at page load. The stylesheets it turns on and the data-nds-loaded stamp are not in the server HTML. A framework that compares <head> with the server HTML on each navigation removes them, while the gate stays, so the page hides itself until a full reload. Turbo, htmx boost and Blazor enhanced navigation work this way. Mark those links and the stamp as permanent, in the way your framework offers. If it removes them anyway, run the stylesheet loop again after each navigation and put the stamp back. A framework that renders the head in the browser creates the inline script without running it. The Next.js App Router does this for a not-found page under several root layouts. There, your startup code runs the stylesheet loop and adds the main script itself. Components on a new view are covered on Init.

Built-in Features

  • No Unstyled Content

    The gate hides the content until the main CSS applies. A visitor never sees raw HTML or half-styled components.

  • Reserved Layout

    The gate gives the top bar, the main navigation and the hero their final heights at first paint. Nothing moves when the styles land.

  • Saved Theme Before Paint

    The inline script reads the theme the visitor saved and writes it on <html> before the first paint. A dark page never flashes light.

  • Non-Blocking Styles

    Each stylesheet downloads as a preload and applies when it arrives, so no CSS file blocks the first paint. The hero preload starts the largest image before the body is parsed.

  • One Script Tag

    The main script loads every other bundle and the icon sheets itself, after the page shows. A page needs no tag for them.

  • Strict CSP

    A nonce or a hash can cover a script element, but never an inline event handler. So the head applies its stylesheets with a script, not an onload attribute.

Best Practices

  • Change the ?ver= value on every NDS upgrade. A stale value serves the old files from the browser cache. The loader adds the main script’s ?ver= to every bundle it loads.
  • Keep the head entries in their order. The inline script must come after every data-nds-defer preload, because it converts only the preloads above it.
  • Keep every NDS stylesheet in one folder. The loader builds the icon and accessibility sheet URLs from the main CSS link, by swapping its file name, so those sheets keep their shipped names. The main sheet can take another name, such as a fingerprinted nds-main.3f2a.min.css, while its preload keeps data-nds-defer="main".
  • Never add a tag for nds-delegated.min.js, nds-extras.min.js or any other bundle. The loader adds each one when the page needs it.
  • Before a component’s bundle arrives, NDS.X.method() loads it and returns a Promise of the result. await a call whose result you use: on a component that is already loaded, await returns the result as it is.
  • To defer a stylesheet of your own, give its preload data-nds-defer: <link rel="preload" href="css/site.css" as="style" data-nds-defer>. The inline script applies it with the others.
  • For a default stylesheet theme, add <link id="nds-theme-stylesheet" rel="stylesheet" href="…"> right after the critical CSS preload, as a blocking stylesheet, so the brand applies before the first paint. See Themes.

API

Files

File Holds Loading
nds.critical.min.css Tokens, palette, fonts, reset, section layout, hero and the gate A preload behind the gate, or a blocking stylesheet
nds-main.min.css Every component and layout style A preload. The page shows once it applies
nds-icons.min.css UI icons (nds-icon) Added by the loader once the main CSS applies
hgi-rounded-stroke-min.css The content icon map (hgi hgi-stroke). The icon font faces are in the main CSS, one for each file of 25 icons Added by the loader when the page shows
nds-main.min.js The loader and the components that paint the first screen A defer script at the end of <body>
nds-delegated.min.js, nds-extras.min.js, nds-editor.min.js, nds-chart.min.js, nds-code.min.js, nds-cookies.min.js The other components Added by the loader after the page shows, when the page holds one of their components
nds-accessibility.min.js and nds-accessibility.min.css The accessibility panel. See Accessibility Added by the loader on the first press of the accessibility button, or at load for a visitor with saved settings
nds-audit.min.js The page audit Added by the first NDS.init.audit() call, or by enableLogging. Never on a production page

Data Attributes

Attribute Element Effect
data-nds-defer a <link rel="preload" as="style"> The inline script turns the preload into a stylesheet in place, so it keeps its order in <head>
data-nds-defer="main" the main CSS preload The same, and it marks the main CSS. The loader waits for that sheet to apply before it shows the page, and finds the folder of the other NDS sheets from its URL
data-theme <html> The inline script adds the theme saved under nds-theme in localStorage to it, before the first paint. The theme switcher writes that key
data-timezone <html> Write it yourself: the site’s timezone, as an IANA name such as Asia/Riyadh. Without it, NDS uses the visitor’s timezone. See Date
data-date-format <html> Write it yourself: the site’s date format, such as DD/MM/YYYY. Any element can carry its own for the content inside it. See Date
data-nds-sidemenu-fold <html> The inline script copies the side menu rail’s fold, saved under nds-sidemenu-collapsed in localStorage, to it, so the first paint is already folded or open. The side menu removes it when it starts. Do not set it yourself. See Side Menu
data-nds-loaded <html> The loader sets it once the main CSS has applied, and the page shows. Do not set it yourself

The loader’s window settings (NDS_INIT_CONFIG, NDS_ASSETS_PATH) are on Init.

Content Security Policy

A strict policy blocks inline code, so the gate and the inline script each need a grant. Pick the first that fits:

  1. A nonce, when your server builds each page and can write a new random value into it.
  2. A hash, when the page is a plain file, even one your own server sends. Add the SHA-256 of the gate and of the inline script: one edit to the policy.
  3. A blocking stylesheet, only when the policy can take neither. The first paint waits for the critical CSS. See Blocking Critical CSS.
Part Needs
The inline script A nonce or a hash in script-src
The gate (<style>), unless you load the critical CSS as a blocking stylesheet A nonce or a hash in style-src
Every stylesheet and script file 'self'
The bundles the loader adds 'self', or the nonce: the loader copies the main script’s nonce onto each one
UI icons (nds-icon) img-src data:: each icon is an inline SVG mask
Content icons (hgi) font-src data:: an inline font holds each icon’s space while the icon font loads
A style attribute on your markup (style="--gap: 16px") 'unsafe-inline'. No nonce or hash covers an attribute, so move the value to a class
Trusted Types (require-trusted-types-for 'script') The policy name nds in trusted-types

A policy that covers it all:

Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-YOUR_RANDOM_VALUE'; style-src 'self' 'nonce-YOUR_RANDOM_VALUE'; img-src 'self' data:; font-src 'self' data:;

Put the same value on the gate, the inline script and the main script:

<style nonce="YOUR_RANDOM_VALUE">/* the gate */</style> <script nonce="YOUR_RANDOM_VALUE">/* the inline script */</script> <script nonce="YOUR_RANDOM_VALUE" defer src="assets/js/nds-main.min.js?ver=…"></script>

Your server makes a new random value for each response: a fixed value gives an attacker the same permission as your own code. Under a policy with no 'self' in script-src, the main script needs the value too. Without it, the bundles the loader adds are blocked, and their components never start.

The hash is the SHA-256 of the bytes between the tags, never the tags, in base64. It covers those bytes exactly, indentation and line endings included. A file saved with CRLF line endings hashes differently from the same file with LF, and a formatter or minifier breaks the match too. When the policy blocks a block, the browser prints the hash it expected in the console error. Add that hash to the policy as 'sha256-…'. Do it again whenever anything edits the block.

A policy that allows no injected scripts can still run NDS: add each bundle’s own <script> tag with its nonce or integrity. The loader skips a bundle that already has a tag.

Inline Knobs

Under a strict policy, a knob in a style attribute never applies. The browser prints a CSP error, and NDS.init.audit() counts the blocked attributes (style-blocked). Search your pages for style="--, and move each knob to a class in your own stylesheet:

<!-- Before: dead under a strict CSP --> <div class="nds-block nds-flex nds-col" style="--align: center;"> <!-- After: the same NDS classes, plus one of yours --> <div class="nds-block nds-flex nds-col signin-stack">
.signin-stack { --align: center; }

NDS components are not affected: their scripts set styles through the CSSOM, which no policy blocks.

Last Modified Date: 19/11/2026 - 10:42 AM
Was this page useful?
60% of users said Yes from 2843 Feedbacks