CSS Layout

Responsive Design

CSS Responsive Design is a design approach that uses flexible layouts, images, and CSS media queries to create web pages that adapt and display optimally across different devices and screen sizes.

What is responsive web design?

Responsive design makes one page adapt to every screen size by using fluid layouts, flexible images and media queries. Start with the mobile layout, add the viewport meta tag, size with relative units (%, rem, fr), and use @media (min-width: …) to change the layout as space increases.

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

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

.layout { display: grid; gap: 1rem; }
@media (min-width: 768px) {
  .layout { grid-template-columns: 250px 1fr; }
}

Interactive Responsive Demo

Code Editor

<div class="responsive-container">
  <div class="responsive-item">Box 1</div>
  <div class="responsive-item">Box 2</div>
  <div class="responsive-item">Box 3</div>
  <div class="responsive-item">Box 4</div>
</div>

Preview

Responsive Design Fundamentals

Responsive web design is an approach that ensures web pages render well on a variety of devices and window or screen sizes. It uses flexible layouts, flexible images, and CSS media queries.

Core Principles

Fluid Grids: Layouts that use relative units

Flexible Images: Images that scale with container

Media Queries: CSS rules for different screen sizes

Mobile First: Start with mobile design, scale up

Progressive Enhancement: Add features for larger screens

Content Priority: Most important content first

Viewport and Meta Tags

Essential Viewport Meta Tag

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

This meta tag is crucial for responsive design. It tells the browser how to control the page's dimensions and scaling.

width=device-width

Sets the width of the page to follow the screen-width of the device

initial-scale=1.0

Sets the initial zoom level when the page is first loaded

Flexible Layout Techniques

CSS Grid Auto-Fit

.grid {
  display: grid;
  grid-template-columns:
    repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
}

Automatically responsive grid that adapts to screen size

Flexbox Wrapping

.flex-container {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.flex-item {
  flex: 1 1 300px;
}

Flexible items that wrap to new lines as needed

Relative Units

width: 100%;
max-width: 1200px;
padding: 5vw;
font-size: 4vw;
margin: 2rem auto;

Use %, vw, vh, rem for scalable sizing

Clamp Function

font-size: clamp(1rem, 4vw, 3rem);
width: clamp(200px, 50%, 800px);
margin: clamp(1rem, 5vw, 4rem);

Set minimum, preferred, and maximum values

Aspect Ratio

.video {
  aspect-ratio: 16 / 9;
  width: 100%;
}

.square {
  aspect-ratio: 1;
}

Maintain proportions across screen sizes

Common Responsive Patterns

Sidebar Layout

.layout {
  display: grid;
  grid-template-columns: minmax(250px, 1fr) 3fr;
  gap: 2rem;
}

@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

Two-column layout that stacks on mobile devices.

Card Grid

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
  padding: 1rem;
}

.card {
  min-height: 200px;
  padding: 1.5rem;
}

Automatically responsive card layout with consistent sizing.

Holy Grail Layout

.holy-grail {
  display: grid;
  grid-template-areas:
    "header header header"
    "nav main aside"
    "footer footer footer";
  grid-template-columns: 200px 1fr 200px;
  min-height: 100vh;
}

@media (max-width: 768px) {
  .holy-grail {
    grid-template-areas:
      "header"
      "nav"
      "main"
      "aside"
      "footer";
    grid-template-columns: 1fr;
  }
}

Classic three-column layout that adapts to mobile by stacking.

Images and Media

Responsive Images

Basic Responsive Image

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

Object Fit

img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

Picture Element

<picture>
  <source media="(min-width: 768px)" srcset="large.jpg">
  <source media="(min-width: 480px)" srcset="medium.jpg">
  <img src="small.jpg" alt="Responsive image">
</picture>

Best Practices

Best Practices

  • • Start with mobile-first design
  • • Use relative units (rem, %, vw, vh)
  • • Test on real devices, not just browser tools
  • • Optimize images for different screen densities
  • • Consider touch targets and accessibility
  • • Use progressive enhancement

Performance Tips

  • • Use CSS containment for complex layouts
  • • Optimize critical rendering path
  • • Implement lazy loading for images
  • • Minimize layout shifts (CLS)
  • • Use modern image formats (WebP, AVIF)
  • • Consider container queries for components

Responsive Design — step by step

1 / 4
1

Fluid Layouts with % and vw

Use percentage widths and viewport units (vw, vh) instead of fixed pixels so elements naturally resize as the viewport changes.

1 of 4
💡Use minmax() for Automatic Columns
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) creates a fully responsive grid with no breakpoints — columns wrap automatically when they cannot fit at their minimum size.

🎯 Quick Check

Which CSS declaration makes an image responsive without distortion?

Responsive Design: summary

Key points

  • Without the viewport meta tag, mobile browsers render the page at about 980px wide and zoom out.
  • Mobile-first means the base CSS targets small screens, and min-width queries add complexity for larger ones.
  • max-width: 100% on images and videos stops them overflowing narrow screens.
  • Grid's auto-fit with minmax(), and clamp() for font sizes, often replace several media queries.
  • Container queries (@container) adapt a component to the space it's given, not to the whole viewport.

Common mistakes

  • Fixed pixel widths on containers, which cause horizontal scrolling on phones.
  • Testing only by resizing a desktop browser. Also test on a real phone or in DevTools device mode.

Frequently asked questions

›What breakpoints should I use?

Choose breakpoints where your layout breaks, not for specific devices. Common starting points are about 640px, 768px, 1024px and 1280px.

›What is mobile-first CSS?

Writing the default styles for small screens and adding @media (min-width: …) rules for larger ones. Phones then download only the simple base layout rules they need.

›Do I need a framework for responsive design?

No. Flexbox, Grid, relative units and a few media queries are enough for most sites. Frameworks like Tailwind only make writing them faster.