Understanding Media Queries in CSS

Hero image for Understanding Media Queries in CSS. Image by Emily Morter.
Hero image for 'Understanding Media Queries in CSS.' Image by Emily Morter.

As web development continues to evolve, creating websites that adapt to different screen sizes and devices has become essential. Media queries, introduced in CSS3, are a cornerstone of responsive web design. They enable developers to tailor styles based on the characteristics of the user's device or viewport. In this article, I intend to take a highlevel look at media queries, how they work, and their role in mobilefirst versus desktopfirst design strategies.


What are Media Queries?

Media queries are conditional rules in CSS that apply styles only if specific criteria are met, such as screen width, height, or orientation. They allow us to adapt our website's layout and appearance to different devices without needing separate codebases.

The basic syntax of a media query looks something like this:

@media (condition) {  /* Styles to apply if the condition is true */}

For example, we could use a media query to apply styles to screens that are wider than 768 pixels like this:

@media (min-width: 768px) {  body {    background-color: lightblue;  }}

Here, a light blue background will only be applied to body if the viewport width is 768 pixels or more.


How Media Queries are Used

Media queries are most commonly used to create, responsive designs and layouts. They enable us as developers to adjust styles for different screen sizes, which ensures an optimal experience for our users, regardless of the device (or screen size) they are using.

Here's a simple example of a responsive layout:

/* Default styles */body {  font-size: 16px;}/* Adjust for tablets */@media (min-width: 600px) {  body {    font-size: 18px;  }}/* Adjust for desktops */@media (min-width: 1024px) {  body {    font-size: 20px;  }}

In this example:

  • On small screens, the font size remains 16px.
  • On tablets (600px and wider), the font size increases to 18px.
  • On desktops (1024px and wider), the font size is bumped to 20px.

Of course, there are any number of different ways to implement responsive typography, but as an example of media queries, this hopefully demonstrates how their flexibility allows us to build responsive designs.


Mobile‑First vs. Desktop‑First

When working with media queries, the order and intent of your styles and how they are applied matter. This is where the debate between mobilefirst and desktopfirst design comes in.

Mobile‑First

In a mobilefirst approach, the default styles are optimised for smaller screens. Media queries are then used to progressively enhance the design for larger screens.

For example:

/* Default styles for mobile */body {  font-size: 16px;}/* Larger screens */@media (min-width: 768px) {  body {    font-size: 18px;  }}

Desktop‑First

In a desktopfirst approach, the default styles target larger screens. Media queries are then used to modify the design downwards for smaller devices. For example:

/* Default styles for desktop */body {  font-size: 20px;}/* Smaller screens */@media (max-width: 768px) {  body {    font-size: 16px;  }}

Comparing the Two

Both approaches have merits, and the issue essentially boils down to progressive enhancement vs. graceful degradation principles. Mobilefirst approaches align with the modern web's emphasis on accessibility and performance. It ensures that the most lightweight styles are loaded first on what are likely to be lowerpower devices, possibly using slower networks. Desktopfirst approaches, on the other hand, may be more intuitive for projects prioritising desktop users.

This is a debate that has been rumbling along for a long time, but I fully expect (and strongly advocate) that we will continue to see mobilefirst development take priority simply because it is better for the enduser, which means it will as a result be better for the website owners and our clients too.


Wrapping up

Media queries are an essential tool for responsive web design. They allow developers to create styles that adapt seamlessly to various devices and screen sizes. Whether you choose a mobilefirst or desktopfirst approach, the key is to ensure your design provides a consistent and enjoyable experience for users across all platforms.

Key Takeaways

  • Media queries apply styles conditionally based on device characteristics like screen width or orientation.
  • They are integral to responsive design, helping websites adapt to different devices.
  • Mobilefirst design focuses on smaller screens by default, whilst desktopfirst starts with larger screens.

By effectively leveraging media queries, developers can build websites that meet the needs of today's diverse users and devices.


Categories:

  1. CSS
  2. Development
  3. Front‑End Development
  4. Guides
  5. Responsive Development