Skip to content

Media Query

In one line

A media query is a CSS feature that applies styles only when specific conditions about the device or viewport are met.

In simple words

Media queries let you write CSS that only applies under certain conditions — like when the screen is a particular width, the device is in landscape mode, or the user prefers dark mode. This is the foundation of responsive web design.

The most common use is targeting screen widths: @media (min-width: 768px) applies styles only on screens 768px or wider. You can combine conditions with and, use commas for "or", and check features like orientation, resolution, and color scheme.

A "mobile-first" approach writes base styles for mobile and adds enhancements with min-width queries. A "desktop-first" approach starts with desktop styles and uses max-width queries to adapt for smaller screens.

Example

css
/* Base styles (mobile) */
.menu { display: none; }

/* Tablet and up */
@media (min-width: 768px) {
  .menu { display: block; }
}

The menu is hidden on mobile and shown on screens 768px or wider — a mobile-first responsive pattern.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary