Working with CSS Media Queries and Media Types, Part 2

With more and more Internet traffic now coming from mobile devices, it's time to start thinking about how your website can be optimized to look good and function well on phones, tablets, and other devices, if you haven't thought about it already. Although there's no magic pill to transform a site to look good across multiple devices, you can start the process by integrating Cascading Style Sheets (CSS) media queries into your site. Media queries provide a way to dynamically reorganize content in a page using pure CSS. Although they aren't the only thing to consider when creating mobile-optimized sites, CSS media queries do provide a simple way to repurpose existing content without changing back-end code.

In the previous article of this series, "Working with CSS Media Queries and Media Types, Part 1," I introduced the concept of CSS media queries and discussed the fundamentals of how you can use media queries to target different screen sizes. I showed how you could use media queries to convert a three-column–wide page into a more vertical view of data suitable for devices such as an iPhone, as shown in Figure 1.

The top part of Figure 1 represents a simple web page displayed in the browser. When the site is viewed on a mobile device, it's transformed dynamically according to the screen width. In this article, I'll provide an additional look at how CSS media queries can be used to mobile-enable a sample website called Widget Masters, shown in Figure 2, without having to change any server-side code or HTML code.

As shown in Figure 2, this site has some of the standard items that appear in most websites today, including the title area, menu bar, and sections where data is displayed. Without including CSS media queries, the site is readable but has to be zoomed out for a user to see everything, cuts off some of the menu items, and requires horizontal scrolling to get to additional content, as you can see in the example in Figure 3.

Although the site works on mobile devices, it's definitely not optimized for mobile. Let's see what we can do to optimize the site for mobile viewing by using CSS media queries to override existing styles in the site based on different screen widths.

Adding CSS Media Queries into the Mix

The Widget Masters website relies on standard CSS combined with HTML5 elements to provide the layout shown in Figure 2. For example, to lay out the menu bar shown at the top of the page, the nav element is used, as you can see in the code in Figure 4.

You could certainly use a standard div element instead, if desired. The HTML in Figure 4 is combined with the CSS shown in Figure 5 to add a CSS3 gradient, handle the horizontal orientation, and add some general hover effects.

When mobile devices hit the site, the layout of the menu items needs to be adjusted so that they're all visible without users having to swipe left or right to access them. This type of modification can be accomplished using CSS media queries by targeting specific screen sizes. To start, you can add a media query into the site's CSS file, as shown next:

This media query targets screens that have a maximum width of 320 pixels. Additional types of queries can also be added; refer to " Working with CSS Media Queries and Media Types, Part 1" for more details, as well as resources that you can use to test media queries in different devices. In that article, I emphasize (and I'll emphasize again) that CSS media queries only modify the overall layout and look and feel of a site. They don't optimize the site as far as the size of the images or content sent to the device, which is important to keep in mind.

To make the navigation menu more accessible on devices such as an iPhone or Android, you can use the CSS shown in Figure 6.

This code changes the height of the menu from 40 pixels to 100 percent, takes off the li element floats, changes the line-height, and changes the margins. Figure 7 shows an example of what the menu looks like when run on a device with a width of 320 pixels.

Mobile devices with a maximum width of 480 pixels need different CSS styles applied because they have 160 additional pixels of width. To do this, you can add a new CSS media query into the style sheet, as shown in Figure 8. Looking through the CSS, you'll see that only a minimal override is added to adjust the padding of anchor tags because the menu fits by default in this screen width.

Running the site on a device with 480 pixels will render the menu in Figure 9. Notice that the space between the menu items is much smaller compared with what was shown earlier in Figure 2.

In addition to modifying the menu, you can change the three horizontal content sections shown earlier in Figure 2 from a horizontal layout to a vertical layout, so that they look good on a variety of smaller mobile devices and can be more easily navigated by end users. The HTML5 article and section elements are used as containers for the three sections in the site, as shown in Figure 10.

Styles to target the display size of the images in each section are also included. It's important to note that the original image is still being downloaded from the server and isn't being optimized in any way for the mobile device. It's certainly possible for the CSS to include URL information for a mobile-optimized image, if desired.

Figure 12 and Figure 13 show the site rendered on an iPhone with the CSS media queries in place. Each of the sections now displays vertically, making it much easier for the user to access them. Images within each section also scale appropriately to fit properly.

More Flexibility for Your Website

CSS media queries provide a great way to override default styles in a website and target devices with different resolutions. In this article, you've seen how CSS media queries can be used to convert a standard browser-based site into a site that's more accessible to mobile users. Although much more can be done to optimize sites for mobile, CSS media queries provide a nice starting point if you don't have the time or resources to create mobile-specific versions of sites. Next, we'll show you how to utilize the techniques that you've learned to implement a responsive design interface with HTML5 and CSS3.