HTML integrity Attribute

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

Definition and Usage

The integrity attribute turns on Subresource Integrity (SRI). You assert the exact content a linked script or stylesheet must have. The browser hashes the downloaded bytes and loads the resource only if the hash matches.

Use it on <script src> and on <link> when rel is stylesheet, preload, or modulepreload. For CDN (cross-origin) files, also set crossorigin="anonymous" so CORS can expose the body for hashing.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about April 2018. Defends against supply-chain and CDN tampering.

Hash must match exact bytes.

Any file change breaks SRI until you regenerate the hash. Placeholder hashes do not protect anything.

HTML Syntax

syntax.html
<script src="url" integrity=" algo-base64hash …" crossorigin="anonymous">

Same pattern on <link rel="stylesheet" href="...">. Multiple hashes are space-separated.

Attribute Values

Each token is algorithm- plus a base64 digest of the file contents:

ValueDescriptionTry it
sha256-...SHA-256 digest (weaker of the three; still allowed)Try it
sha384-...SHA-384 digest — common, strong default for CDN assetsTry it
sha512-...SHA-512 digest — strongest; preferred when listed with othersTry it
Multiple tokensSpace-separated list. Browser uses the strongest algorithm present; same-algo tokens allow alternate file versionsTry it

Generate a digest from the file, then prefix it: openssl dgst -sha384 -binary file.js | openssl base64 -A → prepend sha384-.

Live Preview

Hash sample content in the browser (Web Crypto). The result is a real integrity value for those exact bytes.

Pick an algorithm and sample bytes, then generate. Production hashes must come from your real CDN file.

integrity value

sha384-… (click Generate)

Match these bytes on the CDN, or the browser will block the resource.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor. Example hashes are truncated for study — generate full digests from your real files.

Example 1 — Secure External Script

Verify a CDN JavaScript file before the browser runs it.

HTML
<script
  src="https://cdn.example.com/lib.js"
  integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
  crossorigin="anonymous"></script>
Try It Yourself

How It Works

  1. Point at the file. src loads the external script.
  2. Declare the expected hash. integrity="sha384-..." is the approved digest.
  3. Enable CORS for CDNs. crossorigin="anonymous" lets the browser read the body to hash it.
  4. Browser decides. Same hash → run. Different bytes → refuse to execute.

Example 2 — Secure External Stylesheet

Apply the same check to CSS from a CDN.

HTML
<link
  rel="stylesheet"
  href="https://cdn.example.com/styles.css"
  integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
  crossorigin="anonymous">
Try It Yourself

How It Works

  1. Link the CSS. rel="stylesheet" plus href loads the file.
  2. Attach integrity. Same algorithm-base64 format as scripts.
  3. Keep crossorigin. Required for cross-origin SRI so CORS can expose the bytes.
  4. Protect styles too. Tampered CDN CSS is blocked just like a bad script.

Example 3 — Multiple Hash Algorithms

List several digests. The browser keeps only the strongest algorithm it supports, then accepts if any of those values match.

HTML
<script
  src="https://cdn.example.com/lib.js"
  integrity="sha256-NmUxNTFiMDUzZGIwZjcwZDIyYTc5NTA4ZmQyNT
             sha384-Tk2Yjg3YmYzMWNkZTdhMTFkM2FlNDg4ZjE3MzEzNTk3ZDlh
             sha512-OGUwYThkZDc2YzFlZGI5MDEzZmZhMGFkMGQ0OTQ3MzZkNGYZTEzODk2"
  crossorigin="anonymous"></script>
Try It Yourself

How It Works

  1. Space-separated list. Each token is one algo-hash pair.
  2. Strength order. SHA-256 < SHA-384 < SHA-512. The strongest present wins.
  3. Same algorithm, several hashes. Any matching digest of that algorithm is enough (useful for alternate file versions).
  4. Fallback for older agents. Listing multiple algorithms helps if a browser lacked a stronger one (rare today).

Edge Cases & Pitfalls

Most SRI failures are hash drift, missing CORS, or setting integrity too late.

cors

Cross-origin needs crossorigin

Without CORS mode, the browser cannot hash a cross-origin body. Add crossorigin="anonymous" and ensure Access-Control-Allow-Origin.

bytes

Any byte change breaks the hash

Minify, newline, or version bumps all change the digest. Regenerate after every file update.

timing

Set integrity before the fetch

For first-load SRI, the attribute must be present before the browser requests the file. Prefer HTML or inject the element with integrity already set.

scope

Not for inline scripts or images

No src means nothing to verify. <img> and general elements do not use integrity.

layer

SRI complements HTTPS and CSP

Integrity checks content identity; it does not replace TLS or a Content Security Policy (nonce is a related CSP tool).

Browser Support

integrity is an HTML attribute for Subresource Integrity in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

integrity

Widely available since about April 2018. Chrome 45+, Firefox 43+, Safari 11.1+, Edge 17+, and Opera 32+ enforce SRI on scripts and stylesheets. Internet Explorer has no support.

97% Global support (approx.)
Google Chrome 45+
Full support
Mozilla Firefox 43+
Full support
Apple Safari 11.1+
Full support
Microsoft Edge 17+
Full support
Opera 32+
Full support
Samsung Internet 5+
Full support
Internet Explorer No SRI
Not supported
integrity (SRI) Widely supported

Bottom line: Safe to use on CDN scripts and stylesheets in all modern browsers. Pair with crossorigin for cross-origin assets; IE ignores integrity entirely.

Key Takeaways

  • Role: prove a script or stylesheet still has the bytes you approved (SRI).
  • Elements: <script src> and <link> (stylesheet / preload / modulepreload).
  • Format: sha256-, sha384-, or sha512- plus base64; strongest listed algorithm wins.
  • CDN rule: add crossorigin="anonymous" and keep hashes in sync with every release.

One line: integrity blocks tampered CDN files — only matching hashes are allowed to run or style the page.

Frequently Asked Questions

It enables Subresource Integrity (SRI). The browser hashes the fetched file and compares it to your expected hash. If they do not match, the script or stylesheet is blocked.
script with src, and link when rel is stylesheet, preload, or modulepreload. Inline scripts without src do not use integrity.
Cross-origin SRI needs a CORS fetch so the browser can read the response body and hash it. Add crossorigin="anonymous" on CDN script and link tags, and the server must send Access-Control-Allow-Origin.
sha256, sha384, and sha512. The value is algorithm-base64Hash. Prefer sha384 or sha512. When several algorithms appear, the browser uses only the strongest set.
The browser refuses to run the script or apply the stylesheet and logs an integrity error in DevTools. Fix the hash or the file so they match again.
Whenever the file bytes change — even one character. After every CDN version bump or edit, regenerate the hash or the resource will be blocked.

Next: ismap

Learn the ismap attribute for server-side image maps.

ismap 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