HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML download Attribute
Photo Credit to CodeToFun
🙋 Introduction
The download
attribute in HTML is a powerful feature that allows you to specify a filename for the downloaded file when a user clicks on a hyperlink.
This attribute is especially useful when you want to provide users with the ability to download content, such as documents, images, or other files, and control the name of the file they receive.
🎯 Purpose of download
The primary purpose of the download
attribute is to enhance the user experience by giving them the option to save files with a specific name directly from the web page.
Without this attribute, the browser might use the original filename or prompt the user with a generic filename, depending on the file type.
💎 Values
The download
attribute accepts a string value, which represents the desired filename for the downloaded file.
This string can include the file extension. Here's an example:
<a href="example.pdf" download="my_document.pdf">Download PDF</a>
🧠 How it Works
In this example, clicking the link would download the file named my_document.pdf instead of the default example.pdf.
📄 Example
Let's look at a simple example of how to use the download
attribute in an HTML anchor tag:
<a href="sample.jpg" download="my_image.jpg">
<img src="sample.jpg" alt="Download Image">
</a>
🧠 How it Works
In this example, clicking the image would download the file named my_image.jpg instead of the default sample.jpg.
🔄 Dynamic Values with JavaScript
Similar to the autocomplete attribute, you can also set the download
attribute dynamically using JavaScript.
This is useful when you want to generate filenames based on certain conditions or user interactions. Here's a brief example:
<script>
// Dynamically set download filename for a link
document.getElementById("dynamicDownloadLink").download = "generated_file.txt";
</script>
🧠 How it Works
In this script, the download
attribute is set to generated_file.txt for a link with the id dynamicDownloadLink. This dynamic approach is particularly helpful when dealing with dynamically generated content.
🏆 Best Practices
- Use the
download
attribute to provide a better user experience when offering downloadable content on your website. - Ensure that the filename you specify is clear, descriptive, and includes the appropriate file extension.
- Be mindful of file types and their compatibility with the
download
attribute across different browsers.
🎉 Conclusion
The download
attribute is a valuable tool for web developers who want to give users more control over the filenames of downloaded files.
By incorporating this attribute into your links, you can offer a seamless and user-friendly download experience on your website.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML download Attribute), please comment here. I will help you immediately.