Skip to main content

Advanced HTML

HTML Responsive Design

Written by Updated HTML Responsive Design exercises

A responsive page works on any screen: a phone, a tablet, a laptop, a huge monitor. On the HTML side it takes one meta tag and images that can shrink. The rest is CSS: flexible widths, Flexbox or Grid, and media queries.

Without the viewport tag, phones pretend to be a desktop screen and shrink the whole page, so people have to pinch and zoom. With it, your CSS can adapt the layout to the real screen size. The full CSS side is covered in Responsive Layout.

Viewport Meta Tag

html

<meta name="viewport" content="width=device-width, initial-scale=1.0">
  • width=device-width makes the page as wide as the phone's screen, instead of a pretend 980px.
  • initial-scale=1.0 starts at normal zoom.

Every page you build should have this line in its <head>.

Responsive Layout with CSS

Using Percentages

html

<style>
  .container {
    width: 90%;
    margin: auto;
  }
</style>

A width in percent shrinks and grows with the screen. margin: auto centers the box. For long text, max-width is even better: the box can get narrower on phones, but never too wide on big screens.

Using Flexbox or Grid

html

<style>
  .flex-container {
    display: flex;
    flex-wrap: wrap;
  }
  .box {
    flex: 1 1 300px;
    margin: 10px;
    background: lightblue;
  }
</style>

<div class="flex-container">
  <div class="box">Box 1</div>
  <div class="box">Box 2</div>
  <div class="box">Box 3</div>
</div>

Each box wants to be 300px wide but can grow or shrink. When the screen is too narrow for two side by side, flex-wrap: wrap moves one to the next line. No media query needed.

CSS Media Queries

Media queries allow styles to change depending on device characteristics (like screen width).

html

<style>
  body {
    background-color: lightblue;
  }

  @media (max-width: 600px) {
    body {
      background-color: lightcoral;
    }
  }
</style>

When the screen is 600px wide or less, the background changes to light coral. Media queries let you change any style at any width. See CSS Media Queries.

Responsive Images

Using max-width

html

<style>
  img {
    max-width: 100%;
    height: auto;
  }
</style>

<img src="nature.jpg" alt="Beautiful Nature">

Images never get wider than their container, and height: auto keeps their proportions.

Using <picture> Element

html

<picture>
  <source srcset="large.jpg" media="(min-width: 800px)">
  <source srcset="small.jpg" media="(max-width: 799px)">
  <img src="default.jpg" alt="Responsive image">
</picture>

The browser picks the large image on wide screens and the small one on narrow screens. See the picture element.

Responsive Text

Text sizes in rem follow the reader's own text size setting. vw follows the screen width, but used alone it ignores that setting and can get far too small on phones; combine both with clamp(), as in font-size: clamp(1.5rem, 1rem + 3vw, 3rem).

Frameworks for Responsive Design

Frameworks like Bootstrap and Tailwind include ready-made responsive grids and classes:

html

<div class="container">
  <div class="row">
    <div class="col-md-6">Left</div>
    <div class="col-md-6">Right</div>
  </div>
</div>

That Bootstrap grid shows two columns side by side on medium screens and up, and stacks them on phones. Frameworks save time, but modern CSS Grid and Flexbox do the same with a few lines of your own.

Example: Complete Responsive Page

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Webpage Example</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }
    header, footer {
      background-color: #29AB87;
      color: white;
      text-align: center;
      padding: 15px;
    }
    .container {
      display: flex;
      flex-wrap: wrap;
      padding: 20px;
      gap: 10px;
    }
    .box {
      flex: 1 1 300px;
      background-color: #f4f4f4;
      padding: 20px;
      border-radius: 8px;
      text-align: center;
    }
    img {
      max-width: 100%;
      height: auto;
      border-radius: 6px;
    }
    @media (max-width: 768px) {
      header, footer {
        font-size: 16px;
        padding: 10px;
      }
    }
  </style>
</head>
<body>
  <header>
    <h2>HTML Responsive Design Example</h2>
  </header>

  <div class="container">
    <div class="box">
      <h3>Flexible Layout</h3>
      <p>The layout automatically adjusts to fit the screen size.</p>
    </div>
    <div class="box">
      <h3>Responsive Image</h3>
      <img src="https://picsum.photos/id/237/200/300" alt="Nature Image">
    </div>
    <div class="box">
      <h3>Media Queries</h3>
      <p>Media queries help to apply styles for different screen widths.</p>
    </div>
  </div>

  <footer>
    &copy; 2025 HTML5 & CSS3 — Responsive Design Demo
  </footer>
</body>
</html>

Resize the preview: the boxes wrap onto new lines, the image shrinks, and the layout keeps working at every width.

Checklist

  • The viewport meta tag is in the head.
  • No element has a fixed width wider than a phone, around 360px.
  • Images have max-width: 100%.
  • Text is readable without zooming, and buttons are big enough to tap.
  • Test on a real phone, not only by resizing a desktop window.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Responsive Design exercises

Fit the screen of a phone

Write a head with the meta tag viewport and the content width=device-width, initial-scale=1.

Result

Check your answer to see it here.