Book Image

HTML5 and CSS3: Building Responsive Websites

By : Ben Frain, Thoriq Firdaus, Benjamin LaGrone
Book Image

HTML5 and CSS3: Building Responsive Websites

By: Ben Frain, Thoriq Firdaus, Benjamin LaGrone

Overview of this book

Responsive web design is an explosive area of growth in modern web development due to the huge volume of different device sizes and resolutions that are now commercially available. The Internet is going mobile. Desktop-only websites just aren’t good enough anymore. With mobile internet usage still rising and tablets changing internet consumption habits, you need to know how to build websites that will just “work,” regardless of the devices used to access them. This Learning Path course explains all the key approaches necessary to create and maintain a modern responsive design using HTML5 and CSS3. Our first module is a step-by-step introduction to ease you into the responsive world, where you will learn to build engaging websites. With coverage of Responsive Grid System, Bootstrap, and Foundation, you will discover three of the most robust frameworks in responsive web design. Next, you’ll learn to create a cool blog page, a beautiful portfolio site, and a crisp professional business site and make them all totally responsive. Packed with examples and a thorough explanation of modern techniques and syntax, the second module provides a comprehensive resource for all things “responsive.” You’ll explore the most up-to-date techniques and tools needed to build great responsive designs, ensuring that your projects won’t just be built “right” for today, but in the future too. The last and the final module is your guide to obtaining full access to next generation devices and browser technology. Create responsive applications that make snappy connections for mobile browsers and give your website the latest design and development advantages to reach mobile devices. At the end of this course, you will learn to get and use all the tools you need to build and test your responsive web project performance and take your website to the next level. This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products: ?Responsive Web Design by Example: Beginner’s Guide - Second Edition by Thoriq Firdaus ?Responsive Web Design with HTML5 and CSS3 - Second Edition by Ben Frain ?HTML5 and CSS3 Responsive Web Design Cookbook by Benjamin LaGrone
Table of Contents (6 chapters)

In the previous chapter, we constructed the blog markup from the header section to the footer section using HTML5 elements. The blog, however, is currently faceless. If you opened the blog in a browser, you will just see it bare; we have not yet written the styles that add up to its appearance.

Throughout the course of this chapter, we will focus on decorating the blog with CSS and JavaScript. We will be using CSS3 to add the blog styles. CSS3 brings a number of new CSS properties, such as border-radius, box-shadow, and box-sizing, that allow us to decorate websites without the need to add images and extra markup.

However, the CSS properties, as mentioned previously, are applicable only within the latest browser versions. Internet Explorer 6 to 8 are not able to recognize those CSS properties, and won't be able to output the result in the browsers. So, as an addition, we will also utilize a number of polyfills to make our blog presentable in the old Internet Explorer.

It's going to be an adventurous chapter. Let's go.

In this chapter, we shall cover the following topics:

CSS3 ships with long-awaited properties, border-radius and box-shadow, that simplify old and tedious methods that were used to present rounded corner and drop shadow in HTML. On top of that, it also brings a new type of pseudo-element that enables us to style the placeholder text shown in input fields through the HTML5 placeholder attribute.

Let's take a look at how they work.

Back in the 90s, creating a rounded corner was complicated. Adding a pile of HTML markup, slicing out images, and formulating multiple line style of rules is inevitable, as presented in the post by Ben Ogle at http://benogle.com/2009/04/29/css-round-corners.html.

CSS3 makes it much simpler to create rounded corners with the border-radius property, and the following is an example:

div {
  width: 100px; height: 100px;
  border-radius: 30px;
}

The preceding style rule will round the box corner (read the A word on CSS Box Model section in Chapter 1, Responsive Web Design) each for 30px, as shown in the following figure:

Creating rounded corners with CSS3

Furthermore, we can also round only to specific corners. The following code snippet, for example, will round only the top-right corner:

Much the same as creating rounded corners, using images was unavoidable to create shadow effects in the website in the past. Now, adding a drop shadow has been made easy with the introduction of the box-shadow property. The box-shadow property consists of five parameters (or values):

