IT581: Practicum in Web Development, Summer 2015 (Hybrid)

Responsive Web Design

Basic Responsive Web Design

Responsive web design is a technique that uses CSS to adapt a page's layout based on screen size. It is just one strategy we are employing to cope with the mind-blowing variety of screen sizes.

Responsive Web Design by Ethan Marcotte at A List Apart, May 25, 2010

Figure 18.10

How it Works

Responsive design has three core components:

A fluid layout

You learned about fluid layouts in "Page Layout" discussion

Flexible images

When the layout scales down, the images and other embedded media need to scale with it; otherwise, they would hang out of view.

CSS media queries

Media queries are a method for applying styles based on the medium via which the document is displayed. Queries start with questions, such as, "Is the document being printed? Then use these print-appropriate styles." Or, "Is the document on a screen, and is that screen at least 1,024 pixels wide and in landscape mode? Then use these styles."

We will add a fourth component, the viewport meta element that makes the width of the web page match the width of the screen. That's where we'll begin our responsive project.


Setting the viewport

To fit standard websites onto small screens, mobile browsers render the page on a canvas called the viewport and then shrink that viewport down to fit the width of the screen (device width).

For example, on iPhones, Mobile Safari sets the viewport width to 980 pixels, so a web page is rendered as though it were on a desktop browser window set to 980 pixels wide. But that rendering gets shrunk down to 320 pixels wide when the iPhone is in portrait orientation, cramming a lot of information into a tiny space.

To signal to browsers that your page will adapt to all devices, add a meta tag to the head of the document.

<meta name="viewport" content="width=device-width, initial-scale=1">

This line tells the browser to set the width of the viewport equal to the width of the device screen (width=device-width), whatever that happens to be. The initial-scale sets the zoom level to 1 (100%).


Exercise Part 1: Set the Viewport Size

  1. The viewport meta element tells the browser what to assume as the screen size.
  2. Important! If you have not yet installed the Firefox/Chrome Add-on "Web Developer Toolbar" do so now! The Toolbar has a "Resize" option that will be very useful for this exercise.
  3. Make copies of the files jenware-rwd.html and jenware-rwd.css. Be sure to save the images too. The style sheet, jenware.css, takes care of the basic styling such as backgrounds, colors, borders, and text styles, providing a good baseline styled experience.
  4. Open the page in your browser (Firefox or Chrome).
  5. In the Web Developer Toolbar that is located under "Tools" tab, click "Responsive Design Mode". The tool opens a new tab that shows many different viewport sizes.
  6. The Jenware page adapts fairly well to all the sizes, except for the logo graphic in the Mobile Portrait size.
  7. Resize the window as wide as you can. You should find that the original page stretches uncomfortably wide.
  8. Add the viewport meta element. Open jenware-rwd.html in a text editor and add the standard meta element as shown here:
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
  9. Place the meta tag below the other Meta tags in your head section or above your title tag.
  10. Because this is a mobile property, you won't notice any changes when you look at the page again in your desktop browser, but the foundation has been laid for improvements.

Fluid layouts

Fluid layouts are created using percentage or em/rem measurements so that elements resize proportionally to fill the available width of the screen or window

It's not feasible to create a design for all the possible device widths on which your page might be viewed. Web designers generally create two or three designs (sometimes a few more) targeted at major device classes, such as smartphones, tablets, and desktop browsers.

They rely on fluid layouts to take care of all the possible sizes in between. Fluid layouts avoid awkward amounts of leftover space and prevent the right side of the page from getting cut off.


Making images flexible

Making images flexible is simple. T make images scale down to fit the size of their container:

        img {
          max-width: 100%;
        }

When you use the max-width property, be sure that there are no width and height attributes in the img elements in the HTML document, or the image won't scale proportionally.

Figure 18.11

Exercise Part 2: Make images flexible

  1. In the CSS file, add the image resizing style rule to the style sheet right after the body rule.
    img { max-width: 100%; }

Check your results with the Toolbar resizer. Also experiment with adjusting the window size.


Media Queries

  • Media queries allow designers to deliver styles based on media type. The defined media types are print, speech, handheld, braille, projection, screen, tty, and tv. The keyword all indicates that the styles apply to all media types
  • Media queries can also evaluate specific media features, such as the device-width, orientation, and resolution.
  • Most properties can be tested for a minimum or maximum value using the min- and max- prefixes, respectively.
  • For example, min-width: 480px tests whether the display is at least 480 pixels wide. 768-pixel-wide displays pass the test and get the styles; a 320-pixel display would not.
  • There is a complete list of device features you can detect with media queries in table 18-1 in the textbook.
  • You add media queries to a style sheet along with your other styles.
    • Here is an example of a style sheet media query that determines whether the media type is a screen and whether it is at least 480 pixels wide:
          @media screen and (min-width: 480px;) {
      
            /* put styles for devices & browsers that pass this test inside the curly braces */
      
          }
    • This next example that tests for whether the screen is under 700 pixels wide and is in landscape orientation. Notice that each feature and value pair is placed inside parentheses. The word "and" strings the various requirements together. The device must pass all of the requirements in order to deliver the enclosed styles.
      @media screen and (max-width: 700px;) and (orientation: landscape;) {
        /* put styles for devices & browsers that pass this test here */
      }
  • The last example, the media query looks to see whether the device has a high-density display like the Retina iPhone, iPad, and newer MacBook Pro.
    @media screen and (-webkit-min-device-pixel-ratio: 2),
            screen and (-moz-min-device-pixel-ratio: 2),
            screen and (-o-min-device-pixel-ratio: 2),
            screen and (-ms-min-device-pixel-ratio: 2),
            screen and (min-device-pixel-ratio: 2) {
    
     /* styles referencing high-resolution images here */
    
    }

