01
Add an image with HTML
Use the img element to add an image.
img is a void element, so it has no closing tag.
This example uses two important attributes.
src tells the browser where to find the image file.
alt provides a text alternative when the image conveys information.
02
Organise your images
As your site grows, organise its files into folders.
Create a folder named:
Your project might look like this:
├── index.html
├── styles.css
└── images/
└── workspace.jpg
From index.html, load the image like this:
03
Write an alt attribute
Use the alt attribute to describe the meaning of the image in context.
It gives visitors a text alternative when they cannot see an informative image.
Here’s an example:
src="images/web-project.jpg"
alt="Homepage of a web design studio"
>
For a purely decorative image, use an empty alt attribute:
04
Make your images responsive
You saw this rule in the previous lesson:
max-width: 100%;
height: auto;
}
It keeps the image within the available space.
You can also control how the image fills a container:
width: 100%;
height: 400px;
object-fit: cover;
}
object-fit: cover fills the container while keeping the image’s proportions. Some parts may be cropped.
05
Link your pages
You’ve already used the a element.
To link to another page on your own site, write:
To link back to the homepage, write:
href matches the actual file. A mismatch can cause a 404 error after publishing.06
Link to a section
You can also link to a section on the same page.
First, give the section an ID.
<h2>Services</h2>
</section>
Then link to that ID.
When a visitor selects the link, the browser takes them to that section.
07
Link to other websites
Here’s how to link to an external page:
Visit website
</a>
If it needs to open in a new tab, write:
href="https://example.com"
target="_blank"
rel="noopener noreferrer"
>
Visit website
</a>
Use a new tab when it helps the visitor. Opening the link in the same tab is usually enough.
08
Style a link as a button
A link can look like a button while still taking visitors to another location.
Tell me about your project
</a>
Use CSS to control how it looks:
display: inline-block;
padding: 14px 24px;
background: #222222;
color: #ffffff;
text-decoration: none;
}
09
Add hover and focus styles
Give visitors a visual cue when they hover over an interactive element.
opacity: 0.8;
}
Add a cue for people who navigate with a keyboard too.
outline: 2px solid currentColor;
outline-offset: 4px;
}
The focus-visible state helps keyboard users see which element has focus.
10
Write descriptive link text
A link’s text should tell visitors what to expect when they select it.
LESS CLEAR
Click here
This wording gives no destination on its own.
CLEARER
View services
This wording tells visitors what they’ll find.
11
Build a section with an image and a link
Combine an image, a short description and a call to action.
<img
src="images/web-project.jpg"
alt="Preview of a web design project"
>
<h2>Featured project</h2>
<p>
A simple, clear and responsive website.
</p>
<a href="#" class="button">
View project
</a>
</section>
Use the image to show the work, the text to explain it and the link to give visitors a next step.
12
Optimise images before uploading
Large image files can slow down a page even when the images look fine.
Choose dimensions that suit where the image will appear.
Compress images before publishing them.
Use modern image formats where appropriate.
Give your files descriptive names.
For example, use:
Instead of:
SUMMARY
What you’ve learned
- The img element adds images.
- The src attribute points to the image file.
- The alt attribute provides a text alternative where appropriate.
- Images should fit different screen sizes.
- Internal links connect pages on the same website.
- IDs let you link to sections.
- External links take visitors to other websites.
- You can style a link as a button.
- Links navigate and buttons perform actions.
- Hover and focus styles give visitors visual feedback.
- Descriptive link text tells visitors what to expect.
- Optimise images before publishing them.
PUT IT INTO PRACTICE
Complete your website content
Open the project you built in the previous lessons.
- Create a folder named images.
- Add at least one image.
- Write appropriate alt text.
- Check that the image is responsive.
- Add a link to a section on the page.
- Add a link to another HTML page.
- Style a call-to-action link as a button.
- Add a hover state.
- Test keyboard navigation.
- Check that all paths work.