👀 Live Preview
Typical preload hint in the document <head>:
The browser fetches app.js early with script-level priority.

The as attribute is a versatile feature in HTML that allows developers to specify the expected media type or file type of a resource. This attribute is commonly used in conjunction with the <link> element—especially with rel="preload"—to provide additional information to browsers and optimize resource loading.
Preload & prefetch.
script, style, font.
Where as applies.
Browser optimization.
Set as dynamically.
Faster page loads.
asThe primary purpose of the as attribute is to provide a hint to the browser about the type of content it should expect when fetching a resource. This can help browsers optimize the loading process, improve performance, and provide a better user experience.
When using rel="preload", the as attribute is required. It tells the browser how to prioritize and store the resource correctly.
Add as to a <link rel="preload"> element with a valid resource type keyword:
<link rel="preload" href="styles.css" as="style"><link> with rel="preload", rel="prefetch", or similar.as="script" for a CSS file).crossorigin in addition to as="font".rel="stylesheet" or <script> tags.The as attribute accepts various values to indicate different resource types. Some common values include:
<link rel="preload" href="app.js" as="script">
<link rel="preload" href="hero.webp" as="image">
<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>| Resource | as Value | Notes |
|---|---|---|
| CSS file | style | Use with rel="preload" |
| JavaScript file | script | Classic scripts |
| Web font | font | Add crossorigin |
| Image | image | Hero images, icons |
| HTML document | document | Prefetch next page |
| Applicable element | <link> | With preload/prefetch |
| Element | Supported? | Notes |
|---|---|---|
<link rel="preload"> | Yes (required) | Primary use case |
<link rel="prefetch"> | Yes | Low-priority future resources |
<link rel="stylesheet"> | No | Does not use as |
<script> | No | Use type="module" instead |
Preload stylesheets, dynamic JavaScript updates, and font preloading with crossorigin.
Typical preload hint in the document <head>:
The browser fetches app.js early with script-level priority.
Here’s a simple example of using the as attribute with the <link> element:
<link rel="preload" href="styles.css" as="style">In this example, the as attribute is set to style, providing a hint to the browser that the linked resource is a stylesheet. This helps browsers prioritize and optimize the loading of stylesheets for improved performance.
You can also dynamically set the as attribute using JavaScript. This can be useful when you need to adjust resource hints based on certain conditions or user interactions. Here’s a brief example:
<link rel="preload" href="demo.js" as="script" id="dynamicLink">
<script>
document.getElementById("dynamicLink").setAttribute("as", "script");
</script>In this script, the as attribute is set on a preload <link> with the id dynamicLink. Set resource hints before the browser fetches the file for best results. For ES modules, use rel="modulepreload" instead of changing as to an invalid value.
Font files require as="font" and the crossorigin attribute:
<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>Without the correct as value and CORS mode, font preloads may be discarded or fail to match the font request later.
Set rel="preload", href, and as.
Applies correct fetch priority and storage bucket.
File is ready when CSS, JS, or fonts are needed.
Critical resources arrive sooner with correct browser handling.
The as attribute with rel="preload" is supported in all modern browsers. Some older browsers ignore preload hints gracefully.
All major browsers honor as on preload and prefetch links.
Bottom line: Use as with confidence on preload links in all modern browsers.
as with rel="preload" for critical resourcesas to the actual file typecrossorigin for font and CORS resourcesrel="modulepreload" for ES module scriptsas on regular rel="stylesheet" linksmodule for the as attributeas and the actual resource typeThe as attribute is a valuable tool in HTML for optimizing the loading of linked resources. By leveraging this attribute with rel="preload", developers can enhance performance and provide a smoother user experience on their websites.
Always pair as with the correct resource type and use it selectively for files that truly benefit from early fetching.
asBookmark these before adding preload hints.
Type for preload.
BasicsNot on script.
Scopescript, style, font.
ValuessetAttribute()
DynamicEarly fetching.
Speed<link> element, typically with rel="preload" or rel="prefetch".as value may be ignored by the browser.rel="preload" with as="style" to fetch CSS early, then apply it with a separate stylesheet link.setAttribute("as", "script") or when creating preload links in script.as with preload. Internet Explorer does not support preload.Practice the as attribute with preload examples in the Try It editor.
5 people found this page helpful