Mobile First media queries

A best practice for responsive sites is to adopt a "mobile first" mentality. That means that you take care of the styles for the smallest, simplest devices first, and use media queries to bring in overriding styles that adapt the design as more display real estate and features become available.

Many designers try to take a "desktop first" a[[roach because they are redesigning an existing site, but mobile first is preferable. Especially since mobile browsers are now more common than desktop ones.

Mobile-first media queries tend to begin with the min- prefix, bringing in new styles when the width is at least the specified width or larger.

Remember that styles lower in a stack override the styles that precede them, whether it's rules in a single style sheet or a list of link elements.

Your baseline styles should come first, followed by the small device styles, followed by the enhanced styles for larger browsers.

Exercise Part 3: Add Media Queries

  1. Open jenware.css in a text editor. The current style sheet creates that edge-to-edge, one-column design that works great for narrow screens but looks miserable when it gets wide.
  2. Start by adding styles for devices that are at least 481 pixels wide.
    • With a little extra space, you can float the product images to the left and clear the following "More about..." links, just as you did a few lessons ago. Don't forget to add the class="more" attributes.
    • The style also adds some margins and rounded corners.
    • Put the new rules at the end of the stylesheet.
          @media screen and (min-width: 481px) {
            #products img {
              float: left;
              margin: 0 6px 6px 0;
            }
            #products .more {
              clear: left;
            }
            #products {
              margin: 1em;
            }
            #testimonials {
              margin: 1em 5%;
              border-radius: 16px;
            }
          }
        
  3. The next set of styles will kick in when the display area is at least 780 pixels wide.
    • The styles in this media query create a two-column layout by floating the #products div to the left and applying a wide left margin to the #testimonials box.
    • The copyright paragraph is cleared so it appears at the bottom of the page.
    • Finally, set a max-width on the #content div so the content will never appear wider than 1,024 pixels, even if the browser is expanded much wider
    • Put the new rules at the end of the stylesheet, after the previous set.
              @media screen and (min-width: 780px) {
                    #products {
                      float: left;
                      margin: 0 2% 1em;
                      clear: both;
                      width: 55%;
                      overflow: auto;
                    }
                    #testimonials {
                      margin: 1em 2% 1em 64%;
                    }
                    p#copyright {
                       clear: both;
                    }
                    #content {
                      max-width: 1024px;
                      margin: 0 auto;
                    }
                  }
  4. Check your site in the browser. Experiment to see how it responds to different widths.

Tricky Issues

Getting responsive right takes a lot of planning and work. Because the mobile Web is relatively new, the development community is still encountering and working through the challenges of mobile design.

Choosing breakpoints

The point at which the media query delivers a new set of styles is called a breakpoint. One of the primary design decisions in creating a responsive design is deciding at which widths to introduce a significant design change.

Some sites have just two layouts triggered at a single breakpoint. More commonly, responsive sites use three designs targeted at typical phone, tablet, and desktop widths, and but some sites use as many as five. How many you choose depends on the nature of your site's design.

Responsive Images

One of the most difficult problems facing mobile web developers is how to get images right.

Ideally, a device will download only the image size that is best for its dimensions and network speed. The goal is to avoid downloading unnecessary data, whether that comes in the form of an image that is larger than it needs to be for a small screen or downloading two versions of an image (low-res and high-res) when only one is needed.

One content doesn't fit all

CSS works fine for swapping out styles and moving elements around on the screen (or even hiding them).

But in many cases, smaller devices are better served with different content or the same content in a different order. JavaScript can handle a certain amount of rearranging and offers a way to conditionally load content.

Responsive limitations

For some websites, particularly text-heavy sites like blogs, a responsive redesign is all that is needed to make them pleasant to use on small screens.

For other sites, however, simply adjusting the styles is not enough. When the mobile use case for a site or service is significantly different from desktop use (based on user testing, of course), then it may be necessary to build a separate mobile site.

Completing the Assignment

  • Upload the final files you created in parts 1, 2, and 3 and all images to your web space on wyrd and provide links on Blackboard.
Comments to: dong@hood.edu
Last Modified: 03 August 2015. 19:40