The first parameter specifies where the shadow takes place. This parameter is optional. Set the value to inset to let the shadow appear inside the box or leave it empty to display the shadow outside.

The second parameter specifies the shadow vertical and horizontal distance from the box.

The third parameter specifies the shadow blur that fades the shadow; a bigger number will produce a bigger but faded shadow.

The fourth parameter specifies the shadow expansion; this value is slightly contradicted to the shadow blur value. This value will enlarge yet also intensify the shadow depth.

The last parameter specifies the color. The color can be in any web-compatible color format, including Hex, RGB, RGBA, and HSL.

Carrying on the preceding example, we can add up box-shadow, as follows:

The preceding code will output the shadow, as shown in the following figure:

Creating drop shadow

Add inset at the beginning if you want to show the shadow inside the box, as follows:

Both the border-radius and box-shadow properties have been well-implemented in many browsers. Technically, if we would cater only to the latest browser versions, we do not need to include the so-called vendor prefix.

Yet, if we intend to enable these two properties, border-radius and box-shadow, back in the earliest browser versions, where they were still marked as experimental by the browser vendors such as Safari 3, Chrome 4, and Firefox 3, adding the vendor prefix is required. Each browser has its prefix as follows:

Let's carry on our previous examples (again). With the addition of the vendor prefix to cater to the earliest versions of Chrome, Safari, and Firefox, the code would be as follows:

The code may turn out to be a bit longer; still it is preferable over having to cope with complicated markups and multiple style rules.

Note

Chrome and its new browser engine, Blink

