CSS3 media queries are dead simple, in terms of their syntax. You’ve got an @media
directive, a media type (which you already know from good ol’ CSS 2, like screen
, print
, all
, etc.) and one or more media features (the characteristics we’re testing against). That’s it:
@media screen and (max-width:500px) {
}
There are some additional little syntax details, but this is basically all you need to know to actually make a media query work. Once you memorize this short, simple syntax and the various media features that are available, you could technically say that you know how to use media queries. But knowing how to use media queries effectively requires a whole lot more considerations than just where to put an @ or {.
Designing web layouts with media queries is a process. You need to keep them in mind from the very beginning and make decisions at several points about how to integrate them in ways that will make the most sense for your site. There are very few always-right or always-wrong answers. What type of media query set-up would work best will depend on your site, your users, and your own capabilities and experience. But I wanted to cover the pros and cons of some of the essential considerations that go into crafting robust media query-driven layouts. These considerations include whether to:
- Make the media queries embedded or external
- Overlap or stack your media queries
- Make mobile or desktop styles the default
- Use conditional comments or JavaScript to add support for IE 8 and earlier versions
This article is just as much for me as it is for you—it can be hard to keep track of all the different configuration variations you can use! Hopefully I’ll be able to make the pros and cons of the various approaches clearer so you can use this article to guide your decisions when you start a project involving media queries.
How to include your media queries: embedded vs. external
There are two ways to include media queries in your site: embed them within a style sheet or include them in a call to a separate, external sheet.