← Web design from scratch

Make your website mobile-friendly

A website needs to work on both large and small screens. In this lesson, you’ll learn the basics of responsive design and adapt the website you’ve already built.

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

01

What does responsive mean?

A responsive website adapts its layout to the size of the screen it’s viewed on.

The same content can be arranged differently on a computer, tablet and phone.

The goal is to build one flexible website that works at different screen sizes.

02

The viewport meta tag

You’ve already used this line in your HTML files.

<meta name="viewport" content="width=device-width, initial-scale=1.0">

This tag tells the browser to match the page width to the device’s viewport width.

01

width=device-width sets the viewport width to the device width.

02

initial-scale=1.0 sets the initial zoom level.

03

Avoid fixed widths

Imagine you write this CSS.

section {
  width: 1200px;
}

On a phone, that section could be much wider than the screen.

Here’s a more flexible option.

section {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

The section can shrink on small screens and is capped at 1,200 pixels on larger screens.

04

Make images flexible too

An oversized image can extend beyond the edge of the screen.

Use this rule to keep it within its container.

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

The image can shrink to fit its container while keeping its original proportions.

05

What are media queries?

Media queries let you apply CSS rules only when specific conditions are met.

Here’s an example.

@media (max-width: 768px) {

  h1 {
    font-size: 40px;
  }

}

This is how it works.

When the viewport is 768 pixels wide or less, the h1 is 40 pixels.

06

Adapt your main heading

In the previous lesson, you used a large heading.

h1 {
  font-size: 64px;
}

That size can be too large on a phone.

Add this rule for smaller screens.

@media (max-width: 768px) {

  h1 {
    font-size: 42px;
  }

}

07

Reduce spacing on mobile

Generous spacing works on a desktop, but can take up too much room on a smaller screen.

Suppose your sections use this padding.

section {
  padding: 90px 30px;
}

You can reduce it on smaller screens with this rule.

@media (max-width: 768px) {

  section {
    padding: 60px 20px;
  }

}

08

Switch to a single column

Imagine your services section uses a three-column grid.

.services-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

On mobile, switch to one column.

@media (max-width: 768px) {

  .services-grid {
    grid-template-columns: 1fr;
  }

}

The cards will stack vertically.

09

Adapt a simple navigation menu

If your horizontal menu no longer fits, stack the items vertically.

@media (max-width: 768px) {

  nav {
    flex-direction: column;
    align-items: flex-start;
    gap: 20px;
  }

}

Later, you can use JavaScript to build a hamburger menu like the one on Digital Ukhti.

Start with a simple menu that visitors can use comfortably.

10

Use fluid sizes where they help

CSS provides a useful function called clamp().

Here’s an example.

h1 {
  font-size: clamp(40px, 6vw, 72px);
}

This lets the text size change gradually as the viewport changes.

01

40px is the minimum size.

02

6vw is the preferred size, based on viewport width.

03

72px is the maximum size.

11

Test your website at different widths

You can do this with a single computer.

Browser developer tools can simulate different screen sizes.

01

Open your page in Chrome or Edge.

02

Right-click the page and choose Inspect.

03

Turn on the device toolbar.

04

Try several screen widths.

Drag the edge of the preview to see how the layout responds between device sizes.

12

What to check on mobile

01

All content fits within the screen.

02

The text is large enough to read.

03

Buttons are easy to tap.

04

Images keep their proportions.

05

Columns rearrange as intended.

06

The navigation is still easy to use.

What you’ve learned

  • A responsive website adapts to the screen size.
  • The viewport meta tag is essential on mobile.
  • Avoid fixed widths where flexibility is needed.
  • Use max-width: 100% to keep images within their containers.
  • Media queries apply styles at specific widths.
  • Headings should adapt to small screens.
  • Spacing can be reduced on small screens.
  • Grids can switch from several columns to one.
  • Navigation should remain easy to use.
  • clamp() lets you set fluid sizes.
  • Test your website at several widths.

Make your website responsive

Open the project you built in the previous lessons and adapt its CSS.

  1. Check that the viewport meta tag is present.
  2. Add max-width: 100% to images.
  3. Create a media query for widths of 768px or less.
  4. Reduce the h1 size on mobile.
  5. Reduce section padding.
  6. Switch multi-column layouts to one column.
  7. Adapt the navigation.
  8. Check the buttons.
  9. Test the page at several widths.
Test beyond your own phone. A responsive website should work naturally across many screen sizes.

PREVIOUS LESSON

← 06 · Style your page with CSS