Create a DOWNLOAD link in a single line of HTML

I am a Front end developer currently working on React JS, HTML, CSS, Python, Vue JS, Webpack, Babel, AEM etc.
Search for a command to run...

I am a Front end developer currently working on React JS, HTML, CSS, Python, Vue JS, Webpack, Babel, AEM etc.
No comments yet. Be the first to comment.
Are you planning to start your coding journey?. Then this is for you - a roadmap to help you achieve your goals.

7 ways in which you can add files to your commit using git add

Display Hello World and custom message using two-way data binding

Build a random quote generator using an API - JavaScript

Kritika Pattalam Bharathkumar | Web Developer - The official blog of Kritika Pattalam Bharathkumar
22 posts
Front End Engineer | Writes about HTML, CSS, JavaScript and everything related to web development | Follow me on Twitter let's chat.
As a website user, one would have come across links or buttons which say "Click to download" or "Download" on a website. Have you ever wondered how can you add one to your website?. Keep reading!!!.
As part of this blog let us see how one can create a download link or button which lets you download an asset (e.g. image, pdf, files, etc.). Do you know what is even more interesting?. This can be done with just a single line of HTML.
The simple way to achieve this is
Yay!!! You have created your download link now.
Simply add some CSS to make the anchor tag look like a button.
<a href="https://blog.kritikapattalam.com/images/click-me.jpg" download>Click to download</a>
In the above-given example, when the user clicks on download, it will save the file as "click-me.jpg", since that is the file name specified in the href attribute.
Simply add the name as a value for the download attribute. For example in the below code, I have added download = test, hence when the user clicks on download the file will be saved as test.jpg
<a href="https://blog.kritikapattalam.com/images/click-me.jpg" download="test">Click to download</a>
NO, they cannot. Download attribute works only for files from the same Origin URL's (i.e the file should be hosted on the same origin as the site you are trying to implement it in) or blob or data URL files.
In the below example, href value is from twitter.com whereas my site is blog.kritikapattalam.com. In this case, the download button will not work.
<!--- Site origin - https://blog.kritikapattalam.com --->
<a href="https://twitter.com/images/click-me.jpg" download="test">Click to download</a>
Well, that is it for this blog, and by now you should know how to create a download on your website.
Follow me on Twitter | LinkedIn for more web development related tips and posts. Feedbacks and suggestions are welcome.