← Web design from scratch

Add images, buttons and links

In this lesson, you’ll add images, create different kinds of links and design calls to action that help visitors navigate your site.

Level · Beginner Time · 25–30 min Path · Web design

01

Add an image with HTML

Use the img element to add an image.

<img src="image.jpg" alt="Image description">

img is a void element, so it has no closing tag.

This example uses two important attributes.

01

src tells the browser where to find the image file.

02

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:

images

Your project might look like this:

my-first-website/
├── index.html
├── styles.css
└── images/
    └── workspace.jpg

From index.html, load the image like this:

<img src="images/workspace.jpg" alt="Workspace desk">
Each path must match the actual file name and location.

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:

<img
  src="images/web-project.jpg"
  alt="Homepage of a web design studio"
>

For a purely decorative image, use an empty alt attribute:

<img src="images/decoration.jpg" alt="">

04

Make your images responsive

You saw this rule in the previous lesson:

img {
  max-width: 100%;
  height: auto;
}

It keeps the image within the available space.

You can also control how the image fills a container:

.project-image {
  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:

<a href="services.html">View services</a>

To link back to the homepage, write:

<a href="index.html">Home</a>
Check that the name in 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.

<section id="services">
  <h2>Services</h2>
</section>

Then link to that ID.

<a href="#services">View services</a>

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:

<a href="https://example.com">
  Visit website
</a>

If it needs to open in a new tab, write:

<a
  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.

<a href="contact.html" class="button">
  Tell me about your project
</a>

Use CSS to control how it looks:

.button {
  display: inline-block;
  padding: 14px 24px;
  background: #222222;
  color: #ffffff;
  text-decoration: none;
}
Use a link for navigation. Use a button for actions such as submitting a form or running a function.

09

Add hover and focus styles

Give visitors a visual cue when they hover over an interactive element.

.button:hover {
  opacity: 0.8;
}

Add a cue for people who navigate with a keyboard too.

.button:focus-visible {
  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.

<section class="project">

  <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.

01

Choose dimensions that suit where the image will appear.

02

Compress images before publishing them.

03

Use modern image formats where appropriate.

04

Give your files descriptive names.

For example, use:

web-design-project.jpg

Instead of:

IMG_847293.jpg

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.

Complete your website content

Open the project you built in the previous lessons.

  1. Create a folder named images.
  2. Add at least one image.
  3. Write appropriate alt text.
  4. Check that the image is responsive.
  5. Add a link to a section on the page.
  6. Add a link to another HTML page.
  7. Style a call-to-action link as a button.
  8. Add a hover state.
  9. Test keyboard navigation.
  10. Check that all paths work.
Test every link before moving on. A broken link can stop a visitor from reaching the content or action they need.

PREVIOUS LESSON

← 07 · Make your website mobile-friendly