Links and images
Links are what make the web a web. Without them every page would be an island. In this lesson you'll learn how to link to other pages, how to add images and how to tell the browser where to find files.
Your first link
Links use the a element, which stands for anchor. The href attribute says where the link goes:
<a href="https://developer.mozilla.org">MDN Web Docs</a>
The text between the tags is what people see and click on. The href is where they end up.
A tip about link text: make it describe where the link goes. "Read the MDN guide to HTML" is much better than "click here". People scanning a page, and people using screen readers, often jump from link to link, and a list of "click here" tells them nothing.
Opening links in a new tab
By default, a link opens in the same tab. To open it in a new tab, add target="_blank":
<a href="https://github.com" target="_blank">My GitHub profile</a>
Use this sparingly. Most people prefer to decide for themselves whether to open a new tab. It makes the most sense for links that leave your website, so visitors don't lose their place.
Absolute and relative paths
This part trips up almost everyone at first, so take it slowly.
An absolute path is a complete address, including https://. You use it to link to other websites:
<a href="https://www.wikipedia.org">Wikipedia</a>
A relative path points to a file in your own project, starting from the file you're in. Imagine this project:
my-website/
├── index.html
├── projects.html
└── images/
└── me.jpgFrom index.html, you'd link to the projects page like this:
<a href="projects.html">My projects</a>
No https:// and no domain. Just the file name, because projects.html sits in the same folder as index.html.
To reach a file inside another folder, add the folder name and a slash:
<img src="images/me.jpg" alt="Me smiling at the camera">
And to go up one folder, use two dots, just like cd .. in the terminal:
<a href="../index.html">Back to the home page</a>
Relative paths are great because they keep working when you move your whole website somewhere else, like from your computer to a real server.
Your page about.html is in the same folder as index.html. Which link from index.html works?
Select one answer
Adding images
Images use the img element. It's an empty element, so there's no closing tag:
<img src="images/me.jpg" alt="Me smiling at the camera">
srcis the path to the image. It works exactly likehrefon links, so both absolute and relative paths are fine.altis a text description of the image.
Why alt text matters
The alt text is easy to skip, but please don't. It's read aloud by screen readers for people who can't see the image. It's also shown if the image fails to load, and search engines use it to understand what the image shows.
Good alt text describes what's important about the image in a short sentence:
- Good:
alt="Golden retriever catching a frisbee in a park" - Not helpful:
alt="image"oralt="photo123.jpg"
If an image is purely decorative and doesn't add any information, use an empty alt, alt="". That tells screen readers to skip it instead of reading out the file name.
What should the alt attribute contain?
Select one answer
Image sizes
You can set the size of an image with width and height:
<img src="images/me.jpg" alt="Me smiling at the camera" width="300" height="300">
The numbers are in pixels. Setting both helps the browser save the right amount of space while the image loads, so the page doesn't jump around. Later, in the CSS course, you'll learn better ways to make images fit nicely on every screen.
One more tip: keep image files small. A photo straight from a phone can be several megabytes, which makes a page slow to load. For a website, a few hundred kilobytes is plenty. Free tools like Squoosh (squoosh.app) can shrink images right in your browser.
You're in the file blog/post.html and want to link to index.html in the folder above. Which path is right?
Select one answer
Add links and an image to your website:
- In your
my-websitefolder, create a folder calledimages - Put a photo of yourself, your pet or something you like in it. Rename it to something simple like
me.jpg(lowercase, no spaces) - Add the image to
index.htmlwith a relative path and a goodalttext - Add a link to a website you like, with descriptive link text
- Add a link to your GitHub, LinkedIn or another profile if you have one, opening in a new tab
If the image doesn't show up, check the path carefully. Is the folder name right? Is the file ending .jpg or .png? Paths are the most common source of broken images, for beginners and experienced developers alike.