HTML nonce Attribute

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The nonce attribute is a global HTML attribute. It holds a cryptographic “number used once” so Content Security Policy (CSP) can allow a specific inline <script> or <style> without opening everything via unsafe-inline.

Your server generates a fresh token on every response, puts the same value on the element and in the CSP header (for example script-src 'nonce-…'), then the browser runs only matching markup.

Baseline · Widely available.

Not deprecated. Prefer external files or CSP hashes for static inline code when you can; use nonce when you must allow dynamic inline script or style safely.

Server-only secret — never hardcode a fixed nonce in production.

Generate a new unpredictable value per page load. Reusing one token across requests defeats the point of CSP nonces.

HTML Syntax

syntax.html
<script nonce=" RANDOM_BASE64">
<style nonce=" RANDOM_BASE64">

Match the HTML value in the CSP header: Content-Security-Policy: script-src 'nonce-RANDOM_BASE64' (and style-src the same way for styles).

Attribute Values

One value: a cryptographically strong random string (MDN recommends at least 128 bits, often base64-encoded).

ValueMeaningTry it
Random tokenMust match 'nonce-…' in the CSP headerTry it
On <script>Allowlists that inline script under script-srcTry it
On <style>Allowlists that inline style under style-srcTry it
Empty / missingNo CSP nonce match — blocked when policy forbids unsafe inlineTry it

Live Preview

Browsers hide the content attribute. Compare getAttribute("nonce") with the .nonce property on a demo script.

Nonce hiding

Demo token is study-only. Real pages generate a new nonce on the server for each response.

Result

Click Read values.

HTML Examples

Study patterns below. Full CSP needs a matching header (or <meta http-equiv> in demos). Use Try It Yourself for a live editor.

Example 1 — Allow an Inline Script

Same token on the element and in CSP script-src. Production should send CSP as an HTTP header.

HTML
<!-- Demo only: prefer an HTTP Content-Security-Policy header in production -->
<meta http-equiv="Content-Security-Policy"
      content="script-src 'nonce-abc123XYZ'">

<p id="msg">Waiting…</p>
<script nonce="abc123XYZ">
  document.getElementById("msg").textContent =
    "Inline script allowed by matching nonce.";
</script>
Try It Yourself

How It Works

  1. Server creates a random token (demo uses a fixed string for study).
  2. CSP lists 'nonce-abc123XYZ' under script-src.
  3. script carries the same nonce value.
  4. Browser allows that inline script; others without the token stay blocked.

Example 2 — Allow an Inline Style Block

Same idea for style-src and <style nonce="…">.

HTML
<meta http-equiv="Content-Security-Policy"
      content="style-src 'nonce-styleTok9'">

<style nonce="styleTok9">
  #box { color: green; }
</style>
<p id="box">Styled with a matching nonce.</p>
Try It Yourself

How It Works

  1. style-src allowlists 'nonce-styleTok9'.
  2. <style nonce="styleTok9"> matches that policy.
  3. Rules apply to the page (here, green text).
  4. Other inline styles without the token would be blocked by a strict CSP.

Example 3 — Read .nonce, Not getAttribute

MDN documents that the content attribute is hidden; the IDL property still returns the value.

HTML
<script id="s" nonce="demo-token"></script>
<pre id="out"></pre>
<script>
  var el = document.getElementById("s");
  document.getElementById("out").textContent =
    "getAttribute: " + JSON.stringify(el.getAttribute("nonce")) +
    "\n.nonce: " + JSON.stringify(el.nonce);
</script>
Try It Yourself

How It Works

  1. Element is created with a nonce value.
  2. getAttribute("nonce") often returns "" (hiding).
  3. .nonce returns the real token for scripts that need it.
  4. Goal: stop CSS attribute selectors from leaking the nonce to attackers.

Edge Cases & Pitfalls

Most failures are mismatched tokens, reused nonces, or reading the wrong API.

match

HTML and CSP must use the same token

Header uses 'nonce-VALUE'; the attribute is just VALUE — no nonce- prefix on the element.

once

New nonce every response

Never ship a constant demo string in production. Rotate on each page load.

hide

Do not rely on getAttribute("nonce")

Use the .nonce property when JavaScript needs the value.

hash

Static inline? Prefer a CSP hash

Hashes avoid per-request nonce plumbing when the inline code never changes.

meta

HTTP header beats meta for real apps

Demos may use <meta http-equiv="Content-Security-Policy">; production should send CSP as a response header.

Browser Support

nonce is an HTML global attribute. CSP enforcement is a browser security feature, not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

nonce

Supported in modern browsers for CSP allowlisting of inline script and style. Content-attribute hiding is part of the security model.

95% Global support (approx.)
Google Chrome 61+
Full support
Mozilla Firefox 31+
Full support
Apple Safari 10+
Full support
Microsoft Edge 79+
Full support
Opera 48+
Full support
Samsung Internet 8+
Full support
Internet Explorer No
Not supported
nonce Widely supported

Bottom line: Safe to use with CSP in current browsers. Always generate a fresh server-side nonce per response.

Key Takeaways

  • Role: CSP allowlist token for specific inline script/style.
  • Server generates a unique value every page load.
  • Match the attribute value with 'nonce-…' in CSP.
  • Read element.nonce, not getAttribute("nonce").

One line: nonce is a one-time CSP token that allowlists a specific inline script or style.

Frequently Asked Questions

It holds a cryptographic one-time token. With a matching Content-Security-Policy header (for example script-src 'nonce-…'), the browser allows that specific inline script or style without using unsafe-inline for everything.
Your server. Create a fresh, unpredictable value on every page response (for example crypto.randomBytes(16).toString('base64')), put it on the element, and send the same value in the CSP header.
Browsers hide the content attribute so CSS or other readers cannot steal the token. Read script.nonce (or element.nonce) instead.
No. It is a Baseline widely available global attribute used with Content Security Policy.
For static inline scripts or styles, a CSP hash is often better because you do not need a unique token per request. Use nonce when the inline content must stay dynamic.
No. A nonce must be unique per document load. Reusing it weakens CSP and can let attackers replay a known token.

Next: novalidate

Skip built-in HTML form validation when you need custom checks.

novalidate tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful