Responsive Web Design by Example:Beginner's Guide(Second Edition)
上QQ阅读APP看书,第一时间看更新

A look into responsive frameworks

Building a responsive website can be very tedious work. There are many measurements to be considered while building a responsive website, one of which would be creating the responsive grid.

The grid helps us build websites with proper alignment. If you have ever used 960.gs ( one of the popular CSS frameworks, you would have experienced how easy it is to organize the web page layout by adding preset classes, such as grid_1 or push_1, in the elements.

However, the 960.gs grid is set in a fixed unit, namely, pixel (px), which is not applicable when it comes to building a responsive website. We need a framework with the grid set in percentage (%) unit to build responsive websites; we need a responsive framework.

A responsive framework provides the building blocks to build responsive websites. Generally, it includes the classes to assemble a responsive grid, the basic styles for typography and form inputs, and a few styles to address various browser quirks. Some frameworks even go further with a series of styles to create common design patterns and web user interfaces such as buttons, navigation bars, and image slider. These predefined styles allow us to develop responsive websites faster with less hassle. The following are a few other reasons why using a responsive framework is a favorable option to build responsive websites:

  • Browser compatibility: Assuring the consistency of a web page on different browsers is really painful and more distressing than developing the website itself. However, with a framework, we can minimize the work to address browser compatibility issues. The framework developers have most likely tested the framework on various desktop browsers and mobile browsers with the most constrained environment prior to releasing it publicly.
  • Documentation: A framework, in general, also comes with comprehensive documentation that records the bits and pieces on using the framework. The documentation will be very helpful for entry users to begin studying the framework. It is also a great advantage when we are working with a team. We can refer to the documentation to get everyone on the same page and follow the standard code of writing conventions.
  • Community and extensions: Some popular frameworks such as Bootstrap and Foundation have an active community that helps address the bugs in the framework and extends the functionality. The jQuery UI Bootstrap ( good example in this case. The jQuery UI Bootstrap is a collection styles for jQuery UI widgets to match the feel and look of Bootstrap's original theme. It's now common to find free WordPress and Joomla themes that are based on these frameworks.

Through the course of this book, we will be building three responsive websites by using three different responsive frameworks, namely Responsive.gs, Bootstrap, and Foundation.

The Responsive.gs framework

Responsive.gs ( variants of grids, that is, 12, 16, and 24 columns. What's more, Responsive.gs is shipped with box-sizing polyfill, which enables CSS3 box-sizing in Internet Explorer 6, 7, and 8, and makes it decently presentable in those browsers.

Note

Polyfill is a piece of code that enables certain web features and capabilities that are not built in the browser natively. Usually, it addresses the older versions of Internet Explorer. For example, you can use HTML5Shiv (https://github.com/aFarkas/html5shiv) so that new HTML5 elements, such as <header>, <footer>, and <nav>, are recognized in Internet Explorer 6, 7, and 8.

A word on CSS box model

HTML elements, which are categorized as block-level elements, are essentially boxes drawn with the content width, height, margin, padding, and border through CSS. Prior to CSS3, we were facing constraints when specifying a box. For instance, when we specify a <div> tag with a width and height of 100px, as follows:

div { 
  width: 100px;
  height: 100px;
}

The browser will render div as a 100px square box, as shown in the next diagram:

However, this will only be true if the padding and border have not been added in. As a box has four sides, a padding of 10px (padding: 10px;) will actually add 20px to the width and height—10px for each side, as shown in the following diagram:

While it takes up space on the page, the element's margin space is reserved outside the element rather than as part of the element itself; thus, if we give an element a background color, the margin area will not take that color.

CSS3 box sizing

CSS3 introduced a new property called box-sizing, which lets us specify how the browser should calculate the CSS box model. There are a couple of values that we can apply within the box-sizing property.

In each of the projects in this book, we will be using the border-box value so that we can determine the box dimensions with ease for the websites. Let's take our preceding example to understand this, but this time we will set the box-sizing model to border-box. As mentioned in the preceding table, the border-box value will retain the box's width and the height as 100px, regardless of the padding and border addition. The following illustration shows a comparison between the outputs of the two different values, content-box (the default value) and border-box:

In this book, we will use Responsive.gs and explore more of it in the next two chapters to build a simple responsive blog.

The Bootstrap framework

Bootstrap ( by Mark Otto ( intended only for internal use on Twitter. In short, Bootstrap was then launched for free for public consumption.

Note

Bootstrap has long been associated with Twitter, but since the author has departed from Twitter and Bootstrap itself has grown beyond his expectations, Bootstrap now stands on its own brand (http://blog.getbootstrap.com/2012/09/29/onward/).

If you refer to the initial development, the responsive feature was not yet added. It was then added in Version 2 due to the increasing demand for creating responsive websites.

Bootstrap also comes with many more added features as compared to Responsive.gs. It is packed with preset user interface styles, which comprise common user interfaces used on websites such as buttons, navigation bars, pagination, and forms, so you don't have to create them from scratch again when starting a new project. On top of that, Bootstrap is also powered with some custom jQuery plugins such as image slider, carousel, popover, and modal box.

You can use and customize Bootstrap in many ways. You can directly customize the Bootstrap theme and its components directly through the CSS style sheets, the Bootstrap customize and download page (http://getbootstrap.com/customize/), or the Bootstrap LESS variables and mixins, which are used to generate the style sheets.

In this book, we will go into Bootstrap in Chapter 5, Developing a Portfolio Website with Bootstrap, and Chapter 6, Polishing the Responsive Portfolio Website with LESS, to build a responsive portfolio website.

The Foundation framework

Foundation (by ZURB, a design agency based in California. Similar to Bootstrap, Foundation is not just a responsive CSS framework; it is shipped with a preset grid, components, and a number of jQuery plugins to present interactive features.

Some high-profile brands, such as McAfee (http://www.mcafee.com/common/privacy/english/slide.html), which is one of the most respectable brands for computer antivirus, have built their websites using Foundation.

The Foundation style sheet is powered by Sass, a Ruby-based CSS preprocessor. We will be discussing more about Sass, along with the Foundation features in the last two chapters of this book; therein, we will be developing a responsive website for a startup company.

Tip

There are many complaints that the code in responsive frameworks is excessive; as a framework such as Bootstrap is used widely, it has to cover every design scenario and thus, it comes with some extra styles that you might not need for your website. Fortunately, we can easily minimize this issue by using the right tools, such as CSS preprocessors, and following a proper workflow.

Frankly, there isn't a perfect solution; and using a framework certainly isn't for everyone. It all comes down to your needs, your website's needs, and in particular, your client's needs and budgets. In reality, you will have to weigh these factors to decide whether you will go with a responsive framework or not. Jem Kremer has an extensive discussion in this regard in her article Responsive Design Frameworks: Just Because You Can, Should You? (http://www.smashingmagazine.com/2014/02/19/responsive-design-frameworks-just-because-you-can-should-you/)

A brief introduction to CSS preprocessors

Both Bootstrap and Foundation use CSS preprocessors to generate their style sheets. Bootstrap uses LESS ( Sass as the only way to generate its style sheets (http://sass-lang.com/).

CSS preprocessor is not an entirely new language. If you have known CSS, you should be accustomed to the CSS preprocessor immediately. The CSS preprocessor simply extends CSS by allowing the use of programming features such as variables, functions, and operations.

The following is an example of how we write CSS with the LESS syntax:

@color: #f3f3f3;

body {
  background-color: @color;
}
p {
  color: darken(@color, 50%);
}

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

When the preceding code is compiled, it takes the @color variable that we have defined and places the value in the output, as follows:

body {
  background-color: #f3f3f3;
}
p {
  color: #737373;
}

The variable is reusable throughout the style sheet and this enables us to retain style consistency and make the style sheet more maintainable.

We are going to use and explore CSS preprocessors, LESS, and Sass further during the course of building responsive websites with Bootstrap (Chapter 5, Developing a Portfolio Website with Bootstrap and Chapter 6, Polishing the Portfolio Website with LESS) and Foundation (Chapter 7, A Responsive Website for Business with Foundation, and Chapter 8, Extending Foundation).

Have a Go Hero — delve into responsive web design

Our discussion on responsive web design here, though essential, is merely the tip of the iceberg. There is so much more about responsive web design than what we have recently covered in the preceding sections. I would suggest that you take your time to get yourself more insight and remove any apprehension on responsive web design, including the concept, the technicalities, and some constraints.

The following are some of the best recommendations for references:

Pop Quiz — responsive web design main components

Q1. In his article, which we have referred to about two times in this chapter, Ethan Marcotte mentioned the main technical ingredients that formulate a responsive website. What are those main components?

  1. Viewport Meta Tag and CSS3 Media Queries.
  2. Fluid grids, flexible images, and media queries.
  3. Responsive images, breakpoints, and polyfills.

Q2. What is a viewport?

  1. The screen size of the device.
  2. The region where the web page is rendered.
  3. The meta tag to set the web page's viewport size.

Q3. Which one of these is the correct way to declare CSS3 Media Queries?

  1. @media (max-width: 320px) { p{ font-size:11px; }}
  2. @media screen and (max-device-ratio: 320px) { div{ color:white; }}
  3. <link rel="stylesheet" media="(max-width: 320px)" href="core.css" />