Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials CSS Media Queries
CSS Beginner FREE

Media Queries

Lesson 1 of 16 Beginner Interactive

Media queries let you apply CSS rules based on device characteristics like screen width, height, and orientation. They are essential for responsive design.

Syntax

@media (condition) { CSS rules }

Common Breakpoints

  • Mobile — up to 768px
  • Tablet — 768px to 1024px
  • Desktop — 1024px and up

Mobile-First Approach

Start with styles for mobile devices, then add min-width media queries to enhance for larger screens. This is the recommended approach.

  • @media (min-width: 768px) — applies when viewport is 768px or wider
  • @media (max-width: 768px) — applies when viewport is 768px or narrower

Syntax

CSS
/* Mobile-first (base styles for mobile) */
.container {
    padding: 16px;
}

/* Tablet */
@media (min-width: 768px) {
    .container {
        padding: 24px;
        max-width: 720px;
        margin: 0 auto;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    .container {
        max-width: 960px;
        padding: 32px;
    }
}

/* Orientation */
@media (landscape) {
    .sidebar { display: block; }
}
Responsive Media Queries
HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Responsive Media Queries</title>
    <style>
        body { font-family: Arial, sans-serif; }
        .row { display: flex; flex-wrap: wrap; gap: 10px; }
        .card { width: 100%; background: #e2e8f0; padding: 16px; box-sizing: border-box; }
        @media (min-width: 640px) {  /* tablet */
            .card { width: 50%; }
        }
        @media (min-width: 1024px) { /* desktop */
            .card { width: 33.333%; }
        }
        @media print {
            .card { background: #fff; }
        }
    </style>
</head>
<body>
    <div class="row">
        <div class="card">Resize the preview panel to see the breakpoints.</div>
        <div class="card">640px and above -> width: 50%.</div>
        <div class="card">1024px and above -> width: 33.333%.</div>
    </div>
</body>
</html>

Practice

1
Exercise

Create a media query that changes the background to light gray on screens smaller than 600px.

Answer
@media (max-width: 600px) {
    body {
        background-color: #f5f5f5;
    }
}
2
Exercise

Write a mobile-first layout: 1 column on mobile, 2 columns on tablet, 3 on desktop.

Answer
.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: repeat(3, 1fr); } }

Quick Quiz

1

What does @media (min-width: 768px) do?

min-width means the media query applies when the viewport is at least 768px wide.

2

Why is mobile-first approach recommended?

Mobile-first starts with base styles and progressively enhances, leading to cleaner and more maintainable code.

Interview Questions

Common breakpoints: mobile (< 768px), tablet (768px - 1024px), desktop (> 1024px). However, content should drive breakpoints, not devices. Use content-based breakpoints where the layout needs to change.

min-width applies styles when the viewport is at least the specified width (used for mobile-first). max-width applies styles when the viewport is at most the specified width (used for desktop-first). min-width is generally preferred for progressive enhancement.