How to use the download attribute

Default avatar.
April 22, 2013
How to use the download attribute.

ThumbnailHTML5 came with all new APIs, new input types and attributes for forms. As is often the case, those major additions often obscure the minor upgrades and I think that this is particularly true of the download attribute.

As you know, there are some files that the browser doesn't automatically download; images, other web pages and depending on the settings in your browser, sometimes even PDFs. The download attribute gives the browser a native way to download these files automatically, without having to fall back on JavaScript. This is really useful for any app that deals with the downloading of images, such as image upload sites.

Using the download attribute

Since the download attribute doesn't use scripts of any kind, it's as simple as adding the attribute to your link:

<a href="myFolder/myImage.png" download>Download image</a>

What's great about this attribute is that you can even set a name for the downloadable file, even when it's not the name on your server. This is great for sites with complex file names, or even dynamically created images, that want to provide a simple and user-friendly file name. To provide a name, you just add an equals sign, followed by the name you want to use surrounded in quotes, like so:

<a href="myFolder/reallyUnnecessarilyLongAndComplicatedFileName.png" download="myImage">Download image</a>

Note that the browser will automatically add the correct file extension to the downloaded file, so you don't need to include that inside your attribute value.

Browser support

Currently, only Chrome 14+ and Firefox 20+ support the download attribute, so you may need to fall back on some simple JavaScript to detect if the attribute is supported. You can do so like this:

var a = document.createElement('a');

if(typeof a.download != "undefined")
{
// download attribute is supported
}
else
{
// download attribute is not supported
}

Conclusion

Taking into consideration everything that has been added to HTML5, the download attribute is a very small part, but in my opinion it's an attribute that was long overdue, and definitely has its uses in today's apps for both usability and simplification.

Have you implemented the download attribute? What are your unsung heroes of HTML5? Let us know in the comments.

Featured image/thumbnail, download image via Shutterstock.

Sara Vieira

Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website.

Read Next

3 Essential Design Trends, November 2024

Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…

20 Best New Websites, October 2024

Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…

Exciting New Tools for Designers, October 2024

We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…

15 Best New Fonts, September 2024

Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…

3 Essential Design Trends, October 2024

This article is brought to you by Constantino, a renowned company offering premium and affordable website design You…

A Beginner’s Guide to Using BlueSky for Business Success

In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…

The Importance of Title Tags: Tips and Tricks to Optimize for SEO

When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…

20 Best New Websites, September 2024

We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…

Exciting New Tools for Designers, September 2024

This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…

3 Essential Design Trends, September 2024

September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…

Crafting Personalized Experiences with AI

Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…

15 Best New Fonts, August 2024

Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…