HomeHTML Tutorials 〉 HTML Anchor Tag ('<a>') - Syntax, Attributes, and Examples for Beginners

HTML Anchor Tag ('<a>') - Syntax, Attributes, and Examples for Beginners !

HTML Anchor Tag ('<a>') - Syntax, Attributes, and Examples for Beginners !

What is the HTML anchor tag and how is it used in web development?

The HTML anchor tag '<a>' is used to create hyperlinks that link to other pages, files, or sections of a page. This blog post explains the syntax, type, and attributes of the anchor tag with real-world examples and CSS styling tips. We also explored related topics, SEO linking practices, and FAQs. The complete blog post on HTML anchor tag is as follows.

Introduction

Want to create links on your webpage? The HTML anchor tag ('<a>') is the most commonly used element to create clickable links in a web document. Whether you're linking to another page, an external site, a document, or even a specific section within a page — the anchor tag makes it possible.

This guide will help you understand the anchor tag syntax, its attributes, use cases, and how to style links with CSS.

What is the HTML Anchor Tag?

The '<a>' tag (anchor tag) is used to define a hyperlink. It connects one page to another or to different parts of the same page.

Syntax:

Syntax ✍

<a href="URL">Link Text</a>

  • '<a>': Opening tag.
  • 'href': Attribute that specifies the destination URL.
  • 'Link Text': The clickable text shown to users.
  • '</a>': Closing tag.

HTML Anchor Tag Type:

It is an inline-level element, meaning it does not start on a new line and only takes up as much width as necessary.

Common Attributes of the Anchor Tag

Here are the most important and frequently used attributes of the '<a>' tag:

1. 'href' (Hyperlink Reference)

Specifies the destination URL.

Code : 1 📝

<!DOCTYPE html>
<html>
<body>

<a href="https://www.google.com">Visit Google</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

Clicking the link redirects the user to Google.

2. 'target'

Specifies where to open the linked document.

Code : 2 📝

<!DOCTYPE html>
<html>
<body>

<a href="https://example.com" target="_blank">Open in New Tab</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

  • target="_blank" opens the link in a new browser tab.
  • target="'_parent"
  • target="_self"
  • target="_top"

3. 'title'

Provides additional information on hover.

Code : 3 📝

<!DOCTYPE html>
<html>
<body>

<a href="https://example.com" title="Click to visit Example">Example Site</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

Shows a tooltip when hovering over the link.

4. 'download'

Prompts file download when clicking the link.

Code : 4 📝

<!DOCTYPE html>
<html>
<body>

<a href="image.jpg" download>Download Image File</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

Triggers file download instead of opening the file.

5. 'rel'

Specifies the relationship between the current and linked document.

Code : 5 📝

<!DOCTYPE html>
<html>
<body>

<a href="https://external.com" rel="nofollow">External Link</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

Tells search engines not to pass link equity.

6. 'type'

Specifies the MIME type of the linked document (used with 'download' or scripts).

Code : 6 📝

<!DOCTYPE html>
<html>
<body>

<a href="audio.mp3" type="audio/mpeg" download>Download Audio</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

Helps browsers identify the content type.

Example with CSS Inline Styling

Code : 7 📝

<!DOCTYPE html>
<html>
<body>

<a href="https://example.com" style="color: white; background-color: #007BFF; padding: 10px; border-radius: 5px; text-align: center; display: inline-block; font-size: 16px;">
Visit Example
</a>

</body>
</html>

output 📌

Try It....

You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.

  • 'color': Sets text color.
  • 'background-color': Sets button-like background.
  • 'padding': Adds space around text.
  • 'border-radius': Rounds the corners.
  • 'text-align': Centers text.
  • 'font-size': Makes the text readable.

Conclusion

The anchor ('<a>') tag is a fundamental part of HTML and is used to create all types of links on a webpage. Understanding its attributes, syntax, and styling options allows you to make your content interactive and user-friendly. It's a small tag with powerful functionality, essential for both navigation and user engagement.

Suggested Topics:

Related Topics:

Frequently Asked Questions (FAQs)

What is the purpose of the anchor tag in HTML?

It is used to create hyperlinks to other web pages, files, or locations within the same document.

Can we use multiple attributes in one anchor tag?

Yes, you can use attributes like 'href', 'target', 'title', and 'download' together.

Is the anchor tag a block or inline element?

It is an inline element by default.

How can I make the anchor tag look like a button?

Use CSS inline styling or class-based styling with properties like padding, background-color, border-radius, etc.

What does 'target="_blank"' do?

It opens the linked page in a new browser tab.