
Understanding Media Queries in CSS

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 high‑level look at media queries, how they work, and their role in mobile‑first versus desktop‑first 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 (
600pxand wider), the font size increases to18px. - On desktops (
1024pxand wider), the font size is bumped to20px.
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 mobile‑first and desktop‑first design comes in.
Mobile‑First
In a mobile‑first 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 desktop‑first 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. Mobile‑first 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 lower‑power devices, possibly using slower networks. Desktop‑first 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 mobile‑first development take priority simply because it is better for the end‑user, 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 mobile‑first or desktop‑first 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.
- Mobile‑first design focuses on smaller screens by default, whilst desktop‑first starts with larger screens.
By effectively leveraging media queries, developers can build websites that meet the needs of today's diverse users and devices.
Related Articles

Optimising Website Performance with HTML, CSS, and JavaScript. 
Container Queries in CSS. Container Queries in CSS

Advanced Techniques for Responsive Web Design. Advanced Techniques for Responsive Web Design

Using the filter() Method in JavaScript. Using the
filter()Method in JavaScript
The Value of Choosing a Web Developer Near You: Customised Solutions for Local Success. The Value of Choosing a Web Developer Near You: Customised Solutions for Local Success

Commenting in JSX. Commenting in JSX

Default Parameters in JavaScript: A Guide. Default Parameters in JavaScript: A Guide

Access CSS Variables from a Database via db‑connect. Access CSS Variables from a Database via
db‑connect
Preview Mode in Next.js with a Headless CMS. Preview Mode in Next.js with a Headless CMS

Class vs. Functional Components in React. Class vs. Functional Components in React

Understanding the Difference Between <b> and <strong>. Understanding the Difference Between
<b>and<strong>
Using CSS to Deal with Widows. Using CSS to Deal with Widows