Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 6: Responsive Web Design & Projects
CSS ⏱ 12 min read

Media Queries & Breakpoints

Media queries are the engine of responsive design, allowing you to trigger different CSS styles at specific screen sizes.

What is it?

The `@media` at-rule applies CSS rules only when specified device conditions (such as screen width, orientation, or dark mode preference) evaluate to true.

Why is it used?

Enables multi-column desktop layouts to collapse into single-column mobile layouts, transforms desktop navbars into mobile drawer menus, and customizes spacing.

Detailed Explanation

Mobile-First Methodology: Write your base CSS for mobile phones first. Then use `min-width` media queries to progressively enhance the layout as screens get wider. Mobile-first CSS is cleaner, faster, and easier to maintain. Standard Breakpoint Reference: • Small phones: `< 640px` (base mobile styles) • Tablets: `@media (min-width: 768px)` • Laptops / Desktops: `@media (min-width: 1024px)` • Large screens: `@media (min-width: 1280px)`

Syntax Structure

Syntax Blueprint
/* Mobile-first base styles (default) */
.sidebar {
  display: none;
}

/* Tablet and Desktop enhancement */
@media (min-width: 768px) {
  .sidebar {
    display: block;
    width: 280px;
  }
}

Code Example

CSS Code
/* Mobile-First Layout: Stacks vertically on phone */
.tutorial-layout {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.sidebar {
  display: none; /* Hidden on phones; toggled by mobile menu */
}

.main-content {
  width: 100%;
}

/* Desktop Breakpoint (1024px and wider) */
@media (min-width: 1024px) {
  .tutorial-layout {
    flex-direction: row; /* Horizontal split on desktop */
    gap: 32px;
  }

  .sidebar {
    display: block;    /* Always visible on desktop */
    flex: 0 0 300px;
    position: sticky;
    top: 20px;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
  }

  .main-content {
    flex: 1;
    min-width: 0;      /* Prevents flex child overflow */
  }
}

Explanation of the Code

• On mobile: Sidebar is hidden or off-canvas; main content takes 100% width. • On desktop (`min-width: 1024px`): Layout transitions into a two-column desktop learning workspace with sticky sidebar navigation.

Expected Output

Single column on phones, expanding to a dual-pane sidebar + content workspace on desktop screens.

Real-World Application

This exact responsive pattern powers CodeWithHarry and documentation platforms like Next.js and Tailwind.

Common Mistakes to Avoid

• Creating dozens of arbitrary breakpoints (`@media (min-width: 537px)`). Stick to 3-4 standardized breakpoints. • Writing desktop styles first with `max-width` hacks instead of clean mobile-first `min-width` rules.

Quick Practice Challenge

Write a media query that changes a 1-column grid into 3 columns on screens wider than 768px.

Key Takeaways

• Follow mobile-first design using `min-width` media queries. • Common breakpoints: 640px, 768px, 1024px, 1280px. • Use sticky positioning for desktop sidebars to keep navigation easily accessible.