A hyperlink is created with the HTML anchor tag, written as <a href="URL">link text</a>

The anchor tag tells a browser to turn text or an image into a clickable link. The href attribute holds the web address you want the link to point to. The text between the opening and closing tags is what the reader sees and clicks on.

Here is a working example: <a href="https://www.example.com">Click here</a> creates a link that says "Click here" and goes to example.com when clicked. The href must be a complete URL starting with http:// or https://, or a relative path to another file on the same website.

Key Takeaways

  • The anchor tag <a href="URL">text</a> is the only HTML element needed to create a hyperlink.
  • The href attribute must contain the full web address (https://example.com) or a relative path to a file on your site (/pages/about.html).
  • The text between the opening and closing tags is what appears on the page and is clickable by the reader.
  • You can link to external websites, pages within your own site, email addresses, or files by changing only the href value.
  • Adding target="_blank" to the anchor tag opens the link in a new browser tab instead of the current one.

The anatomy of an anchor tag

Every hyperlink has three parts: the opening tag, the link text, and the closing tag. The opening tag is <a href="URL">. The URL goes inside the quotation marks after href=. The link text comes next — this is what the reader sees on the page, usually in blue and underlined by default. The closing tag is </a>.

If you write <a href="https://www.wikipedia.org">Learn more</a>, the page displays "Learn more" as a clickable link. When someone clicks it, their browser goes to wikipedia.org. The href value is hidden from the reader — they only see the link text.

The href attribute is the only required part of an anchor tag. Without it, the tag does nothing. You can add other attributes like title, class, or id, but href is what makes it a link.

Linking to external websites

To link to another website, use the full URL in the href attribute, starting with http:// or https://. Most modern websites use https://, which is the secure version. <a href="https://www.google.com">Search Google</a> creates a link to Google's homepage.

Always include the protocol (http:// or https://). If you write only <a href="google.com">Search Google</a>, the browser treats it as a relative path and looks for a file called google.com on your own website, not the real Google site. This is a common mistake.

Linking to pages on your own website

When linking to another page on the same website, you do not need the full URL. Instead, use a relative path — the file path from your current page to the target page. If you have a file called about.html in the same folder as your current page, write <a href="about.html">About Us</a>.

If the target file is in a subfolder, include the folder name: <a href="pages/contact.html">Contact</a>. If you need to go up one level to a parent folder, use <a href="../index.html">Home</a>. The two dots (..) mean "go up one folder." This approach is faster and more flexible than using full URLs for internal links, because the links still work if you move your entire site to a different domain.

Opening links in a new tab or window

By default, a link opens in the same browser tab, replacing the current page. To open a link in a new tab instead, add target="_blank" to the anchor tag: <a href="https://www.example.com" target="_blank">Open in new tab</a>.

This is useful when you want the reader to stay on your page while also viewing the linked content. Many websites use target="_blank" for external links so readers do not lose their place. However, some users find it annoying when websites open new tabs without asking, so use it thoughtfully.

Linking to email addresses and phone numbers

You can create a link that opens the reader's email program instead of a web page. Use <a href="mailto:name@example.com">Email us</a>. When clicked, this link opens the default email client with the recipient field already filled in.

Similarly, you can link to a phone number with <a href="tel:+1-555-123-4567">Call us</a>. On mobile devices, this opens the phone dialer. On desktop, the behavior depends on what software is installed. Always include the country code (the + and 1 for the United States) in phone links so they work across regions.

Linking to a specific section on a page

You can link directly to a heading or section within a page using an anchor (also called a fragment identifier). First, add an id attribute to the element you want to link to: <h2 id="pricing">Pricing</h2>. Then link to it with a hash symbol: <a href="#pricing">Jump to pricing</a>.

To link to a section on a different page, combine the page path with the anchor: <a href="pages/services.html#faq">See our FAQ</a>. This is useful for long pages where you want readers to jump directly to the information they need. The id attribute must be unique on the page — no two elements can have the same id.

Frequently Asked Questions

What is the difference between href and src?

href is used in anchor tags to link to another page or resource. src is used in img and script tags to load an image or script file. They serve different purposes: href creates a clickable link, while src loads a resource into the page.

Do I need to use https:// or is http:// okay?

Always use https:// when possible. It encrypts the connection between the reader's browser and the website, protecting their data. Most websites now use https by default. Using http is not wrong, but it is less secure and some browsers warn users about it.

Can I link to a PDF or other file type?

Yes. Use the file path in the href attribute just as you would for an HTML page: <a href="documents/guide.pdf">Download guide</a>. When clicked, the browser either opens the file in a viewer or prompts the user to download it, depending on their browser settings.

What does the title attribute do on a link?

The title attribute adds a tooltip that appears when the reader hovers over the link: <a href="https://example.com" title="Visit example.com">Example</a>. This is optional and mostly used to provide extra context about where the link goes.

Can I use an image as a link?

Yes. Put an img tag inside the anchor tag: <a href="https://example.com"><img src="image.jpg" alt="description"></a>. When the reader clicks the image, they go to the URL in the href. Always include an alt attribute on the image for accessibility.