Chapter 3. Responsive Design

For more than a decade of the Web’s existence, we could safely assume that each user of our site would be accessing it through a computer screen. Despite this, early websites were, by default, adaptive to a variety of screen sizes. The Web’s first site, Tim Berners-Lee’s World Wide Web, works beautifully at a range of screen sizes (Figure 3-1).1

Despite this, we spent time researching and considering the typical browser width and assumed that our users would be perched in front of a reasonably large screen, with a dedicated keyboard. With the evolution of mobile devices, those assumptions have changed. Users may access our sites quickly, on the go, from a wide range of screen sizes. With the diversity of devices and screens, we can no longer safely make assumptions about the screen size of our users’ devices.

The initial reaction to the rise of smartphones was to create dedicated mobile versions of our sites. This often sat at a m. subdomain, such as http://m.example.com, and provided a mobile-optimized experience. At first, this seemed like a great solution because it allowed users to access our services in a format that was streamlined for their device. For developers, this also meant maintaining multiple codebases. For users, this often meant dealing with a limited subset of functionality when using a mobile device.

Figure 3-1. Screenshot of the first website with a narrow viewport

Today, the number of devices that connect to the Web is again expanding. Users may access our applications from a desktop computer, a mobile phone, a tablet, a reading device, a watch, a video game system, or in their car. The site Global Stat Counter reports that 119 different screen resolutions have accessed the Web over the past year.

In 2010, web designer and developer Ethan Marcotte coined the term responsive design, to describe the practice of building websites that adapt to a range of screen sizes. By building responsively, we can develop a single codebase that acclimates to the screen size of the device. This allows us to make fewer assumptions while delivering a site that works in any context.

Responsive design consists of three core elements:

Fluid grids
Allow the layout of the page to condense and expand to fill the screen size, rather than providing a strict width.
Flexible media
Our images and videos are also not limited by a predetermined width, but adapt with the content of the page.
Media queries
A CSS technique that allow developers to apply different CSS rules in varying contexts.

By combining these three browser capabilities, we are able to develop sites for a wide range of browser sizes. When we build responsively, we are ensuring that our sites are delivered to our users in a way that works well in the context that they are accessing our site.

Responsive Design Process

Responsive design is not about “designing for mobile.” But it’s not about “designing for the desktop,” either. Rather, it’s about adopting a more flexible, device-agnostic approach to designing for the web.

Ethan Marcotte

The process of responsive design can be broken down into four steps:

  1. Instruct the browser viewport to adapt to the screen size.
  2. Set flexible media elements that can adapt to the width of the container.
  3. Develop a device-agnostic baseline experience.
  4. Use CSS3 media queries to enhance the experience at a variety of screen sizes (often termed “breakpoints”).

Let’s tease this process apart by creating a very simple responsive page.

By default, mobile browsers will render the page at a desktop screen width. This means that users will need to pinch and zoom to be able to read and access our content. To tell the browser to scale, we can add a meta viewport tag to the <head/> of the HTML document:

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

The most basic approach to responsive media to scale our images and other media elements to the width of their parent container. In our CSS file, we apply a max-width: 100% to media objects to ensure that they never overflow beyond the container width. In Chapter 4, we will explore how to serve various image sizes depending on browser context:

img,
obj,
video {
  max-width: 100%;
  height: auto;
}

With the baseline of a scaled browser viewport and flexible media, we can begin developing the core experience. The core experience can encompass things such as typography, color, and base styles that should appear in all browsers. By doing so, we ensure that every user is served a site that will work well in their browser regardless of capability. Originally, this approach was termed mobile first, but I’ve come to favor Trent Walton’s description of device agnosticism. By taking this approach, we are developing in a future-friendly way that is prepared for devices of all sizes.2

With our baseline styles in place, we can begin adding styles based on browser width. To do this, we use CSS media queries, which allow us to apply specific styles to given browser widths. These can and should be based on the ideal conditions of our application content. For the purpose of responsive design, we’ll focus on max-width and min-width media queries.

A max-width media query allows us to define styles that will only appear up until a certain breakpoint:

@media (max-width: 600px) {
  /* Smaller device/browser styles go here */
}

In contrast, min-width media queries allow us to set styles that are only applied at larger browser sizes:

@media (min-width: 601px) {
  /* Larger device/browser styles go here */
}

In the end, we may wind up with a stylesheet that is structured with base styles followed by media queries defining styles for various browser sizes, often termed breakpoints:

/* Base Styles */

@media (max-width: 600px) {
  /* Smaller device/browser styles */
}

@media (min-width: 601px) {
  /* Large device/browser styles */
}

@media (min-width: 850px) {
  /* Larger device/browser styles */
}

@media (min-width: 1100px) {
  /* Largest device/browser styles */
}

By using breakpoints, we can define styles based on the context of the user’s browser, adapting the content of our site to better meet their needs.

CSS Frameworks

If you are not a fan of writing CSS (and who could blame you?), you may opt to use a CSS framework such as Bootstrap or Foundation. These and many other UI frameworks are responsive by default and can be useful for rapid prototyping and quick interface development

Responsive Design Considerations

When developing a responsive design, there are a number of conditions a developer should take into account. Primarily, we should avoid making assumptions about user context and build for non-ideal conditions.

Some of the key considerations for developing responsive designs:

  • Provide users with large click areas for links and buttons.
  • Ensure that site navigation is accessible and easy to understand.
  • Make forms as simple as possible, and autofill form content when possible.
  • Focus on the content of the application, and set breakpoints accordingly, rather than by common device sizes.

Further Reading

1 Though Berners-Lee’s first website adapts to any browser width, it still scales on most mobile browsers due to browser behavior. As we will discuss later in the chapter, adding a viewport meta tag to our HTML prevents this from happening.

2 Brad Frost filmed a demonstration of a project from 2013 on an Apple Watch.

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

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