HTML links: anchors, paths, new tabs and page jumps
Create links to other sites, your own pages and sections of a page, plus email, phone and download links.
In this lesson you will
- Create links with clear, descriptive text
- Choose between absolute and relative paths
- Open tabs safely and jump within a page
- Make email, phone and download links
About 11 minutes
Why links are the heart of the web
The “hyper” in HyperText means text that links to other text. Links are what turn a pile of separate documents into the web. The element is called an anchor, written <a>, and its destination goes in the href attribute (hypertext reference).
<a href="https://www.wikipedia.org">Visit Wikipedia</a>
The text between the tags is what the visitor clicks. Make that text meaningful.
| Poor link text | Better link text |
|---|---|
| Click here | Read the HTML beginner guide |
| Link | Download the price list (PDF) |
| More | See all courses |
Meaningful link text helps screen reader users, who often list all the links on a page, and it helps search engines understand the destination.
Three kinds of link address
- Absolute URLs
- The full address, including
https://. Use them for other websites. - Relative URLs
- A path from the current file, for pages on your own site.
about.htmlmeans a file in the same folder. - Fragment links
- Start with
#and jump to a part of a page that has a matchingid.
Linking inside your own site
Imagine this folder structure:
my-website/
index.html
about.html
blog/
first-post.html
images/
logo.png
| From this file | To this file | Write |
|---|---|---|
index.html |
about.html |
href="about.html" |
index.html |
blog/first-post.html |
href="blog/first-post.html" |
blog/first-post.html |
index.html |
href="../index.html" |
blog/first-post.html |
about.html |
href="../about.html" |
The two dots .. mean “go up one folder”. Relative links keep working when you move the entire site to a new domain, which is why professionals prefer them for internal links.
A link starting with a slash, such as /about.html, always starts from the top of the website no matter where the current file is. WordPress and most large sites use this style.
Opening links in a new tab
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Example (opens in a new tab)</a>
The target="_blank" attribute opens a new tab. Always pair it with rel="noopener noreferrer", which stops the new page from getting control of yours and protects your visitors. Use new tabs sparingly. Many people prefer to decide for themselves, and on a phone extra tabs are annoying.
Jumping within a page
<a href="#prices">Go to prices</a>
<!-- ...lots of content... -->
<h2 id="prices">Prices</h2>
An id must be unique on the page and should use lower case and hyphens. Fragment links are excellent for long pages such as guides and FAQs, and they let you share a link to an exact section.
Email, phone and download links
<a href="mailto:hello@example.com">Email us</a>
<a href="tel:+911234567890">Call us</a>
<a href="files/guide.pdf" download>Download the guide</a>
The mailto: and tel: links open the visitor’s email app or phone dialler. The download attribute asks the browser to save the file instead of opening it.
Putting your real email address in a mailto: link invites spam robots to harvest it. A contact form is safer for a public site, and you will build one soon.
Try it: build a mini menu
Make each list item a link. The first goes to index.html, the second to about.html, the third to https://example.com and opens in a new tab.
<p>Home</p>
<p>About</p>
<p>Example site</p>
Wrap the text: <a href="index.html">Home</a>. For the third add target="_blank" rel="noopener noreferrer".
<p><a href="index.html">Home</a></p>
<p><a href="about.html">About</a></p>
<p><a href="https://example.com" target="_blank" rel="noopener noreferrer">Example site</a></p>
Link states and checking your work
By default links are blue and underlined, turn purple once visited, and show a different colour while pressed. You will restyle them with CSS later, but never remove the underline without giving another strong visual hint. People need to recognise what is clickable. Always test every link after you write it. A broken link is one of the quickest ways to lose a visitor’s trust.
Worked example: a navigation bar of links
Most sites have a row of links at the top. Here is the structure written the way professionals write it, using a list of links inside a nav element. You will meet both elements properly soon, so focus on the links.
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="courses.html">Courses</a></li>
<li><a href="blog/index.html">Blog</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
Every page in the site repeats this block, with one small change: pages inside the blog folder need ../ before each path. This is the number one reason links break on beginner sites, and now you know why. Later, tools such as WordPress generate these menus for you, but understanding the path logic means you can always diagnose a broken one.
Making an image a link
Any content can sit inside an anchor, not just text. Wrapping a logo in a link to the home page is a web convention that visitors expect.
<a href="index.html"><img src="images/logo.png" alt="Skills in Minutes home"></a>
Link checking routine
Before you publish any page, click every link yourself, once from the start page and once from a deeper page. Check that external links still work, that the text says what the destination is, that downloads open the right file, and that nothing points to a file:///C:/Users/... address, which only works on your own computer. Browser developer tools and free link checkers can scan a whole site later.
Practice exercises
- Create three files:
index.html,about.htmlandcontact.html. Link them all to each other. - Create a
blogfolder withpost.htmlinside. Link from the post back to the home page using../. - Write a long page with three sections and a “Jump to” list of fragment links at the top.
- Add a
mailto:link and test that it opens your email app.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which attribute holds a link’s destination?
srchreflinkurl
Hypertext reference, abbreviated href, is the attribute for the destination.
Which link text is best?
- Click here
- Read more
- Download the free HTML cheat sheet
- Link
Descriptive text tells visitors and search engines what they will find.
From blog/post.html, which link reaches index.html in the main folder?
index.html../index.html/blog/index.htmlblog/index.html
Two dots go up one folder, so ../index.html reaches the file one level above.
What should you add alongside target="_blank"?
class="new"rel="noopener noreferrer"lang="en"id="top"
This protects visitors by stopping the opened page from controlling the original one.
Which link jumps to an element with id="faq"?
href="faq"href="#faq"href="@faq"href=".faq"
A fragment link starts with a hash followed by the id.
Summary
- Links use the a element with an href.
- Relative paths link your own files; absolute URLs link other sites.
- Pair target=”_blank” with rel=”noopener noreferrer”.
- An id plus #id creates an in-page jump.
Questions
What does href stand for?
Hypertext reference. It holds the address the link points to.
Should every external link open in a new tab?
No. Use new tabs only when leaving would lose something important, such as a half-filled form. Otherwise let visitors choose.
Why is my relative link broken?
Check the file name spelling, capital letters, and whether the file is in a different folder. A slash or two dots changes the starting point.
Can an image be a link?
Yes. Put an img element inside the a element. You will practise that in the next lesson.
