Mastering CSS Media Queries: Syntax Explained

Mastering CSS Media Queries: Syntax and Beyond

In the realm of responsive web design, CSS media queries are indispensable tools that enable us to apply different styles for different media types/devices. They allow us to create flexible and adaptable designs that respond to the user's environment, be it a desktop browser, a tablet, or a smartphone. Let's delve into the syntax of CSS media queries and explore its intricacies.

Understanding the Basic Syntax

The fundamental structure of a CSS media query is as follows:

@media { }

Here, specifies the type of media the styles apply to, and contains the CSS rules that will be applied if the media query matches.

an image of a computer screen with the text css media queries simple explanation
an image of a computer screen with the text css media queries simple explanation

Media Types

CSS offers several predefined media types, including:

  • all: Applies to all media types.
  • print: Designed for printed media like newspapers and magazines.
  • screen: Intended for screens, such as computer monitors and mobile devices.

However, media queries often use media features instead of media types for more precise control.

Media Features and Expressions

Media features allow us to target specific characteristics of the user's device or environment. They can be used alone or combined using logical operators (and, not, only) to create complex media queries.

Master Media Queries: Quick CSS Tips for Responsive Design!
Master Media Queries: Quick CSS Tips for Responsive Design!

Common Media Features

Some of the most commonly used media features include:

  • width and height: Match the viewport's width or height.
  • aspect-ratio: Matches the aspect ratio of the viewport.
  • orientation: Matches the orientation of the device (portrait or landscape).
  • resolution: Matches the resolution of the device.

For example, to apply styles when the viewport's width is 600px or less, you would use:

@media (max-width: 600px) { /* CSS styles */ }

Media Query List

You can also combine multiple media queries into a single rule using commas, creating a media query list:

How to Use CSS Media Queries
How to Use CSS Media Queries

@media (max-width: 600px), print { /* CSS styles */ }

In this example, the styles will be applied when the viewport's width is 600px or less or when the document is being printed.

Min- and Max-Keywords

CSS media queries support min- and max-keywords for comparing media features to values:

Keyword Description
min- Matches if the media feature's value is greater than or equal to the given value.
max- Matches if the media feature's value is less than or equal to the given value.

For instance, to apply styles when the viewport's width is between 400px and 800px, you would use:

@media (min-width: 400px) and (max-width: 800px) { /* CSS styles */ }

This covers the essential aspects of CSS media queries' syntax. By mastering these concepts, you'll be well on your way to creating responsive and adaptive web designs that cater to a wide range of devices and environments.

CSS Cheatsheet – Complete Beginner to Advanced Quick Reference Guide
CSS Cheatsheet – Complete Beginner to Advanced Quick Reference Guide
CSS Syntax Simplified
CSS Syntax Simplified
Learn CSS Media Queries by Building Three Projects
Learn CSS Media Queries by Building Three Projects
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
CSS Syntax to Write CSS Rules - Scientech Easy
CSS Syntax to Write CSS Rules - Scientech Easy
CSS Notes for Beginners (Easy + Quick Guide) 🎨
CSS Notes for Beginners (Easy + Quick Guide) 🎨
a blue poster with the words css media queeny on it's side
a blue poster with the words css media queeny on it's side
Media query css for tablet | css media query
Media query css for tablet | css media query
CSS Media Query for Laptop.
CSS Media Query for Laptop.
the box model for css
the box model for css
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
CSS Responsive Layout | CSS Tutorial | CSS media query
CSS Responsive Layout | CSS Tutorial | CSS media query
Learn CSS Media Queries by Building Three Projects
Learn CSS Media Queries by Building Three Projects
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
CSS Media Query  for Desktop.
CSS Media Query for Desktop.
an info sheet showing the cost of mobile phones and their price ranges for each phone
an info sheet showing the cost of mobile phones and their price ranges for each phone
an image of different types of cats
an image of different types of cats
The CSS Cheat Sheet Every Web Designer Secretly Uses
The CSS Cheat Sheet Every Web Designer Secretly Uses
Media query css for mobile | css media query
Media query css for mobile | css media query
GitHub - segmentio/myth: A CSS preprocessor that acts like a polyfill for future versions of the spec.
GitHub - segmentio/myth: A CSS preprocessor that acts like a polyfill for future versions of the spec.
css cheat sheet
css cheat sheet
CSS Animations and Effects Cheat Sheet
CSS Animations and Effects Cheat Sheet