Organizing Media Queries in CSS
You'll learn: Two practical strategies for organizing media queries to keep your CSS maintainable as your project grows.
The Core Challenge
As you add responsive behavior to your stylesheets, you'll need to decide where to place your media queries. There are two main approaches, and each has trade-offs worth understanding at this beginner stage.
Strategy 1: Inline with Selectors
Write media queries immediately after the original rule they modify:
.header {
padding: 10px;
font-size: 14px;
}
@media (min-width: 768px) {
.header {
padding: 20px;
font-size: 18px;
}
}
Pros: All related styles stay together—easy to update a component without hunting through files.
Cons: Media queries repeat throughout your CSS, making global breakpoint changes tedious.
Strategy 2: Grouped at the End
Write all base styles first, then gather all media queries at the bottom:
.header {
padding: 10px;
font-size: 14px;
}
.navigation {
display: block;
}
/* All tablet styles */
@media (min-width: 768px) {
.header {
padding: 20px;
font-size: 18px;
}
.navigation {
display: flex;
}
}
Pros: See all responsive changes for a breakpoint in one place—easier to manage device-specific layouts.
Cons: You'll jump between sections when editing a single component.
Which Should You Choose?
For small projects, either works. Many developers prefer the inline approach for component-focused work, as it matches how you think about individual elements. The grouped approach shines when managing site-wide breakpoint strategies.
Key Takeaway: Media queries can be organized inline with their selectors (component-focused) or grouped by breakpoint at the end of your file (device-focused)—choose based on what makes your specific project easier to maintain.