← Learning Hub
Beginner

Responsive Design

A responsive page adapts to any screen, from a small phone to a wide monitor. Most people browse the web on phones, so this matters a lot.

  • Add <meta name="viewport" content="width=device-width, initial-scale=1"> in the head so phones do not shrink the page.
  • Use flexible widths such as % or max-width instead of fixed pixel widths.
  • Use media queries to change styles at certain screen widths.

In India, most people visit websites on their phones. A page designed only for a laptop will be tiny and hard to use on a phone, and Google ranks mobile-friendly pages higher. Responsive design means one website that looks good everywhere - no separate "mobile site" needed.

A media query is a condition: "if the screen is at least 768px wide, use these extra styles". Common breakpoints are around 600px (large phones), 768px (tablets) and 1024px (laptops), but the best breakpoints are where your design starts to look wrong. Test by resizing the browser or using Chrome's device mode (F12, then the phone icon).

Example
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
      .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
      .grid div { background: #dcfce7; padding: 24px; text-align: center; border-radius: 8px; }
      img { max-width: 100%; height: auto; }

      /* On screens 500px wide or less, use one column */
      @media (max-width: 500px) {
        .grid { grid-template-columns: 1fr; }
      }
    </style>
  </head>
  <body>
    <div class="grid"><div>A</div><div>B</div><div>C</div></div>
    <p>Make the preview narrow to see one column.</p>
  </body>
</html>
▶ Try it yourself
A popular approach is mobile first: write styles for phones, then add media queries with min-width for bigger screens.
Key points
  • Always add the viewport meta tag.
  • Use flexible widths (%, max-width) and flexbox/grid.
  • Media queries change styles at certain screen widths.
  • Design for phones first, then add styles for bigger screens.

📝 Test yourself

1. What does a media query do?
2. Which keeps images inside small screens?
3. Complete the media query rule:
@ (max-width: 600px) { ... }