How To Use Media Queries For Device Targeting | Van SEO Design |
How To Use Media Queries For Device Targeting Posted: 30 Jun 2011 05:30 AM PDT Media queries are a way to serve different css rules to different devices and device characteristics. They’re the final piece in allowing us to create designs that are truly responsive to our audience and how our visitors choose to view our sites. All of these posts rely heavily on the work of Ethan Marcotte and so before getting started I want to point you again to some of Ethan’s writing on the subject.
I highly recommend the last link above, which is Ethan’s new book on responsive design. If you buy the digital version (PDF, ePub, and mobi) it’ll only set you back $9, which will be money well spent. Media TypesAs I mentioned above media queries are a way to serve different css based on device and device characteristics. For example we can send certain css to a browser on a laptop open 1200px wide and send different css to a mobile browser open to 480px and a third set of css to a tv screen displaying at 1024px. We’ve actually been using a part of media queries for years, namely media types so let’s been there. The html below should look familiar to you. We use it all the time to include a css file in an html document. < link rel="stylesheet" type="text/css" href="core.css" media="screen" / > Note the media=”screen” at the end. That’s a media type. You may have written a print specific stylesheet at some point for which you’d use media=”print” There are a variety of other media types you could use beyond screen and print.
Media types are useful, but they aren’t enough. Not every device lists itself properly and realistically what works on one handheld device or tv might not work on another. Enter media queries. Media QueriesBuilding on top of media types the W3C added media queries. A simple media query looks like the following: @media screen and (max-width: 1200px) { css here&hellip } It begins with the media type (in this case screen) and is followed by the query as a feature: value pair. Here we’re targeting browsers with a max-width of 1200px The type and the query are joined by the “and” operator. We’ll see in a bit that we can chain this operator to target very specific devices and characteristics Above we started the media query with @media, but there are actually 3 ways to include media queries.
The @media method is probably the most common and it has the advantage of keeping all your css in a single css file. It’s likely what you’ll use most often in practice. There are a baker’s dozen media features you can use with media queries summarized in the table below.
In the values above viewport is defined as the part of the screen/paper where the document is rendered and includes any scrollbar. The rendering surface defines the width of the screen. Yeah, it’s confusing to me too. Typically though you’ll target the display area or viewport. Chaining Media QueriesAs I said above we can use the “and” operator to chain queries like so: @media screen and (max-width: 1200px) and (orientation: landscape) We can also use the “not” operator as in: @media not projection and (max-width: 1200px) to target devices other than projection with a max-with of 1200px. The “not” operator is evaluated last so the above is evaluated as: @media not ( projection and (max-width: 1200px) ) There’s no specific “or” operator, but can imply “or” by using a comma. @media screen and (min-width: 800px) and (orientation: landscape), @media screen and (max-width: 1200px) The above would target both screens with a min-width of 800px in that are in landscape mode and screens with a max-width of 1200px. The above queries aren’t necessarily those you would use in practice, but you can imagine how powerful and targeted this chaining can be. A New Meta TagWhen Apple created mobile safari they also created a new meta tag attribute for it. To display web pages mobile safari actually displays pages as they appear at 980px wide. It then shrinks down the page so it fits within the 320 x 480 display. The 980px is the viewport. Other mobile browsers followed suit with the larger viewport and shrinking. We’d like things to be a little more consistent with the actual size of the device and fortunately we have the new meta tag. <meta name="viewport" content="width=320" /> Instead of setting an absolute viewport size we can be more flexible and set <meta name="viewport" content="initial-scale=1.0, width=device-width" /> The above basically sets the zoom level to 100% Browser SupportBrowser support for media queries is generally good, though as is often the case IE took awhile to catch up. Media queries are available in IE9, but for anything below we need to do a little extra to get them work. There are some javascript libraries Ethan recommends we use. Both will add support for media queries to older browsers. Why Use Media QueriesEven flexible layouts can bend or worse break. Flexible layouts can resize and accommodate a variety of width ranges, but they can still fail or simply look and work less than ideal above and below certain widths. A different design might also be more effective on different devices. For example:
A variety of conditions beyond the above could lead us to modify our designs based on any of the devices or features we can target. In the simple case of screen width a good choice is to allow our designs to be flexible throughout a range or widths and then modify the design for different sets of widths. Of course this begs the question what ranges of widths should we use? What breaking points should we set? I’ll leave the question hanging until next week when we talk more about developing a responsive design workflow. SummaryMedia Queries are a great way to target different devices and even better different characteristics of different devices. And the best part is they’re intuitive and relatively easy to use. They take the form of media type and media query (as a feature: value pair) and through the ability to chain operators allow us to build powerful and highly targeted queries. Through media queries we can set a variety of points over which our flexible layout might bend and serve different css to prevent those bends from breaking and causing problems for our visitors. By combining flexible layouts and flexible images and other media with media queries we have the ability to create designs that respond to the many different ways our audience might access and view our sites. Next week we’ll take a look at responsive design and talk generally about how we should think about building a responsive workflow. |
You are subscribed to email updates from Van SEO Design » Blog To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |