With the growing demand of mobile devices we must now take into consideration what the user is viewing a website with, and handle these situations appropriately. CSS in particular has some pretty handy methods to assist with this through media queries. You may have seen “@media” before, but did you know you can add even more goodness to it with a statement like this:
@media only screen and (max-width:500px){
/* ... */
}
What does this do exactly? This block of CSS will become active ONLY when the screen width is less than or equal to 500 pixels. Ok, so what does this do for me?
Lets say you have a two column layout. For larger screens this is fine, but how about on mobile devices, or when someone has a very small browser window. It would be nice to somehow convert this into a one column design. You’re now thinking, “Yeah, I’ve done that before with a mobile stylesheet with “@media handheld”. I’m going to take it one step further.
Lets say when the screen is smaller than 500px we slide the navigation out of the way, leaving only a handle. Then on hover of focus we’ll let it re-appear. We’ll also sprinkle in some CSS3 transitions to make it pretty. Lets start with some html.
<!DOCTYPE html>
<html>
<head>
<title>Test CSS Media Query</title>
<link rel="stylesheet" href="screen.css" />
</head>
<body>
<div id="wrapper">
<div id="nav">
<ul>
<li><a href="#" title="Link">Link to Home</a></li>
<li><a href="#" title="Link">Link to There</a></li>
<li><a href="#" title="Link">Link to Here</a></li>
<li><a href="#" title="Link">Link to This</a></li>
</ul>
</div>
<div id="content">
<h1>This is the Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</body>
</html>
Now we pour on the CSS. Before you dive in and get confused with the @media part, lets start above that. We have a standard 2 column layout with #nav and #content. #nav simply floats left, and #content gives it room with margin-left.
Then the fun part with the media query with the viewport width condition. We slide the #nav out of the way and reduce the #content margin. (this gives the one column, but leaves a little handle for the #nav) When the #nav handle is hovered we adjust the margin and overlay the #content. And for added fun I threw in some transition effects, but don’t get caught up on those as they are just eye candy.
@charset "UTF-8";
/*
* Mass Reset
*/
*{ margin: 0; padding: 0; border: 0; }
body{ background-color: #f4fff4; font: normal 1em/1.5em Arial, Serif; }
/*
* Normal layout
*/
#wrapper{ }
#wrapper #nav{ width: 160px; float: left; background-color: #eeeeee; border-right: solid 20px #aaa; height: 100%; position: fixed; z-index: 1000; }
#wrapper #content{ margin-left: 180px; padding: 10px 20px; position: relative; z-index: 100; }
#wrapper #nav ul{ list-style: none; }
#wrapper #nav ul li a{ display: block; padding: 8px 5px 8px 10px; border-bottom: solid 1px #aaa; color: #548f54; text-decoration: none; }
#wrapper #content h1,
#wrapper #content p{ margin: 5px 0; }
/*
* When screen is smaller than 500px
*/
@media only screen and (max-width:500px){
#wrapper #nav{ margin: 0 0 0 -160px; }
#wrapper #content{ margin-left: 20px; }
#wrapper #nav:hover,
#wrapper #nav:focus{
margin: 0 -160px 0 0;
-moz-transition-property: margin;
-moz-transition-duration: 1s;
-moz-transition-timing-function: ease-out;
-webkit-transition-property: margin;
-webkit-transition-duration: 1s;
-o-transition-property: margin;
-o-transition-duration: 1s;
}
}
Now, when our browser shrinks our navigation will hide. On hover or focus the left nav will elegantly slide out (currently in webkit, other browsers will have transition effects eventually, but should still work for now).
This is not a complete, nor robust example. It simply shows how they work, and how you could be creative and show/hide your navigation. Here is a pretty detailed reference for different media queries for iPhone, iPad, etc.
I have also provided the example here: Media Query Example




9 Responses
JB
Mar 11, 2011This works awesome As Is on my blackberry torch! The screen isn’t wide enough ever for the two column layout, but the hide/show navigation works flawlessly!
trent
Mar 11, 2011I tested this particular example on iPad, and iPad scales the entire page to fit. However instead of doing max-width you could do (orientation:portrait) or (orientation:landscape)
Sales Training
Mar 15, 2011Very helpful post. Ive bookmarked this site. thank you.
Kalem Tayeb
Sep 20, 2011Does the zooming level affect the queries ?
Kalem Tayeb
Sep 20, 2011i would like to ask about applying this technique in android version 1.5 ?
Kalem Tayeb
Sep 20, 2011i would like to ask about applying this technique in android version 1.5 ? i mean is’t possible to use media queries with android before 1.6 ?
trent
Sep 21, 2011I don’t currently have an android to test on, but my thoughts is it should work. You may need to have a look at this follow up post:
http://trentrichardson.com/2011/03/15/advanced-media-queries-for-mobile-devices/
Where we tie in a small bit of js for occasions where css doesn’t quite do the trick.
Arun Sengupta
Jul 26, 2012WoW… that was so simple and easy to learn … and damn awesome :D
Thank you so much :)
Perth photography
Sep 29, 2012Amazing website and great tips on CSS media Queries. Thank you.