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.
Syntax
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).
Values
Attribute Values
One value: a cryptographically strong random string (MDN recommends at least 128 bits, often base64-encoded).
.nonce returns the real token for scripts that need it.
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.
Compatibility
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 Chrome61+
Full support
Mozilla Firefox31+
Full support
Apple Safari10+
Full support
Microsoft Edge79+
Full support
Opera48+
Full support
Samsung Internet8+
Full support
Internet ExplorerNo
Not supported
nonceWidely supported
Bottom line: Safe to use with CSP in current browsers. Always generate a fresh server-side nonce per response.
Remember
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.
Readelement.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.