CSS Responsive Design and Media Queries

Build flexible layouts and use media queries to adapt to screen sizes and user preferences.

Hand-drawn responsive design illustration showing one website adapting across desktop, laptop, tablet, and mobile screens

Today, people browse websites using many different devices, including smartphones, tablets, laptops, and desktop computers. Each device has a different screen size, so a website that looks perfect on a desktop may look broken on a mobile phone.

CSS Responsive Design is a technique used to make websites adapt automatically to different screen sizes and devices. A responsive website provides a good user experience regardless of how it is viewed.

Modern websites rely heavily on responsive design because a large percentage of internet users access websites from mobile devices.

Why Is Responsive Design Important?

Responsive design helps:

  • Improve user experience
  • Support mobile, tablet, and desktop devices
  • Reduce the need for separate mobile websites
  • Increase accessibility
  • Improve search engine rankings

For example, an online shopping website should display products neatly on both a large desktop screen and a small smartphone screen.

How Responsive Design Works

Responsive design uses flexible layouts, scalable images, and CSS techniques that adjust content based on the available screen space.

Instead of creating separate websites for different devices, developers create one design that adapts automatically.

Using the Viewport Meta Tag

A responsive website should include the viewport meta tag inside the HTML document.

Example

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

This tells the browser to adjust the page width according to the device screen size.

Without this tag, mobile devices may display the page incorrectly.

Using Flexible Widths

Instead of fixed widths, responsive designs often use percentages.

Example

CSS
.container {
  width: 100%;
}

This allows the container to expand or shrink based on the screen size.

Flexible layouts adapt much better than fixed-width layouts.

Responsive Images

Images should also adjust to different screen sizes.

Example

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

This ensures that images never become larger than their container.

As the screen size changes, the image scales proportionally.

Mobile-First Design

Many developers use a mobile-first approach.

This means:

  1. Design for mobile devices first.
  2. Add enhancements for larger screens later.

Example

CSS
.card {
  width: 100%;
}

@media (min-width: 768px) {
  .card {
    width: 50%;
  }
}

The layout is optimized for smaller screens before expanding for larger devices.

Flexible Layouts with Flexbox

Flexbox is commonly used in responsive design.

Example

CSS
.container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

Items automatically move to new rows when there is not enough space.

This helps layouts remain organized on different screen sizes.

Using CSS Grid for Responsiveness

Grid Layout can also create responsive designs.

Example

CSS
.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

The number of columns adjusts automatically based on the available space.

What Is a Media Query?

A Media Query checks a device condition and applies CSS rules only when that condition is true.

Basic Syntax

CSS
@media (max-width: 768px) {
  body {
    background-color: lightblue;
  }
}

In this example:

  • The styles inside the Media Query are applied only when the screen width is 768 pixels or less.
  • On larger screens, the styles are ignored.

Common Breakpoints

Breakpoints are screen sizes where the layout changes.

Some commonly used breakpoints include:

  • Mobile: 480px and below
  • Tablet: 768px and below
  • Laptop: 1024px and below
  • Desktop: Above 1024px

These are example sizes, not fixed device categories. Choose breakpoints where your content needs a layout change.

Changing Layouts with Media Queries

Media Queries can modify layouts for smaller screens.

Example

CSS
.container {
  display: flex;
}

@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

On larger screens, items appear side by side.

On smaller screens, items stack vertically.

Adjusting Font Sizes

Text should be easy to read on all devices.

Example

CSS
h1 {
  font-size: 40px;
}

@media (max-width: 600px) {
  h1 {
    font-size: 28px;
  }
}

The heading becomes smaller on mobile devices, preventing overflow issues.

Hiding Elements

Media Queries can show or hide content based on screen size.

Example

CSS
@media (max-width: 768px) {
  .sidebar {
    display: none;
  }
}

This hides the sidebar on smaller screens to create more space for the main content.

Using Orientation Queries

Media Queries can also detect device orientation.

Example

CSS
@media (orientation: landscape) {
  body {
    background-color: lightgray;
  }
}

This style applies when the device is in landscape mode.

Key Takeaways

  • Combine flexible layouts and images with media queries.
  • Start with mobile styles and add enhancements for larger viewports.
  • Choose breakpoints where your content needs more space and test at different widths.