HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
5 Topics
Chapter•80 Minutes

Responsive Web Design

Build layouts that adapt to different viewport sizes, devices, input modes, and user preferences.

Responsive Design Principles

Responsive design means creating interfaces that adapt to available space and user conditions rather than designing separate fixed pages for every device.

Fluid layouts, flexible images, relative units, Grid, Flexbox, intrinsic sizing, and media queries work together to create resilient interfaces.

A good responsive design is not simply a desktop layout that becomes smaller. Content hierarchy and interaction patterns may need to change.

Responsive Design
Fluid Container
.container {
  width: min(100% - 2rem, 1200px);
  margin-inline: auto;
}
💡 The container remains fluid on small screens while respecting a maximum reading width on larger screens.
Key Takeaways
  • Responsive design adapts to constraints rather than specific device names.
  • Fluid sizing often reduces the need for many breakpoints.
Knowledge Check

What is the main goal of responsive design?

Media Queries

Media queries apply styles based on environmental conditions such as viewport width, orientation, resolution, color scheme, reduced motion preference, and input capabilities.

Modern responsive design often uses content-driven breakpoints instead of copying popular device widths.

Media Query Decision
Responsive Media Query
.navigation {
  display: flex;
}

@media (max-width: 700px) {
  .navigation {
    flex-direction: column;
  }
}
💡 The layout changes when the available viewport width becomes constrained.
Key Takeaways
  • Choose breakpoints based on content needs.
  • Media queries can test many environmental features beyond width.
Knowledge Check

What does a media query allow CSS to do?

Mobile-First CSS

Mobile-first CSS starts with the constrained layout and progressively enhances it for larger screens. This often results in simpler base styles and clearer media queries.

Mobile-first does not mean mobile devices are more important. It means the stylesheet starts with the smallest practical layout constraints.

Mobile-First Progression
Mobile-First Layout
.dashboard {
  display: grid;
  grid-template-columns: 1fr;
}

@media (min-width: 800px) {
  .dashboard {
    grid-template-columns: 240px 1fr;
  }
}
💡 The base layout works on narrow screens and the larger-screen layout is added progressively.
Key Takeaways
  • Start with a resilient constrained layout.
  • Use min-width queries for progressive enhancement.
Knowledge Check

What is the typical direction of mobile-first responsive CSS?

Container Queries

Container queries allow a component to respond to the size of its containing element rather than the viewport. This is particularly valuable for reusable components that can appear in different parts of an application.

A card can therefore change its layout based on the width of the card's container rather than the entire browser window.

Container Query vs Viewport
Container Query
.card-list {
  container-type: inline-size;
}

.card {
  display: grid;
  gap: 1rem;
}

@container (min-width: 500px) {
  .card {
    grid-template-columns: 160px 1fr;
  }
}
💡 The card changes layout when its containing component becomes wide enough.
Key Takeaways
  • Container queries make components more context-aware.
  • They reduce coupling between components and viewport breakpoints.
Knowledge Check

What is the major advantage of container queries?

Responsive Images and Object Fit

CSS controls how replaced elements such as images and videos fit within their boxes. object-fit and object-position are useful for cards, avatars, thumbnails, and media galleries.

Responsive images also involve HTML features such as srcset and sizes; CSS alone cannot solve every image-delivery problem.

object-fit: cover
Image Cropping
.avatar {
  width: 64px;
  aspect-ratio: 1;
  object-fit: cover;
  object-position: center;
  border-radius: 50%;
}
💡 object-fit: cover fills the box while preserving the image's aspect ratio and cropping excess content.
Key Takeaways
  • object-fit controls replaced-content fitting.
  • Responsive image delivery should also use appropriate HTML image attributes.
Knowledge Check

What does `object-fit: cover` do?