HTML crossorigin Attribute

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

Definition and Usage

The crossorigin attribute is a CORS settings attribute on <script>, <link>, <img>, <audio>, and <video>. It tells the browser how to fetch a cross-origin resource so the page can use it more fully when the server allows CORS.

Common needs: readable canvas/WebGL pixels, better cross-origin script error details, font preload matching @font-face, and credentialed manifests. Pair with integrity on CDNs when you want Subresource Integrity.

Baseline · Widely available.

Not deprecated and not experimental. Standard CORS settings attribute across modern browsers.

Server must allow CORS.

The attribute alone is not enough. The remote response needs the right Access-Control-Allow-* headers, or the browser still blocks full use of the resource.

HTML Syntax

syntax.html
<script src="…" crossorigin=" anonymous | use-credentials">

Same attribute names on <link>, <img>, <audio>, and <video>. Empty crossorigin or crossorigin="" means anonymous.

Attribute Values

Enumerated CORS modes:

ValueDescriptionTry it
(omitted)No CORS mode — default request; canvas may taint; limited script errorsTry it
anonymousCORS request without cookies/credentials to another originTry it
"" or bare crossoriginSame as anonymousTry it
use-credentialsCORS with credentials included (cookies, HTTP auth)Try it

Live Preview

Change the mode on a real <img>. The note shows the live attribute and what the browser will do.

Demo uses a same-origin image so the preview always loads. Cross-origin behavior still depends on the remote server’s CORS headers.

img
Demo image for crossorigin attribute

<img … crossorigin="anonymous">

CORS mode on — no credentials to another origin.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Script with anonymous

Fetch a CDN script in CORS mode without sending credentials.

HTML
<script
  src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"
  crossorigin="anonymous"></script>
Try It Yourself

How It Works

  1. crossorigin="anonymous" asks for a CORS fetch without cookies to another origin.
  2. CDN must allow your origin (or *) with Access-Control-Allow-Origin.
  3. Errors from the script can be more detailed in window.onerror when CORS succeeds.
  4. Optional: add integrity for Subresource Integrity on the same tag.

Example 2 — Image for canvas

Use crossorigin when you will draw the image and read pixels.

HTML
<img id="photo" src="/images/javascript.png" alt="Logo" width="128" height="128" crossorigin="anonymous">
<canvas id="c" width="128" height="128"></canvas>
<script>
  var img = document.getElementById("photo");
  var ctx = document.getElementById("c").getContext("2d");
  img.onload = function () {
    ctx.drawImage(img, 0, 0);
    // Readable when CORS allows (same-origin always OK)
    console.log(ctx.getImageData(0, 0, 1, 1).data);
  };
</script>
Try It Yourself

How It Works

  1. crossorigin="anonymous" marks the image fetch as CORS.
  2. drawImage copies pixels onto the canvas.
  3. getImageData works if the image is same-origin or the remote server allows CORS.
  4. Without CORS on a cross-origin image, the canvas is tainted and reading pixels throws.

Example 3 — Font preload

Web fonts use CORS. Match preload mode with a bare crossorigin.

HTML
<link
  rel="preload"
  href="/files/Roboto/static/Roboto-Regular.ttf"
  as="font"
  type="font/ttf"
  crossorigin>
Try It Yourself

How It Works

  1. rel="preload" starts the font download early.
  2. as="font" tells the browser the resource type (see as).
  3. crossorigin (empty) matches anonymous CORS used by @font-face.
  4. Without it, preload and CSS may not share the cache — a double download.

Edge Cases & Pitfalls

Most bugs are “omitted vs anonymous” mix-ups or missing server headers.

default

Omitted is not anonymous

No attribute means no CORS mode. You must add crossorigin when you need CORS behavior.

creds

Credentials need a specific origin

With use-credentials, the server cannot reply with Access-Control-Allow-Origin: *. It must echo your origin and allow credentials.

icon

Favicon caveat

crossorigin on rel="icon" is not supported in Chromium-based browsers. Prefer it for scripts, fonts, images, and media.

manifest

Credentialed web manifests

If a web app manifest needs credentials, use crossorigin="use-credentials" even for same-origin files.

JS

Property is crossOrigin

In JavaScript the property uses a capital O: el.crossOrigin = "anonymous". Set it before assigning src when creating elements dynamically.

Browser Support

crossorigin is an HTML attribute for CORS on media and resource elements in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

crossorigin

Widely available on script, link, img, audio, and video. Exact CORS success still depends on the server’s response headers.

96% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer IE11 (partial)
Limited
crossorigin Widely supported

Bottom line: Safe to use for CDN scripts, CORS images, and font preloads. Always verify Access-Control headers on the resource host.

Key Takeaways

  • Role: turn on CORS mode for resource fetches.
  • Modes: anonymous (default when present) or use-credentials.
  • Elements: script, link, img, audio, video.
  • Server: CORS headers are required for the mode to succeed.

One line: add crossorigin when you need CORS — omit it only when a plain non-CORS fetch is enough.

Frequently Asked Questions

It is a CORS settings attribute. When present, the browser fetches the resource in CORS mode so the page can use it more fully (for example canvas pixel reads, richer script errors, or matching font preload requests) if the server sends the right Access-Control headers.
script, link, img, audio, and video. On media elements it is also a CORS settings attribute.
anonymous (or an empty value) uses CORS without sending cookies or HTTP credentials to another origin. use-credentials includes credentials; the server must allow your exact origin and Access-Control-Allow-Credentials: true (not *).
No. If you omit crossorigin, the request is not in CORS mode. When the attribute is present, empty or invalid values are treated as anonymous.
Font requests use CORS. A preload without crossorigin may not match the later @font-face fetch, so the browser can download the font twice.
Use the crossOrigin property (capital O), for example img.crossOrigin = "anonymous" or script.crossOrigin = "use-credentials".

Next: data-*

Store custom data on elements with data-* attributes.

data 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