Chrome decided to fork Webkit and built its own browser engine on top of it, named Blink (http://www.chromium.org/blink). Opera, which previously discarded its initial engine (Presto) for Webkit, follows along the Chrome movement. Blink eliminates the use of the vendor prefix, so we would not find -blink- prefix or such like. In Chrome's latest versions, instead of using the vendor prefix, Chrome disables experimental features by default. Yet, we can enable it through the options in the chrome://flags page.

With the addition of HTML5, the placeholder attribute brings the question of how to customize the placeholder text. By default, browsers display the placeholder text with a light gray color. How do we change, for example, the color or the font size?

At the time of writing this, each browser has its own way in this regard. WebKit-based browsers, such as Safari, Chrome, and Opera, use ::-webkit-input-placeholder. Internet Explorer 10 uses :-ms-input-placeholder. Firefox 4 to 18, on the other hand, use pseudo-class, :-moz-placeholder, but it has then been replaced with the pseudo-element ::-moz-placeholder (notice the double colons) since Firefox 19 to keep up with the standard.

These selectors cannot be used in tandem within a single style rule. So, the following code snippet will not work:

They have to be declared in a single style rule declaration, as follows:

This is definitely inefficient; we added extra lines only to achieve the same output. There isn't another viable option at the moment. The standard for styling the placeholder is still in discussion (see the CSSWG discussion at http://wiki.csswg.org/ideas/placeholder-styling and http://wiki.csswg.org/spec/css4-ui#more-selectors for more details).

Once we have explored all the things that we are going to include in this project, let's set up the tool to put them together. In Chapter 1, Responsive Web Design, we have installed Koala. Koala is a free and open source development tool that ships with many features. In this first project, we will use Koala to compile style sheets and JavaScripts into a single file, as well as minify the codes to result in a smaller file size.

There will be about five style sheets that we are going to include in the blog. If we load all these style sheets separately, the browsers will have to pull off five HTTP requests, as shown in the following screenshot:

Working with Koala

As shown in the preceding screenshot, the browser performs five HTTP requests to load all the style sheets, which have a size of 24.4 KB in total and require around 228 ms in total to load.

Combining these style sheets into a single file and squishing the codes therein will speed up the page-load performance. The style sheet can also become significantly smaller, which eventually will also save bandwidth consumption.

As shown in the following screenshot, the browser only performs one HTTP request; the style sheet size is reduced to 13.5KB, and takes only 111 ms to load. The page loads about 50 percent faster in comparison with the preceding example:

Working with Koala

In this section, we will integrate the configured Koala to compile and output the style sheets, by performing the following steps:

  1. Create a new style sheet in the css folder named main.css. This is the prime style sheet, where we will compose our own style rules for the blog.
  2. Create a new style sheet named style.css.
  3. Download normalize.css (http://necolas.github.io/normalize.css/), and put it in the css folder of the project directory.
  4. Download formalize.css (http://formalize.me/), and also put it in the css folder of the project directory.
  5. Open style.css in Sublime Text.
  6. Import the supporting style sheets using the @import rule in the following order, as follows:
    @import url("css/normalize.css");
    @import url("css/formalize.css");
    @import url("css/responsive.gs.12col.css");
    @import url("css/main.css");
    @import url("css/responsive.css");
  7. Launch Koala. Then, drag-and-drop the project directory into the Koala sidebar. Koala will show and list recognizable files, as shown in the following screenshot:
    Time for action – integrating project directory into Koala and combining the style sheets
  8. Select style.css and tick Auto Compile to compile style.css automatically whenever Koala detects a change in it. Have a look at the following screenshot:
    Time for action – integrating project directory into Koala and combining the style sheets
  9. Select the Combine Import option to let Koala combine the content within the style sheets (the content that was included in style.css) with the @import rule. Take a look at the following screenshot:
    Time for action – integrating project directory into Koala and combining the style sheets
  10. Set Output Style: to compress. Take a look at the following screenshot:
    Time for action – integrating project directory into Koala and combining the style sheets

    This will compress the style rules into a single line, which eventually will make the style.css file size smaller.

  11. Click on the Compile button. Take a look at the following screenshot:
    Time for action – integrating project directory into Koala and combining the style sheets

    This will compile style.css and generate a new file named style.min.css as the output.

  12. Open index.html and link style.min.css. using the following code:
    <link href="style.min.css" rel="stylesheet">
What just happened?

We have just integrated the project directory within Koala. We have also created two new style sheets, namely, main.css and style.css. We have also put together five style sheets, including main.css, in the style.css file using the @import rule. We combined these files and generated a new style sheet named style.min.css, which can be found inline with style.css, as shown in the following screenshot:

What just happened?

Finally, we link the minified style sheet, style.min.css, in index.html. Have a go hero – renaming the output

The style.min.css name is the default name set by Koala; it inserts the suffix, min, to every minified output. Though Pop quiz – website performance rules

Q1. Which of the following rules is not the one to improve website performance?

Minifying resources such as CSS and JavaScript.
Compressing image files.
Leveraging browser cache.
Using CSS shorthand properties.
Using CDN to deliver web resources.

Before we get our hands on the code, let's talk about the mobile-first approach that will drive our decision on writing part of the blog style rules.

Mobile-first is one of the buzzwords in the web design community. Mobile-first is a new way of thinking on building websites of today, which also guides the pattern to build websites that are optimized for mobile use. As mentioned in Chapter 1, Responsive Web Design, mobile users are growing and desktop is no longer the main platform where users can access the web.

The mobile-first concept drives us to think and prioritize mobile use on building the website blocks, including how we compose style rules and media queries. In addition, adopting mobile-first thinking, as Brad Frost demonstrated in his blog post (http://bradfrostweb.com/blog/post/7-habits-of-highly-effective-media-queries/), allows producing leaner codes than the other way around (desktop to mobile). Herein, we will first optimize and address the blog for mobile and then enhance to the desktop version afterwards.

Mobile-first is beyond the capacity of this module, we'll make Mobile-first web applications later in Module 3, HTML5 and CSS3 Responsive Web Design Cookbook. The following are some of my recommendation sources to dig into this topic further:

In this section, we are going to write blog base styles. These style rules encompass the content font family, the font size, and a number of elements therein in general.

First of all, it is my personal opinion that using the default system font such as Arial and Times is so boring.

Perform the following steps to compose the base style rules:

  1. To make our blog look more refreshing, we will use a couple of custom fonts from the Google Font library. Google Font has made it easy for us to use fonts on the Web. Google has taken care of the hassle of writing the syntax, as well as ensuring that the font formats are compatible in all major browsers.
  2. In addition, we won't be befuddled with the font license, as Google Font is completely free. All we have to do is add a special style sheet as explained in this page https://developers.google.com/fonts/docs/getting_started#Quick_Start. In our case, add the following link before the prime style sheet link:
    <link href='http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic,700italic|Varela+Round' rel='stylesheet'>

    Upon doing so, we will be able to use the Droid Serif font family, along with Varela Round; see these font specimens and characters in the following web pages:

  3. Set the entire element box sizing to border-box. Add the following line (as well as the other lines in the next steps) in main.css:
    * { 
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      *behavior: url(/scripts/boxsizing.htc);
    }
  4. We are going to set the blog main font, that is, the font that applies to the entire content of the blog. Herein, we will use Droid Serif of Google Font. Add the following style rules after the list of @import style sheet:
    body {
      font-family: "Droid Serif", Georgia, serif;
      font-size: 16px;
    }
  5. We are going to apply a different font family for the headings (h1, h2, h3, h4, h5, and h6) in order to set it apart from the body content. Herein, we will apply the second custom font family that we brought from the Google Font collection, Varela Round.
  6. Add the following line to apply Varela Round to the headings:
    h1, h2, h3, h4, h5, h6 {
        font-family: "Varela Round", Arial, sans-serif;
        font-weight: 400;
    }
  7. In this step, we will also customize the default anchor tag or link styles. It's my personal preference to remove the underline of the default link style.

    Furthermore, we also change the link color to #3498db. It's blue, but subtler than the blue color applied as the default link style, as shown in the following screenshot:

    Time for action – composing the base style rules
  8. Add the following lines to change the default link color:
    a {
        color: #3498db;
        text-decoration: none;
    }
  9. We will set the color of the link to hover state, as well. This color appears when the mouse cursor is over the link. Herein, we set the link hover color to #2a84bf, the darker version of the color we set in step 4. Have a look at the following screenshot:
    Time for action – composing the base style rules
  10. Add the following line to set the color of the link when it is in hover state, as follows:
    a:hover {
        color: #2a84bf;
    }
  11. Make the image fluid with the following style rules, as follows:
    img {
      max-width: 100%;
      height: auto;
    }

In addition, these style rules will prevent the image from exceeding its container when the actual image width is larger than the container.

Please note that the link color, #2a84bf, is my personal selection. There are a number of considerations when choosing a color, such as the brand, the audience, and the content. The link doesn't have to be #2a84bf. The link color in the Starbucks website (http://www.starbucks.co.id/about-us/pressroom), for example, is green, which refers to its brand identity.

So, don't be afraid to explore and try new colors. The following are a few color ideas:

Have a go hero – customizing the link color

Next, we will compose the blog header and navigation style rules. The style rules will mostly be applied through the element's classes. So, before proceeding, please refer to Chapter 2, Web Development Tools, to see the class names and ID that we added in the elements.

What just happened?

We have just added style rules that address some elements in the blog, namely, the headings, the link, and the image. At this stage, there isn't a significant difference yet that appears in the blog, except the font family change in the content and the headings, as well as the link color. Have a look at the following screenshot:

What just happened?

Please note that the link color, #2a84bf, is my personal selection. There are a number of considerations when choosing a color, such as the brand, the audience, and the content. The link doesn't have to be #2a84bf. The link color in the Starbucks website (http://www.starbucks.co.id/about-us/pressroom), for example, is green, which refers to its brand identity.

So, don't be afraid to explore and try new colors. The following are a few color ideas:

Have a go hero – customizing the link color

Next, we will compose the blog header and navigation style rules. The style rules will mostly be applied through the element's classes. So, before proceeding, please refer to Chapter 2, Web Development Tools, to see the class names and ID that we added in the elements.

Have a go hero – customizing the link color

Please note that the link color, #2a84bf, is my personal selection. There are a number of considerations

when choosing a color, such as the brand, the audience, and the content. The link doesn't have to be #2a84bf. The link color in the Starbucks website (http://www.starbucks.co.id/about-us/pressroom), for example, is green, which refers to its brand identity.

So, don't be afraid to explore and try new colors. The following are a few color ideas:

Have a go hero – customizing the link color

Next, we will compose the blog header and navigation style rules. The style rules will mostly be applied through the element's classes. So, before proceeding, please refer to Chapter 2, Web Development Tools, to see the class names and ID that we added in the elements.

The steps are as follows:

  1. Open main.css.
  2. Add some whitespace surrounding the header with padding, and also set the header color to #333, as follows:
    .blog-header {
    padding: 30px 15px;
    background-color: #333;
    }
  3. To make the logo look prominent, we will set it with Varela Round font, which is the same font family we used for the headings. We also make it bigger and transform the letters all to uppercase, as follows:
     .blog-name {
      font-family: "Varela Round", Arial, sans-serif;
      font-weight: 400;
      font-size: 42px;
      text-align: center;
      text-transform: uppercase;
    }
  4. The logo link color currently is #2a84bf, which is the common color we set for links <a>. This color does not suit well with the background color. Let's change the color to white instead, as follows:
    .blog-name a {
        color: #fff;
    }
  5. Set the search input styles, as follows:
    .search-form input {
      height: 36px;
      background-color: #ccc;
      color: #555;
      border: 0;
      padding: 0 10px;
      border-radius: 30px;
    }

    These style rules set the input color, border color, and the background colors. It turns the input into something as shown in the following screenshot:

    Time for action – enhancing the header and the navigation appearance with CSS
  6. As you can see in the preceding screenshot, the placeholder text is barely readable as the color blends with the input background color. So, let's make the text color a bit darker, as follows:
    .search-form input::-webkit-input-placeholder {
      color: #555;
    }
    .search-form input:-moz-placeholder {
      color: #555;  
    }
    .search-form input::-moz-placeholder {
      color: #555;  
    }
    .search-form input:-ms-input-placeholder {  
      color: #555;
    }

    If you use OS X or Ubuntu, you will see the glowing color that highlights the input when it is currently targeted, as shown in the following screenshot:

    Time for action – enhancing the header and the navigation appearance with CSS

    In OS X, the glowing color is blue. In Ubuntu, it will be orange.

  7. I would like to remove this glowing effect. The glowing effect is technically shown through box-shadow. So, to remove this effect, we simply set the input box-shadow to none, as follows:
    .search-form input:focus {
      -webkit-box-shadow: none;
      -moz-box-shadow: none;
      box-shadow: none;
    }

    It's worth noting that the glowing effect is part of the User Experience (UX) design, telling the users that they are currently within the input field. This UX design is particularly helpful if the users were only able to navigate the website with a keyboard.

  8. So, we will have to create an effect that brings a similar UX as a replacement. Herein, we will replace the glowing effect that we removed by lightening the input background color. The following is the complete code of this step:
    .search-form input:focus {
      -webkit-box-shadow: none;
      -moz-box-shadow: none;
      box-shadow: none;
      background-color: #bbb;
    }

    The input background color becomes lighter when it is in focus, as shown in the following screenshot:

    Time for action – enhancing the header and the navigation appearance with CSS
  9. We will write the style for the navigation. First, align the menu to the center, and add some whitespace at the top and the bottom of the navigation with the margin. Have a look at the following code:
    .blog-menu {
      margin: 30px 0;
      text-align: center;
    }
  10. Remove the left-hand side padding of <ul>, as follows:
    .blog-menu ul {
      padding-left: 0;
    }
  11. Add some whitespace between the menus with a margin, and remove the list bullet, as follows:
    .blog-menu li {
      margin: 15px;
      list-style-type: none;
    }
  12. Customize the menu color and font, as follows:
    .blog-menu a {
      color: #7f8c8d;
      font-size: 18px;
       text-transform: uppercase;
       font-family: "Varela Round", Arial, sans-serif;
    }
    .blog-menu a:hover {
        color: #3498db;
    }
What just happened?

We have just Have a go hero – customizing the header

The blog header is given a dark color, #333. I truly understand that this color may look boring to some of you. Hence, freely customize the color as well as the style of the logo and the search input field. Some

Perform the following steps to style the blog content:

  1. Add whitespace on all sides of the content section with padding and margin, as follows
    .blog-content {
      padding: 15px;
      margin-bottom: 30px;
    } 
  2. Separate each blog post with some whitespace and borderline, as follows:
    .post {
      margin-bottom: 60px;
      padding-bottom: 60px;
      border-bottom: 1px solid #ddd;
    }
  3. Align the title to the center, adjust the title font size a little, and change the color with the following style rules:
    .post-title {
      font-size: 36px;
      text-align: center;
      margin-top: 0;
    }
    .post-title a {
      color: #333;
    }
    .post-title a:hover {
      color: #3498db;
    }
  4. Below the title, we have post-meta, which consists of the post author name and the post publishing date. Similar to the title, we also adjust the font size and the whitespace, and change the font color, as follows:
    .post-meta {
      font-size: 18px;
      margin: 20px 0 0;
      text-align: center;
      color: #999;
    }
    .post-meta ul {
      list-style-type: none;
      padding-left: 0;
    }
    .post-meta li {
      margin-bottom: 10px;
    }
  5. The post thumbnail, as you can see in the following screenshot, looks small and squished due to the margin on all its sides:
    Time for action – enhancing the content section appearance with CSS
  6. Let's remove these margins, as follows:
    .post-thumbnail {
      margin: 0;
    }

    Some of the images, as shown in the following screenshot, have a caption:

    Time for action – enhancing the content section appearance with CSS
  7. Let's style it to make it look distinctive from the rest of the content and also show that it is an image caption. Add the following lines of code to style the caption:
    .post-thumbnail figcaption {
      color: #bdc3c7;
      margin-top: 15px;
      font-size: 16px;
      font-style: italic;
    }
  8. Adjust the post excerpt font size, color, and line height, as follows:
    .post-excerpt {
      color: #555;
      font-size: 18px;
      line-height: 30px;
    }
  9. Starting in this step, we will write the style of the blog pagination. First, let's make some adjustments to the font size, the font family, the whitespace, the position, and the alignment, as shown in the following code:
    .blog-pagination {
      text-align: center;
      font-size: 16px;
      position: relative;
      margin: 60px 0;
    }
    .blog-pagination ul {
      padding-left: 0;
    }
    .blog-pagination li,
    .blog-pagination a {
      display: block;
      width: 100%;
    }
    .blog-pagination li {
      font-family: "Varela Round", Arial, sans-serif;
      color: #bdc3c7;
      text-transform: uppercase;
      margin-bottom: 10px;
    }
  10. Decorate the pagination link with rounded corner borders, as follows:
    .blog-pagination a {
      -webkit-border-radius: 30px;
      -moz-border-radius: 30px;
      border-radius: 30px;
      color: #7f8c8d;
      padding: 15px 30px;
      border: 1px solid #bdc3c7;
    }
  11. Specify the link decoration when the mouse cursor hovers over the links, as follows:
    .blog-pagination a:hover {
      color: #fff;
      background-color: #7f8c8d;
      border: 1px solid #7f8c8d;
    }
  12. Finally, place the page number indicator at the top of the pagination links with the following style rules:
    .blog-pagination .pageof {
      position: absolute;
      top: -30px;
    }
What just happened?

We just styled the blog Have a go hero – improving the content section

Most of the style rules we applied in the content section are decorative. It's something that you don't have to follow What just happened?

We have just

Perform the following steps to compose style rules for desktop:

We will add all the style rules in the following steps within this media query. This media query specification will apply the style rules within the viewport width starting from 640 px and up.

What just happened?

We have just added style rules that address the blog for the desktop view. If you are now viewing the blog in the viewport width that is larger than 640 px, you should find that the elements in the blog

Perform the steps to patch Internet Explorer with polyfills:

What just happened?

We just applied polyfills in the blog to patch Internet Explorer rendering issues with HTML5 and Media Have a go hero – polish the blog for Internet Explorer

We will end this project. But, as you can see from the preceding screenshot, there are still many things to address