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.
Generate a digest from the file, then prefix it: openssl dgst -sha384 -binary file.js | openssl base64 -A → prepend sha384-.
Try it
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.
Code
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.
Same algorithm, several hashes. Any matching digest of that algorithm is enough (useful for alternate file versions).
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).
Compatibility
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 Chrome45+
Full support
Mozilla Firefox43+
Full support
Apple Safari11.1+
Full support
Microsoft Edge17+
Full support
Opera32+
Full support
Samsung Internet5+
Full support
Internet ExplorerNo 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.
Remember
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.