References

Beginner-friendly references for web development, with live, editable examples.

The HTML nonce attribute

Global attribute Works on every element All modern browsers
In short

The HTML nonce attribute holds a one-time cryptographic token that a Content-Security-Policy can use to allow a specific inline <script> or <style> while blocking all others. It is a global attribute; the server must generate a fresh, random, unguessable nonce on every response.

Overview

A "nonce" is a number used once. With a strict Content-Security-Policy you can forbid all inline scripts and styles, then selectively allow trusted ones by giving them a nonce that matches the value in your Content-Security-Policy response header.

For this to be safe the nonce must be cryptographically random and generated anew on every page load. A fixed nonce is no better than no CSP. Browsers also hide the attribute value from the DOM (it is only readable through the element.nonce property) to block CSS attribute-selector and dangling-markup leaks.

Syntax

<!-- HTTP response header (fresh 128-bit base64 token per request) -->
Content-Security-Policy: script-src 'nonce-8IBTHwOdqNKAWeKl7plt8g=='; style-src 'nonce-8IBTHwOdqNKAWeKl7plt8g==';

<!-- Matching HTML elements -->
<script nonce="8IBTHwOdqNKAWeKl7plt8g==">console.log('Allowed');</script>
<style nonce="8IBTHwOdqNKAWeKl7plt8g==">.banner { color: #1565c0; }</style>

Values

Value
A random, single-use, base64 token generated per request.

Try it: CSP nonce simulator

CSP Builder Ready

Generate a fresh 128-bit base64 nonce in your browser, toggle Content Security Policy Level 3 directives, and see which script tags execute or get blocked.

Example

Code example
<!-- Server sends: Content-Security-Policy: script-src 'nonce-r4nd0m' -->
<script nonce="r4nd0m">/* this inline script is allowed */</script>

Server-side recipes

Drop-in examples for generating a per-request token in PHP, Node, Nginx, and Cloudflare Workers.

PHP

Generate the nonce before any response body is sent, attach the CSP header, and escape the token when rendering HTML.

<?php
$nonce = base64_encode(random_bytes(16));
header("Content-Security-Policy: script-src 'nonce-{$nonce}' 'strict-dynamic' https:; object-src 'none'; base-uri 'none';");
?>
<script nonce="<?= htmlspecialchars($nonce, ENT_QUOTES, 'UTF-8') ?>">
  console.log('Trusted inline script');
</script>

CSP nonce vs hash vs unsafe-inline

Choose a CSP strategy based on whether HTML is rendered per request or cached on a CDN.

Method Static / CDN Inline scripts Security Best fit
'nonce-...' No (per request) Yes (matching nonce) High (single-use) PHP, Node, Python, and SSR apps
'sha256-...' Yes (fixed bytes) Yes (exact hash) High (exact bytes) Static sites and CDN-cached HTML
'unsafe-inline' Yes All inline (unsafe) Low (no XSS defense) Legacy fallback (ignored with nonce/hash)
Host allowlist Yes External origins only Medium (JSONP risk) Apps with zero inline script or style tags

Best practices

  • Generate a fresh, random nonce on the server for every page response.
  • Match the nonce in both the CSP header and the element.
  • Prefer external files over inline code where you can, reducing the need for nonces.
  • Never reuse a nonce across requests; it must be unguessable and single-use.

Browser compatibility

Baseline: Widely available
Across browsers since May 2022 WHATWG HTML Spec (opens in new tab)

Core CSP Level 2 nonces shipped across browsers by Edge 79 (January 2020), and CSP Level 3 ('strict-dynamic'), el.nonce, and attribute hiding all landed by Safari 15.5 (May 2022).

Feature Chrome Edge Firefox Safari
nonce on <script> & <style>EdgeHTML 15-18 had partial support ChromeSupported since 40+ EdgeSupported since 79+ FirefoxSupported since 31+ SafariSupported since 10+
el.nonce DOM property ChromeSupported since 61+ EdgeSupported since 79+ FirefoxSupported since 75+ SafariSupported since 15.4+
Hide nonce from CSS & getAttribute()Prevents CSS attribute-selector exfiltration ChromeSupported since 61+ EdgeSupported since 79+ FirefoxSupported since 75+ SafariSupported since 15.5+

Frequently asked questions

What does the nonce attribute do?
A single-use cryptographic token a Content-Security-Policy uses to allow a specific inline script or style.
How do I generate a nonce?
On the server, generate a fresh random value (128 bits or more, base64-encoded) for every response, send it in the Content-Security-Policy header as 'nonce-VALUE' and echo the same value in the nonce attribute of each trusted inline script or style.
Can I hard-code a nonce?
No. It must be randomly generated per response and matched in the CSP header, or it provides no security.
Is nonce a global attribute?
Yes, it is a global attribute, so it can be set on any HTML element, in practice <script> and <style>.
Why does element.getAttribute("nonce") return an empty string?
Browsers clear the parsed nonce content attribute from getAttribute("nonce"), CSS attribute selectors, and DOM serialization (innerHTML) to block non-script side-channel leaks such as injected CSS selectors (script[nonce^="a"]) or dangling markup. Scripts running in the page can still read the token via the element.nonce DOM property.
Can I use a CSP nonce on static or CDN-cached HTML?
Not directly, because caching a page serves the same nonce to multiple visitors and lets attackers reuse it. Either switch to sha256 script hashes for static HTML, or inject a fresh nonce per request at the edge (for example, with Cloudflare Workers and HTMLRewriter on trusted static builds).

Related tools