Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Introduction to Media Queries

Media queries are an essential component of modern CSS, allowing styles to be applied based on the characteristics of the user's device. They are the foundation of responsive design, adapting layout and styles to different screen sizes, orientations, or user preferences.

What media queries are and why they matter

Media queries are CSS instructions that test conditions such as screen width, device type, or orientation. If the condition is met, the styles inside the media query are applied.

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

This example applies a light gray background only if the screen width is at most 768px — ideal for mobile phones.

Their role in responsive design

Responsive design means the site automatically adapts to any screen size. Media queries allow:

Without media queries, a site would look the same on all devices — which can lead to a poor experience on mobile or tablet.

Difference between media types and media features

Media types define the general type of environment:

Media features are specific conditions tested within the media query:

@media screen and (min-width: 1024px) {
  .sidebar {
    display: block;
  }
}

Here, styles are applied only on screens (screen) with a width of at least 1024px.

Media Queries Syntax

General structure

Media queries use the @media directive followed by a condition and a block of styles. Basic syntax:

@media (condition) {
  /* Styles applied if the condition is true */
}

Simple and clear examples

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

@media (min-width: 1024px) {
  .container {
    padding: 40px;
  }
}

Keyword Explanation

@media only screen and (max-width: 600px) {
  .box {
    background: #eee;
  }
}

@media not screen and (min-width: 800px) {
  .sidebar {
    display: none;
  }
}
🔧 Live Example – Media Query with max-width

Media Types

Media types: screen, print, speech

Media types define the general environment in which CSS styles are applied. The most common ones are:

These can be combined with media features to apply conditional styles.

When and how to use them

Media types are used to adapt styles based on the display context:

@media print {
  nav, footer {
    display: none;
  }

  body {
    font-size: 12pt;
    color: black;
  }
}

Example: Different Styles for Print vs. Screen

In the example below, the background is blue on screen and white when printed:

@media screen {
  .box {
    background: #3498db;
    color: white;
  }
}

