CSS: responsive design for phones and computers

Lesson 8 of 17 · HTML, CSS and JavaScript

More than half of all visitors open websites on a phone. A responsive site adapts itself to the width of the screen: on a phone everything is in one column, on a computer in several. This lesson covers the viewport, flexible sizes, media queries and the "phone first" approach.

Before you start: required prior knowledge – flexbox and grid.
How to learn: do the tasks at the end on your own in your editor first; open the hint and the solution only after that. Every example has the code and a live result below it; type the code into your own file and change it.

Step 1: the viewport

Without this line a phone shows the page as if the screen were 980px wide and shrinks everything – the text becomes tiny:

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

It must be in the head of every page.

Step 2: flexible sizes

Instead ofUseWhy
width: 900pxmax-width: 900px; width: 100%it shrinks on a narrower screen
images with a fixed widthimg { max-width: 100%; height: auto; }an image never goes off the screen
font sizes in px everywhererem, and for headings clamp(1.5rem, 4vw, 2.5rem)the text adapts
a fixed number of columnsflex-wrap or repeat(auto-fill, minmax(...))the columns rearrange themselves

Step 3: media queries

A media query is a set of CSS rules that apply only under a certain condition, most often for a certain screen width:

<style>
  .columns { display: flex; flex-direction: column; gap: 8px; }   /* phone: one column */
  .columns div { background: #eef6fc; padding: 14px; border-radius: 8px; }
  .message::after { content: "a narrow screen (phone)"; }

  @media (min-width: 600px) {          /* applies when the width is 600px or more */
    .columns { flex-direction: row; }  /* tablet and computer: columns in a row */
    .columns div { flex: 1; }
    .message::after { content: "a wide screen (≥ 600px)"; }
  }
</style>
<p class="message">Right now it is: </p>
<div class="columns">
  <div>First column</div>
  <div>Second column</div>
  <div>Third column</div>
</div>

Result:

The result frame is narrower than 600px on a phone and wider on a computer – open this page on both devices or make the window narrower. In F12 there is a Toggle device toolbar button (Ctrl+Shift+M) that shows the page as it looks on a chosen phone.

Common breakpointsDevice
up to 599pxphone
600px – 1023pxtablet, small laptop
1024px and morecomputer

The breakpoints are not a law – they are placed where the layout "breaks" as you make the window narrower.

Phone first (mobile first)

The recommended way of writing: the basic rules are for the phone (simple, one column), and @media (min-width: ...) adds a more complex layout for larger screens. That way the CSS is shorter and the page is faster on a phone.

/* the basics: phone */
.page { display: grid; gap: 12px; }
nav ul { display: flex; flex-direction: column; }

/* tablet and larger */
@media (min-width: 700px) {
  nav ul { flex-direction: row; gap: 16px; }
}

/* computer */
@media (min-width: 1024px) {
  .page { grid-template-columns: 220px 1fr; }   /* menu on the left, content on the right */
}

A few more rules for a pleasant site

  • Buttons and links for a finger – at least 44×44px; small links next to each other are hard to hit.
  • Line length – 60–80 characters are the easiest to read: main { max-width: 70ch; }.
  • Dark theme – @media (prefers-color-scheme: dark) { ... } respects the device's setting.
  • Wide tables – put them in a div with overflow-x: auto so that they scroll horizontally instead of breaking the page.

Ready-made libraries such as Bootstrap have ready classes for a responsive grid and components. They are used in the Logo creator project, but it is important that you first understand how all of this works with plain CSS.

Practice tasks

Each task has an example, a hint and a solution. The solutions have been checked in the browser.

TASK 01Difficulty: harder

A responsive home page

Make a page with a header (logo + menu), three cards and a footer. On a phone: the menu below the logo, the cards one below the other. From 700px: the menu to the right of the logo, the cards in a row.

Common mistakes and tips
  • Without meta viewport, media queries do not work as you expect on a phone.
  • Check the page in device mode (Ctrl+Shift+M) while you write, not only at the end.
  • Do not hide content on a phone just because it does not fit – rearrange it.
  • Images without max-width: 100% go off the screen and make the whole page scroll horizontally.

Check your knowledge

Open the quiz

Responsive design

1. What does @media (min-width: 600px) { ... } do?




2. Which line must be in the head for responsive design to work on a phone?




3. What does "mobile first" mean?



4. How do you stop an image from being wider than the screen?