What is the anchor tag?

The <a> tag (short for "anchor") is how you create links in HTML. It's one of the most important elements โ€” without it, the web wouldn't be "World Wide Web" at all. Links connect pages together.

The <a> tag uses the href attribute to specify where the link goes:

  • href="https://example.com" โ€” Links to another website (absolute URL)
  • href="about.html" โ€” Links to another page on your site (relative URL)
  • href="mailto:hello@example.com" โ€” Creates an email link
  • href="tel:+1234567890" โ€” Creates a phone link
  • href="#section-id" โ€” Jumps to a section on the same page (anchor link)
Key point:
The href attribute tells the browser where to go when the user clicks the link. Without it, the <a> tag does nothing.

Try it yourself: Various link types

Create a page with different types of links. Edit the code below and click Run to see how each link behaves.

โœ๏ธ Try It Yourself โ€” Edit & Run
Preview

The target attribute

The target attribute controls where a link opens:

  • target="_blank" โ€” Opens in a new tab (most common for external links)
  • target="_self" โ€” Opens in the same tab (default behavior, usually not needed)
  • target="_parent" โ€” Opens in the parent frame (advanced, for iframes)
  • target="_top" โ€” Opens in the topmost frame (advanced, breaks out of framesets)
target-examples.html Using the target attribute
UX tip:
Use target="_blank" sparingly. It's helpful for external links and resources, but overusing it can confuse users. Let them control whether they want a new tab.

Try it yourself: In-page navigation

Build a page with a navigation menu that uses anchor links to jump between sections. Try clicking the nav links in the preview.

โœ๏ธ Try It Yourself โ€” Navigation Menu
Preview

Link best practices and accessibility

Good links are clear, descriptive, and help users understand where they're going:

  • Use descriptive link text. "Click here" tells users nothing. Instead: "Read our blog post about web design" or "Visit our pricing page".
  • Make links visually distinct. Links should look different from regular text (usually blue and underlined).
  • Use target="_blank" sparingly. Only use it for external sites when absolutely necessary. Users can right-click and open in new tab themselves.
  • Check for broken links. Dead links are frustrating. Test all your links before publishing.
  • Use meaningful anchor IDs. IDs should describe the section: id="pricing" not id="section3".
  • Never set href="#". This causes the page to jump to the top. Use JavaScript if you need a non-functional link.
accessibility.html Good vs bad link practices
Accessibility note:
Screen readers read link text aloud. If your text is "click here," screen reader users won't know where the link goes. Always use descriptive link text.
Mini Challenge: Build a Restaurant Page

Task: Create a complete restaurant page with navigation. Include:

  • A navigation menu at the top with anchor links to 3 sections
  • Three sections: Menu, Location, and Contact, each with an id
  • Each section should have a heading and some content
  • An external link to Google Maps (use target="_blank")
  • An email link in the Contact section
  • A phone link in the Contact section

Build it in one of the Try It Yourself editors above or create your own HTML file.

Hint: Start with basic structure: create a <nav> with three anchor links pointing to #menu, #location, and #contact. Then create three <section> elements with matching IDs. Use <a href="https://maps.google.com" target="_blank"> for the map link, <a href="mailto:..."> for email, and <a href="tel:..."> for phone.

Key takeaways

  • The <a> tag creates links. The href attribute specifies where the link goes.
  • Use absolute URLs for external sites, relative URLs for your own site.
  • Special link types: mailto: for email, tel: for phone, #id for anchor links.
  • The target="_blank" attribute opens links in a new tab.
  • Navigation menus use <nav> with links or lists of links.
  • Always use descriptive link text โ€” not "click here".
  • Anchor links let users jump between sections on the same page using matching href="#id" and id attributes.

Test your knowledge

Question 1 of 5
Which attribute specifies where a link goes?
Question 2 of 5
What does target="_blank" do?
Question 3 of 5
How do you create an email link?
Question 4 of 5
How do you create a link to a section on the same page?
Question 5 of 5
Why is descriptive link text important?
0
/5
Great work! You understand HTML links.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just connected the web with links.
Build a complete site next.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’