Favorites

Responsive Design Basics - HTML Tutorial

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

People visit websites on phones, tablets, laptops and huge desktop monitors. Responsive design means building pages that adapt to whatever screen they're viewed on, rather than making a separate site for each device.

The good news is that HTML is responsive by default: text wraps to fit the window. Most responsive problems come from things we add, such as fixed widths, large images and multi-column layouts. This section covers the key techniques for keeping your pages flexible.

1. Include the Viewport Tag

Every responsive page starts with this line in the head, which you met in The Head Element:

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

Without it, phones display your page as if it were on a desktop screen, then shrink it down. With it, the page is laid out at the real width of the device.

2. Use Flexible Sizes

Fixed widths, such as width: 960px, are the most common cause of pages that don't fit on a phone. Use flexible sizes instead:

  • Use max-width instead of width for containers, so they can shrink on small screens.
  • Use percentages or fr units for columns.
  • Use rem for font sizes and spacing.
.container {
  max-width: 70rem;
  margin-inline: auto;
  padding-inline: 1rem;
}

On a large screen, the content stops growing at 70rem and sits in the middle. On a small screen, it fills the width, with a little padding so the text doesn't touch the edges.

3. Make Images Flexible

Add this rule to every style sheet so images never overflow their container:

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

4. Adapt the Layout with Media Queries

A media query applies CSS only when a condition is true, such as the screen being at least a certain width:

@media (min-width: 48rem) {
  /* These rules only apply when the viewport is 48rem (768px) wide or more */
}

The recommended approach is mobile first: write your basic styles for small screens, then use min-width media queries to add layout as more space becomes available. Small-screen layouts are usually simpler, so this keeps your CSS shorter.

Here's the page layout from Grid Basics, rewritten mobile first:

Example: A Mobile-First Page Layout
/* Small screens: one column, everything stacked */
.page {
  display: grid;
  gap: 1rem;
  grid-template-areas:
    "header"
    "content"
    "sidebar"
    "footer";
}

.page > header { grid-area: header; }
.page > main   { grid-area: content; }
.page > aside  { grid-area: sidebar; }
.page > footer { grid-area: footer; }

/* Wider screens: put the sidebar next to the content */
@media (min-width: 48rem) {
  .page {
    grid-template-columns: 1fr 18rem;
    grid-template-areas:
      "header  header"
      "content sidebar"
      "footer  footer";
  }
}

On a phone, the sidebar sits below the main content. Once the screen is at least 48rem wide, it moves alongside. The width where the layout changes is called a breakpoint. Choose breakpoints based on where your content starts to look cramped or stretched, rather than trying to match particular devices.

5. Let Text Scale Smoothly

Large headings that look great on a desktop can be overwhelming on a phone. The clamp() function sets a size that grows with the screen width, but stays between a minimum and a maximum:

h1 {
  font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}

The three values are the minimum, the preferred size and the maximum. The vw unit is 1% of the viewport width, so the preferred size grows as the screen gets wider. Including a rem value in the middle keeps the text responsive to the visitor's own text size settings.

6. Design for Touch

On a touch screen, people tap with their fingers rather than click with a precise mouse pointer:

  • Make links and buttons large enough to tap easily, with some space between them. Around 44 by 44 pixels is a comfortable size for a tap target.
  • Don't hide important information behind hover effects, because touch screens don't have hover.

Testing Your Pages

You don't need a drawer full of devices to test responsive pages:

  • Drag the edge of your browser window to make it narrower and wider, and watch how the page reacts.
  • Use the device toolbar in your browser's Developer Tools to preview common phone and tablet sizes.
  • When you can, check the page on a real phone. Some things, such as how easy buttons are to tap, are hard to judge on a computer.

That's the end of the Page Layout chapter! You can now structure a page and make it work on any screen. In the next chapter, you'll learn how to collect information from your visitors with forms.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee