Homeglossary

Media Query

Media query is a CSS rule that applies styles when a browser, device, or user preference meets defined conditions. It helps a website or web app adapt its presentation for different screen sizes, print output, interaction methods, and accessibility preferences. Rather than identifying a specific device, modern media queries respond to the capabilities and needs of the current browsing environment.

Build responsive AI apps with Emergent.

What Is a Media Query in CSS?

A media query is a CSS rule that applies styles only when the browsing environment meets a stated condition. It lets a website or web app change its presentation for screen size, printing, color preference, motion preference, or input method, without changing its data or business logic. The CSS @media rule evaluates features of the current environment rather than trying to identify a named device.

How Media Queries Work

Media queries add conditional styling on top of a flexible base layout.

  1. Create default styles that work well on a small or narrow screen.
  2. Write an @media condition, such as @media (min-width: 48rem) {.nav { display: flex; } }.
  3. When the viewport reaches 48rem or wider, the matching rules become active.
  4. Let the CSS cascade resolve which matching rule takes priority. min-width adds styles as space grows, while max-width limits styles below a threshold.

Common Media Query Conditions

Useful conditions describe space, user settings, and interaction capabilities.

ConditionPractical use
widthChange a page layout when content no longer fits comfortably.
orientationAdjust a compact view in landscape or portrait.
prefers-color-schemeOffer a light or dark color theme.
prefers-reduced-motionReduce nonessential animation for people who request it.
hover or pointerAvoid hover-only controls and adapt targets for touch input.
printHide navigation and format pages for printing.

Benefits of Media Queries

They support responsive design while keeping presentation rules in CSS.

  • Improve readability as available space changes.
  • Make navigation and controls easier to use on touch screens.
  • Support accessible motion and color preferences.
  • Create print-friendly versions from the same codebase.
  • Use CSS breakpoints where a layout needs adjustment.

Practical Limits of Media Queries

Media queries are valuable, but they are not a complete layout strategy.

  • Too many breakpoints can make CSS fragile and hard to maintain.
  • Viewport width does not reveal the space available to an individual component.
  • They cannot replace semantic HTML, flexible grids, or real-device testing.
  • Less common features need progressive enhancement for older browsers.

Media Queries vs. Container Queries

These tools answer different questions about available space.

ToolBest use
Media queryPage-wide conditions, such as viewport size, print mode, and user preferences.
Container queryReusable components that must adapt to the size of their parent container.

Best Practices for Responsive and Accessible CSS

Choose conditions based on content and people, not device labels.

  • Start mobile-first and add layout changes only when content fails.
  • Use flexible sizing, wrapping, and grids before adding a breakpoint.
  • Respect reduced-motion and color-scheme preferences.
  • Test keyboard access, browser zoom, and intermediate widths.

Using Media Queries in Emergent Projects

When building with an AI app builder or AI website builder, describe expected responsive behavior, touch interactions, and accessibility preferences in the requirements. Emergent can help create full-stack interfaces, but teams should still review generated screens across sizes and interaction modes as part of app design and development. Build responsive AI apps with Emergent.

Start Building
on Emergent today
Start Building