Mobile-first paradigm

Mobile devices have a smaller screen, are generally touch oriented, and have different user experience expectations than a desktop computer.

To accommodate the smaller screen, we use responsive web design techniques. This means designing the application in such a way that it accommodates the screen size and other device attributes. The idea is crafting websites to provide an optimal viewing and interaction experience across a wide range of devices. The techniques include changing font sizes, rearranging elements on the screen, using collapsible elements that open when touched, and resizing images or videos to fit the available space. This is called responsive because the application responds to device characteristics by making these changes.

Note

By mobile-first, we mean that you design the application first to work well on a mobile device and then move on to devices with larger screens. It's about prioritizing mobile devices first.

The primary technique is to use media queries in the stylesheet to detect device characteristics. Within sections controlled by media queries targeting a range of devices, the CSS declarations must match the targeted devices.

It may help to consult a concrete example. The Twenty Twelve theme for Wordpress has a fairly straightforward responsive design implementation. It's not built with any framework, so you can see clearly how the mechanism works, and the stylesheet is small enough to be easily digestible. Refer to its source code in the Wordpress repository at https://themes.svn.wordpress.org/twentytwelve/1.9/style.css.

The stylesheet starts with a number of resets, where the stylesheet overrides some typical browser style settings with clear defaults. Then, the bulk of the stylesheet defines styling for mobile devices. Toward the bottom of the stylesheet is a section labeled Media queries where, for certain sized screens, the styles defined for mobile devices are overridden to make sense for devices with larger screens.

It does this with the following two media queries:

@media screen and (min-width: 600px) {
  /* For screens above 600px width */}
@media screen and (min-width: 960px) {
  /* For screens above 960px width */}

In other words, the first segment of the stylesheet configures the page layout for all devices. Next, for any browser viewport at least 600px wide, reconfigure the page to display on the larger screen. Then, for any browser viewport at least 960px wide, reconfigure it again. The stylesheet has a final media query to cover print devices.

The pixel widths given earlier are what's called a breakpoint. Meaning, those threshold viewport widths are where the design changes itself around. You can see these breakpoints in action easily by going to any website with a responsive design, then resizing the browser window. Watch how the design jumps at certain sizes. Those are the breakpoints chosen by the author of that website.

There's a wide range of differing opinions about the best strategy to choose your breakpoints. Do you target specific devices or do you target more general characteristics? The Twenty Twelve theme did fairly well on mobile devices using only two media queries. At the other end of the scale, the CSS-Tricks blog posted an extensive list of specific media queries for every known device, which is available at https://css-tricks.com/snippets/css/media-queries-for-standard-devices/.

We should at least target these device scenarios:

  • Small: This includes iPhone 4.
  • Medium: This can refer to tablet computers, or the larger smart phones.
  • Large: This includes larger tablet computers, or the smaller desktop computers.
  • Extra-large: This refers to larger desktop computers and other large screens.
  • Landscape/portrait: You may want to create a distinction between landscape mode and portrait mode. Switching between the two of course changes viewport width, possibly pushing it past a breakpoint. However, your application may need to behave differently in the two modes.

Enough with the theory, let's get back to our code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset