What is the purpose of the download attribute in an anchor tag?
β’5 min read

Here is a clear explanation with example code π
β
Purpose of the download Attribute in an Anchor (<a>) Tag
The download attribute in an <a> tag tells the browser to download the file instead of opening it in the browser.
πΉ What it does:
- Forces the linked file (image, PDF, text, etc.) to download.
- Allows you to define the file name for the downloaded file.
- Prevents the browser from displaying the file preview.
β Basic Example
<a href="example.pdf" download>Download PDF</a>
JavaScriptβ Clicking this link will download example.pdf instead of opening it in the browser.
β Example With Custom File Name
<a href="report.pdf" download="MyReport.pdf">Download Report</a>
JavaScriptβ The file will download as MyReport.pdf, even though the actual file name is report.pdf.
β Downloading an Image File
<a href="photo.jpg" download="holiday-photo.jpg">Download Image</a>
JavaScriptβ Image will be downloaded instead of previewing.


