The basic HTML code for a link
A link to another website uses the anchor tag, written as <a> in HTML. The tag wraps around the text you want to be clickable, and an href attribute tells the browser where to go when someone clicks it.
Here is the simplest working example:
<a href="https://www.example.com">Click here</a>
When you put this code on a webpage, it displays as a clickable link that says "Click here". The href="https://www.example.com" part is the instruction — it points to the website you want to link to. The text between the opening <a> and closing </a> tags is what the visitor sees and clicks on.
Key Takeaways
- Every link needs an opening <a> tag with an href attribute, the clickable text, and a closing </a> tag.
- The href value must be a complete web address starting with http:// or https:// when linking to external websites.
- The text between the tags is what visitors see — make it descriptive so they know where the link goes before clicking.
- Use target="_blank" if you want the link to open in a new tab instead of replacing the current page.
- Test every link after you add it to make sure it points to the right place and works from a visitor's browser.
Writing the href attribute correctly
The href attribute is where you put the web address. For links to external websites — any site that is not your own — always include the full address starting with https:// or http://.
These work:
- <a href="https://www.wikipedia.org">Wikipedia</a>
- <a href="https://www.github.com">GitHub</a>
- <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">Video</a>
A common mistake is leaving off the https:// part. If you write href="www.example.com" without the protocol, the browser treats it as a relative link and looks for a folder called www.example.com on your own server instead of going to the actual website. Always include the full address.
Making the link text descriptive
The text between <a> and </a> should tell the visitor where they are going. Avoid vague phrases like "click here" or "link" because they do not explain what the visitor will find.
Instead of:
<a href="https://www.mozilla.org">Click here</a>
Write:
<a href="https://www.mozilla.org">Mozilla Firefox browser</a>
Descriptive link text helps visitors decide whether to click, and it also helps screen readers used by people with vision loss understand what the link does. Search engines also pay attention to link text when deciding what a page is about.
Opening links in a new tab
By default, a link replaces the current page in the browser. If you want the link to open in a new tab instead, add target="_blank" to the anchor tag.
<a href="https://www.example.com" target="_blank">Visit Example</a>
This is useful when you are linking to an external site and you want visitors to stay on your page. Many websites use this for links to social media, external resources, or reference material. However, do not overuse it — most visitors expect links to behave normally and may find it annoying if every link opens a new tab.
If you use target="_blank", consider adding rel="noopener noreferrer" as well. This is a security measure that prevents the linked website from accessing certain information about your page.
<a href="https://www.example.com" target="_blank" rel="noopener noreferrer">Visit Example</a>
Testing your links
After you add a link to your webpage, open the page in a browser and click the link to make sure it goes to the right place. Check that the destination website loads completely and that you did not accidentally type the address wrong.
If a link does not work, the most common causes are a typo in the href, a missing https://, or a website that has moved or shut down. Copy the web address directly from your browser's address bar and paste it into the href attribute to avoid typing mistakes.
If you are building a larger website, test links from different browsers — Chrome, Firefox, Safari, and Edge all handle links the same way, but it is good practice to verify. Also test on a phone or tablet to make sure the link is easy to tap.
Linking to specific parts of a webpage
You can link not just to a website's home page, but to a specific section of a page using an anchor. This requires the destination page to have an id attribute on the element you want to link to.
If the destination page has code like <h2 id="pricing">Pricing</h2>, you can link directly to that section by adding a hash and the id name to the end of the web address:
<a href="https://www.example.com#pricing">See our pricing</a>
This is useful for linking to a specific section of a long page, like a FAQ section or a particular product. The visitor lands on the page and the browser automatically scrolls to the section you linked to.
Common mistakes to avoid
Forgetting the closing tag is one of the most common errors. Every <a> must have a matching </a>, or the rest of your page may display incorrectly.
Another mistake is putting the closing tag in the wrong place. The text that should be clickable goes between the tags:
Wrong: <a href="https://www.example.com"> Visit Example </a> website — only "Visit Example" is clickable.
Right: <a href="https://www.example.com">Visit Example website</a> — the whole phrase is clickable.
Do not nest links inside other links. You cannot put an anchor tag inside another anchor tag, and browsers will not display it correctly. If you need multiple links in one area, put them side by side instead of nested.
Frequently Asked Questions
Do I need to use https or is http okay?
Use https:// whenever possible. It is the secure version and is now the standard for all websites. Most modern websites redirect http:// to https:// automatically, so the link will still work, but https:// is the correct choice.
What is the difference between an external link and an internal link?
An external link points to a different website and needs the full web address with https://. An internal link points to another page on your own website and can use a shorter path like href="/about" or href="pages/contact.html". Internal links do not need the full web address.
Can I change the color or style of a link?
Yes, using CSS. By default, links are blue and underlined, but you can change this with a style sheet. Use the a selector in CSS to change the color, remove the underline, add a background color, or change the appearance when someone hovers over it.
What does rel="noopener noreferrer" actually do?
noopener prevents the linked page from accessing the window.opener object, which could let it control your page. noreferrer prevents the linked site from knowing which page sent the visitor. Both are security measures, especially important when using target="_blank".
How do I link to an email address instead of a website?
Use href="mailto:name@example.com" instead of a web address. When someone clicks it, their email program opens with a new message addressed to that email. You can also add a subject line: href="mailto:name@example.com?subject=Hello".