What is the purpose of the download attribute in <a> elements?

Suppose you’re building a website, and you want to let users grab a file—like a PDF guide—with a single click. How do you make an anchor element (<a>) triggering a download instead of opening the file in the browser?

That’s when the download attribute is useful.

It’s a simple, powerful tool in HTML that tells browsers, “Hey, download this file, don’t just display it.

Let’s examine this attribute in detail.

Why was the download attribute created?

The <a> element is normally used to navigate to another page or open a resource, like an image or PDF, in the browser.

But sometimes, you need to manually specify what happens to the file instead of letting the browser decide. Using the download attribute forces the browser to save the file locally, giving you control over the process.

It’s as if you’re giving someone a file without letting them see it beforehand.

For example:

Clicking the link will download the “myfile.pdf”.
(click on the image to open in a new tab)

When a user clicks this link, their browser starts downloading myfile.pdf instead of opening it.

How Does It Work in Practice?

Adding the download attribute is very simple. You just include it into an <a> tag with an href pointing to your file.

You can even suggest a filename for the download by setting a value, like this:

Clicking the link will download the “myfile.pdf” and store it to your system with the denoted name (UserGuide.pdf).

This tells the browser to save the file as UserGuide.pdf, even if the original file has a different name.

But here’s a catch: the download attribute only works reliably for files on the same domain (or “origin”) as your website due to security restrictions.

If you’re linking to a file on another site, the browser might ignore the attribute. While annoying, it improves security.

Why is this relevant to you?

The download attribute isn’t just about convenience; it’s about user experience.

Nobody likes clicking a link expecting to save a file, only to have it open in an awkward browser tab.

By using download, you make your site feel user-friendly and professional. In addition, it’s a small but significant detail showing your focus on user needs which is always a plus in web development.

It’s also worth noting that not every file type needs this attribute.

Browsers already know to download certain files (like .zip or .exe) automatically. But for common web formats like PDFs, images, or text files, download is a great addition that ensures the expected results.

What potential issues should I be aware of?

While the download attribute is great, it’s not without flaws. Older browsers (like Internet Explorer) don’t support it, although their usage is very limited now.

We have already mentioned that the download attribute only works for files on the same domain.

Also, if the Content-Disposition specifies a filename, it takes priority over a filename specified in the download attribute.

Another thing: mobile browsers can be a bit quirky. Depending on device settings and available apps, some may still try to open files rather than download them. It’s helpful to test on a variety of devices.

Conclusion

The download attribute makes a simple <a> element directly send files to users. It provides a smooth way to share resumes, photos, and spreadsheets.

So, next time you’re linking to a file, ask yourself: should this open, or should it download? If the later, just use the download property!

Although my blog doesn’t support comments, feel free to reply via email or X.