HTML download Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Links & Files

What You’ll Learn

The download attribute on <a> and <area> links tells the browser to download the linked resource instead of opening it in the page. You can optionally set a custom filename so users save files with a clear, descriptive name.

01

Force Download

Save, not navigate.

02

Custom Name

Filename string.

03

a and area

With href only.

04

Same Origin

Security rule.

05

JavaScript

link.download.

06

data / blob

Generated files.

Purpose of download

Without download, clicking a link to a PDF, image, or other file may open the resource in a new tab instead of saving it. The download attribute prompts the browser’s save dialog and lets you suggest a filename that is clearer than the server path.

This improves user experience when offering templates, reports, images, or exported data. It is especially helpful when the original URL filename is generic (like file?id=42) but you want the saved file to be readable (like invoice-march.pdf).

💡
Filename, not a path

The download value should be a filename such as report.pdf, not a full URL like /files/report.pdf.

📝 Syntax

Add download to an anchor with a valid href:

download.html
<a href="/files/honey-bee.pdf" download="honey-bee-guide.pdf">

  Download PDF

</a>

Syntax Rules

  • Only valid on <a> and <area> elements with an href.
  • Optional string value suggests the saved filename (include the extension).
  • Empty download or download="" still forces download with a default name.
  • Same-origin URLs are required for reliable custom filenames in most browsers.

💎 Values

The download attribute accepts an optional filename string:

  • download="my_document.pdf" — Save as my_document.pdf.
  • download="photo.png" — Save an image with a custom name.
  • download (no value) — Download with the resource’s default filename.
download-values.html
<!-- Custom filename -->

<a href="data.csv" download="sales-2026.csv">Export</a>



<!-- Default filename -->

<a href="chart.png" download>Save chart</a>

⚡ Quick Reference

ItemDetailsNotes
Elements<a>, <area>Must have href
ValueFilename string (optional)Include extension
BehaviorDownload instead of navigateOpens save dialog
Same originRequired for custom nameCross-origin may fail
JS propertylink.downloadSet filename dynamically
Generated filesblob:, data: URLsCommon with JS exports

Applicable Elements

ElementSupported?Notes
<a href="...">YesPrimary use case
<area href="...">YesImage map hotspots
<button>NoUse anchor or JS Blob download
<img>NoWrap in <a download>

download vs normal link

Link typeUser actionTypical result
<a href="doc.pdf">ClickBrowser may open PDF in tab
<a href="doc.pdf" download>ClickBrowser downloads the file
<a href="doc.pdf" download="guide.pdf">ClickDownloads as guide.pdf
Cross-origin hrefClick with downloadMay navigate instead of download

Examples Gallery

PDF with a custom filename, image download links, and dynamic filenames with JavaScript.

Example — PDF with Custom Filename

Download a PDF and save it with a friendly name:

pdf-download.html
<a href="/files/honey-bee.pdf" download="honey-bee-guide.pdf">

  Download PDF

</a>
Try It Yourself

How It Works

The browser downloads the file from href but uses the name from download in the save dialog.

Example — Downloadable Image Link

Wrap an image so clicking it saves the file:

image-download.html
<a href="/images/javascript.png" download="javascript-logo.png">

  <img src="/images/javascript.png" alt="Download logo">

</a>
Try It Yourself

How It Works

The link points to the image URL. download tells the browser to save the file instead of displaying it.

Dynamic Values with JavaScript

Set the download filename when generating files dynamically:

dynamic-download.html
<a id="dynamicDownloadLink" href="data:text/plain,Hello%20World!">Download</a>



<script>

  document.getElementById("dynamicDownloadLink").download = "generated_file.txt";

</script>
Try It Yourself

How It Works

JavaScript can set download on links pointing to data: or blob: URLs created at runtime.

♿ Accessibility

  • Use clear link text — Say “Download PDF report” instead of vague “Click here.”
  • Indicate file type — Mention format and size when helpful (e.g. PDF, 2 MB).
  • Do not rely on images alone — If the link wraps an image, provide meaningful alt text.
  • Warn about large files — Let users know before starting a big download.

🧠 How download Works

1

User clicks link

Anchor with href and download attribute.

Click
2

Browser fetches resource

Same-origin check for custom filename.

Fetch
3

Save dialog appears

Suggested filename from download value.

Save
=

File saved locally

User gets the file without leaving your page.

Browser Support

The download attribute is supported in all modern browsers. Same-origin restrictions apply consistently across engines.

HTML5 · Fully supported

Download links in modern browsers

Chrome, Firefox, Safari, and Edge support download on a and area elements.

97% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
download attribute 97% supported

Bottom line: Use download for same-origin files; test cross-origin behavior separately.

💡 Best Practices

✅ Do

  • Use descriptive filenames with extensions
  • Host downloadable files on the same origin
  • Wrap images in download links when needed
  • Use blob URLs for client-generated exports
  • Tell users what they are downloading

❌ Don’t

  • Put full paths in the download value
  • Expect custom names on cross-origin URLs
  • Use download on elements without href
  • Hide malicious downloads behind vague link text
  • Rely on download for HTML pages you want viewed

Conclusion

The download attribute is a valuable tool for offering files directly from hyperlinks. It turns navigation links into save actions and lets you suggest clear filenames for PDFs, images, exports, and other resources.

Use it on same-origin <a> links, pair descriptive link text with the correct file extension, and set filenames dynamically with JavaScript when generating content at runtime.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about download

Bookmark these before adding download links.

5
Core concepts
📄 02

Filename

Not a URL path.

Value
🔗 03

a / area

With href.

Scope
🔒 04

Same Origin

For custom names.

Security
⚙️ 05

JS + blob

Dynamic exports.

Advanced

❓ Frequently Asked Questions

It tells the browser to download the linked resource instead of navigating to it, with an optional suggested filename.
<a> and <area> elements that have an href attribute.
A filename string like report.pdf. Omit the value to download with the default name.
Cross-origin links may not download with a custom filename. The browser may open the URL instead. Same-origin links work reliably.
Yes. Set link.download = "filename.ext". This is common with dynamically created blob: or data: URLs.
Yes in all modern browsers. Test same-origin and cross-origin cases in your target browsers.

Offer files with one click

Practice the download attribute with PDF and image links in the Try It editor.

Try PDF download example →

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.

3 people found this page helpful