HTML <a> Tag
HTML <a>
Tag Tutorial by CodesWithPankaj
<a>
Tag Tutorial by CodesWithPankajThe <a>
tag in HTML is used to create links, allowing users to navigate from one page to another or link to external websites. It stands for "anchor" and is one of the most essential tags in HTML. Let's dive into its usage and sub-elements to understand how it works.
1. Basic Structure of <a>
Tag
<a>
TagThe syntax of the <a>
tag looks like this:
href
attribute: This is the most important attribute in the<a>
tag. It defines the URL where the link will go.Link Text: This is the clickable part of the link that the user will see.
Example 1: Creating a Simple Link
In this example:
href
is set to "https://www.codeswithpankaj.com".Link Text is "Visit CodesWithPankaj".
When clicked, this link will take the user to the CodesWithPankaj website.
2. Opening Links in a New Tab
If you want a link to open in a new tab, use the target
attribute with _blank
.
Here:
target="_blank"
instructs the browser to open the link in a new tab.
3. Creating Links to Sections of the Same Page
The <a>
tag can also link to different parts of the same page using anchors.
Step 1: Add an ID to the Section You Want to Link To
Step 2: Create a Link to the Section
Here:
The
href="#about"
links to the element withid="about"
.This link will scroll the page to the "About Us" section.
4. Linking to an Email Address
To create a link that opens an email app when clicked, use the mailto:
scheme in the href
attribute.
In this example:
href="mailto:support@codeswithpankaj.com"
opens the user’s email client to send an email tosupport@codeswithpankaj.com
.
5. Adding Titles to Links
You can add a title attribute to give extra information about the link. When the user hovers over the link, the title appears as a tooltip.
6. Downloading Files with <a>
Tag
<a>
TagThe <a>
tag can also be used to download files by using the download
attribute.
In this example:
download
prompts the browser to downloadsample.pdf
instead of opening it.
7. Styling Links with CSS
By default, links are blue and underlined. We can style them differently with CSS.
8. Full Example: Putting It All Together
This guide should help you understand the <a>
tag and its various uses. Practice by creating your own links to get comfortable with different attributes. Happy coding with CodesWithPankaj!
Last updated