Course contentsShow
← Web Development
Lesson 390 of 2,9827. Responsive DesignFree lesson

Media Query Syntax and Structure

Learn the @media rule syntax, media types, and how to write basic media queries for responsive layouts.

Media Query Syntax and Structure

You'll learn how to write the @media rule that adapts your CSS to different screen sizes and conditions.

Now that you understand mobile-first philosophy and base styles, you need a way to apply different CSS rules when the screen size changes. That's exactly what media queries do—they let you say "use these styles only when certain conditions are met."

The Basic Syntax

A media query uses the @media rule followed by conditions in parentheses, then a block of CSS:

@media (min-width: 768px) {
  /* CSS rules that apply only when viewport is 768px or wider */
  .container {
    max-width: 720px;
  }
}

Media Types

You can target different output devices (though screen is most common):

@media screen and (min-width: 600px) {
  /* For screens 600px and up */
}

@media print {
  /* For printed pages */
}

If you omit the media type, all is assumed (works everywhere).

Common Conditions

  • min-width: Styles apply when viewport is at least this wide (mobile-first approach)
  • max-width: Styles apply when viewport is at most this wide
  • and: Combine multiple conditions
@media (min-width: 600px) and (max-width: 900px) {
  /* Only for viewports between 600px and 900px */
}

Think of media queries like conditional statements: "If the screen is this size, then apply these styles."

Key Takeaway: Media queries use @media followed by conditions like (min-width: 768px) to apply CSS rules only when those conditions match, enabling responsive layouts that adapt to different screen sizes.