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 linkhref="tel:+1234567890"โ Creates a phone linkhref="#section-id"โ Jumps to a section on the same page (anchor link)
href attribute tells the browser where to go when the user clicks the link. Without it, the <a> tag does nothing.
Creating basic links
Here are the most common ways to create links:
Absolute vs Relative URLs
Absolute URLs are complete addresses that work from anywhere. They include the full protocol and domain:
https://www.example.com/about.htmlโ Full URL, works from any websitehttps://www.example.comโ Homepage
Relative URLs are paths relative to your current page. They only work on your own site:
about.htmlโ Same directory as current pagepages/contact.htmlโ File in a subdirectory../index.htmlโ File in the parent directory (one level up)
Special link types: Email, phone, and anchor links
Beyond web links, you can create email links, phone links, and anchor links that jump to sections on the same page.
How anchor links work
Anchor links jump to a specific section on the page by matching the href="#id-name" with an element's id attribute. When you click the link, the page automatically scrolls to that section.
- The link:
<a href="#section1">Go to Section 1</a> - The target:
<h2 id="section1">Section 1</h2> - When clicked, the page jumps to the h2
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.
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="_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.
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"notid="section3". - Never set href="#". This causes the page to jump to the top. Use JavaScript if you need a non-functional link.
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. Thehrefattribute 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,#idfor 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"andidattributes.
Test your knowledge
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 โ