@media print {
  .box {
    background: white;
    color: black;
    border: 1px solid #000;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Frequently Used Media Features

Popular media features: max-width | min-width | orientation | prefers-color-scheme

Media features are specific conditions tested within a media query. The most commonly used ones include:

max-width vs. min-width

These two media features are fundamental for responsive design:

@media (max-width: 600px) {
  .nav {
    display: none;
  }
}

@media (min-width: 1024px) {
  .nav {
    display: flex;
  }
}

Example: adapting layout based on screen width

In the example below, the layout automatically adjusts depending on the screen size:

.grid {
  display: grid;
  grid-template-columns: 1fr;
}

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

@media (min-width: 1024px) {
  .grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Responsive Design with Breakpoints

What are breakpoints and how to choose them

Breakpoints are layout thresholds defined using media queries, allowing styles to adapt based on screen size. They mark transitions between different design versions: mobile, tablet, desktop, etc.

There's no universal set — breakpoints should be chosen based on the site's content and structure. Ideally, they're based on actual content dimensions, not just device types.

Common breakpoint examples

Here are some commonly used reference values:

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

@media (min-width: 1024px) {
  .nav {
    flex-direction: row;
  }
}

Strategies: mobile-first vs. desktop-first

Mobile-first means that the default styles are for small screens, and media queries progressively enhance the design for larger screens.

.card {
  font-size: 1rem;
}

@media (min-width: 768px) {
  .card {
    font-size: 1.2rem;
  }
}

Desktop-first assumes that the default styles are for desktop screens, and media queries scale them down for mobile devices.

.card {
  font-size: 1.2rem;
}

@media (max-width: 768px) {
  .card {
    font-size: 1rem;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Organizing Media Queries in CSS

Where to place them in the CSS file

Media queries can be placed:

/* Base styles */
.button {
  padding: 12px 24px;
  font-size: 1rem;
}

/* Media query at the end */
@media (max-width: 768px) {
  .button {
    font-size: 0.9rem;
  }
}

Logical grouping of styles

Organizing media queries by component or functionality helps maintain clean and scalable code:

/* Grouping by component */
.header { ... }
@media (max-width: 768px) {
  .header { ... }
}

.nav { ... }
@media (max-width: 768px) {
  .nav { ... }
}

Organizing Media Queries in CSS

Separating styles into external files

For large projects, it's recommended to separate styles into dedicated files:

<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/responsive.css">
<link rel="stylesheet" href="css/print.css" media="print">
🔧 Live Example – Different Styles for Print vs. Screen

Practical Examples

Layout that adapts on mobile

A simple layout with two columns on desktop that becomes a single column on mobile:

.layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Menu that transforms into a hamburger

A horizontal menu that becomes vertical and hidden on mobile, activated by a button:

.menu {
  display: flex;
  gap: 20px;
}

.menu-toggle {
  display: none;
}

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

  .menu-toggle {
    display: block;
  }

  .menu.active {
    display: flex;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Gallery that shifts from 1 to 4 columns based on screen size

A flexible gallery that automatically adapts to the screen width:

.gallery {
  display: grid;
  gap: 20px;
  grid-template-columns: 1fr;
}

@media (min-width: 600px) {
  .gallery {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 900px) {
  .gallery {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1200px) {
  .gallery {
    grid-template-columns: repeat(4, 1fr);
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Testing and Debugging

How to use DevTools to simulate devices

All modern browsers offer Developer Tools (DevTools) that allow you to simulate different screen sizes and devices. In Chrome, Firefox, and Edge, you can access responsive mode like this:

You can select predefined devices (iPhone, iPad, Galaxy, etc.) or enter custom dimensions.

Responsive modes in Chrome, Firefox, Edge

Each browser offers a slightly different experience:

In all cases, you can inspect elements, modify styles in real time, and test media queries directly in DevTools.

Detecting style issues

DevTools helps you quickly identify problems related to media queries:

You can even temporarily add media queries directly in DevTools for quick testing.

@media (max-width: 600px) {
  .debug {
    outline: 2px dashed red;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Common Mistakes and How to Avoid Them

Overusing media queries

One of the most frequent mistakes is oversaturating the CSS file with media queries for every tiny variation. This leads to:

Solution: Use well-chosen breakpoints and scalable styles with clamp(), flex, grid.

/* Avoid this */
@media (max-width: 500px) { ... }
@media (max-width: 520px) { ... }
@media (max-width: 540px) { ... }

/* Better */
@media (max-width: 600px) { ... }

Arbitrary breakpoints without testing

Defining breakpoints without testing the layout on real devices or simulations can lead to inconsistent experiences. For example, a breakpoint at 850px might be useless if it doesn't match any real device.

Solution: Test your layout in DevTools and base breakpoints on content, not assumptions.

Lack of a clear responsive design strategy

Without a consistent approach (mobile-first or desktop-first), styles can become contradictory and hard to scale.

Solution: Choose a strategy and apply it consistently. Recommended: mobile-first, with default styles for small screens and media queries for scaling up.

/* Mobile-first */
.card {
  font-size: 1rem;
}

@media (min-width: 768px) {
  .card {
    font-size: 1.2rem;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

Recap and Final Exercise

Key Concepts Recap

Exercise: Build a page with a layout that adapts to 3 screen sizes

The goal is to create a section with 3 boxes that display:

.grid {
  display: grid;
  gap: 20px;
  grid-template-columns: 1fr;
}

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

@media (min-width: 1024px) {
  .grid {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

Challenge: Add different styles for landscape orientation

Use a media query with orientation to dynamically change the style:

@media (orientation: landscape) {
  .grid {
    background: #f1c40f;
  }
}
🔧 Live Example – Different Styles for Print vs. Screen

🧠 Final Quiz – Media Queries Recap


Top