What Are Media Queries?

CSS3 has brought about a ton of fancy visual effects such as shadows and animations, but what about practical improvements? Is there anything about CSS3 that actually improves the way you can build websites from a usability standpoint?

The answer is a resounding “yes” and is due largely to the inclusion of media queries. Media queries are incredibly useful because they solve a huge problem that arose quite suddenly in web design: the need to design for vastly different screen sizes.

User screen sizes have always differed but for the most part this difference was limited to a few inches and you could bet on a huge majority of your audience fitting within parameters that were fairly easy to design around. These days though you have some users on a 27″ Apple cinema display and others on a 3.5″ smartphone.

That’s a huge difference and there are plenty of stops along the way. The ever-growing number of devices that are web friendly make it increasingly difficult for designers to present one static solution that caters to everyone’s needs.

This need has given birth to the idea of responsive web design, which goes far beyond fluid page widths and actually dramatically changes the layout of a page as the size of the browser window or device screen size changes. Media queries are one of the most powerful tools for meeting this goal because they allow the designer to set special CSS according to certain pre-established rules.

Owltastic: An Excellent Example of Responsive Web Design

The information above is a bit abstract, so let’s see how this works in the real world by looking at an example from the talented designer and developer Meagan Fisher. Here’s what her site looks like in my 13″ MacBook’s browser window at full screen.


Now, if I adjust that window size, a lot starts to happen. Columns adjust their width, the layout shifts to be more vertical and non-essential items disappear completely. Here is a screenshot with two more page widths, check out how much the design adapts to different situations.


Notice that we started with a three column layout, which then morphed into a two column layout and finally landed on a single column layout with my browser window as small as it would go. This is more than just one designer showing off to others, this represents a seriously impressive attempt to make the site not only usable, but specifically optimized to the broad range of devices that are likely to see it.

How It Works

This may seem like it’s going to be a ton of extra work, and I’d be lying if I said that it’s a painless process, but the good news is that CSS and media queries do a lot of the heavy lifting for you. Let’s jump into Owltastic’s code a little to see how this all works.

For starters, you’re going to see a lot of expert use of floats and percentages used for sizing. Notice in the snippet below Meagan used percentages for both the width of the element and the margin. She’s also meticulous about telling you where these percentages come from in the form of a comment with some quick math.

.navigation li {
  float: left;
  display: block;
  width: 16.24%; /* 105/645 */
  margin-right: 4.7%; /* 30.315/645 */