HTML5 and CSS3: Building Responsive Websites
()
About this ebook
Related to HTML5 and CSS3
Related ebooks
Responsive Web Design with HTML5 and CSS3 - Second Edition Rating: 4 out of 5 stars4/5Responsive Web Design with HTML5 and CSS3 Rating: 4 out of 5 stars4/5Mastering Responsive Web Design with HTML5 and CSS3 Rating: 0 out of 5 stars0 ratingsBuilding Websites All-in-One For Dummies Rating: 4 out of 5 stars4/5Mastering Bootstrap 4 Rating: 5 out of 5 stars5/5Web Design All-in-One For Dummies Rating: 3 out of 5 stars3/5Responsive Web Design by Example : Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsWordPress Web Application Development Rating: 5 out of 5 stars5/5Sass and Compass for Designers Rating: 0 out of 5 stars0 ratingsLearning Bootstrap 4 - Second Edition Rating: 5 out of 5 stars5/5Bootstrap 4 Site Blueprints Rating: 5 out of 5 stars5/5Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications, and libraries Rating: 3 out of 5 stars3/5Responsive Web Design with HTML5 and CSS3 Essentials Rating: 5 out of 5 stars5/5Object-Oriented JavaScript Rating: 4 out of 5 stars4/5Professional CSS3 Rating: 5 out of 5 stars5/5Mastering JavaScript Rating: 4 out of 5 stars4/5HTML5 & CSS3 For Beginners: Your Guide To Easily Learn HTML5 & CSS3 Programming in 7 Days Rating: 4 out of 5 stars4/5Professional JavaScript for Web Developers Rating: 0 out of 5 stars0 ratingsHTML5, JavaScript, and jQuery 24-Hour Trainer Rating: 2 out of 5 stars2/5JavaScript Unlocked Rating: 5 out of 5 stars5/5The CSS Guide: The Complete Guide to Modern CSS Rating: 5 out of 5 stars5/5CSS in Depth Rating: 2 out of 5 stars2/5Learning JavaScript Data Structures and Algorithms Rating: 5 out of 5 stars5/5JavaScript and AJAX For Dummies Rating: 4 out of 5 stars4/5Object-Oriented JavaScript - Third Edition Rating: 4 out of 5 stars4/5HTML, CSS, and JavaScript Mobile Development For Dummies Rating: 4 out of 5 stars4/550 Recipes for Programming CSS3 Rating: 0 out of 5 stars0 ratingsWeb Design Playground: HTML & CSS The Interactive Way Rating: 5 out of 5 stars5/5
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn Algorithmic Trading: Build and deploy algorithmic trading systems and strategies using Python and advanced data analysis Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Deep Learning For Dummies Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Learn Python in 10 Minutes Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python for Finance Cookbook: Over 50 recipes for applying modern Python libraries to financial data analysis Rating: 0 out of 5 stars0 ratingsHow To Become A Data Scientist With ChatGPT: A Beginner's Guide to ChatGPT-Assisted Programming Rating: 4 out of 5 stars4/5Clean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5JavaScript Enlightenment Rating: 4 out of 5 stars4/5Mastering C# and .NET Framework Rating: 5 out of 5 stars5/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsEthical Hacking Rating: 4 out of 5 stars4/5Accelerated DevOps with AI, ML & RPA: Non-Programmer’s Guide to AIOPS & MLOPS Rating: 5 out of 5 stars5/5Git Essentials Rating: 4 out of 5 stars4/5
Reviews for HTML5 and CSS3
0 ratings0 reviews
Book preview
HTML5 and CSS3 - Thoriq Firdaus
Table of Contents
HTML5 and CSS3: Building Responsive Websites
HTML5 and CSS3: Building Responsive Websites
Credits
Preface
What this learning path covers
What you need for this learning path
Who this learning path is for
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Module 1
1. Responsive Web Design
Responsive web design in a nutshell
Viewport meta tag
A word on screen size and viewport
Media queries
A look into responsive frameworks
The Responsive.gs framework
A word on CSS box model
CSS3 box sizing
The Bootstrap framework
The Foundation framework
A brief introduction to CSS preprocessors
Have a Go Hero — delve into responsive web design
Pop Quiz — responsive web design main components
Responsive web design inspiration sources
Summary
2. Web Development Tools
Choosing a code editor
Sublime Text Package Control
Time for action – installing Sublime Text Package Control
What just happened?
Have a go hero – install the LESS and Sass syntax-highlighting package
Setting up a local server
Time for action – installing XAMPP
What just happened?
Choosing a CSS preprocessor compiler
Browser for development
Source maps
Mobile emulator
Managing project dependency with Bower
Time for action – installing Node.js
What just happened?
Have a go hero – get yourself familiar with command lines
Time for action – installing Bower
What just happened?
Bower commands
Pop quiz – web development tools and command lines
Summary
3. Constructing a Simple Responsive Blog with Responsive.gs
Responsive.gs components
The classes
Using HTML5 elements for semantic markups
HTML5 search input types
HTML5 placeholder attribute
HTML5 in Internet Explorer
A look into polyfills in the Responsive.gs package
Box sizing polyfills
CSS3 media queries polyfill
Examining the blog's wireframe
Organizing project directories and files
Time for action – creating and organizing project directories and assets
What just happened?
Have a go hero – making the directory structure more organized
Pop quiz – using polyfill
The blog HTML structures
Time for action – constructing the blog
What just happened?
Have a go hero – creating more blog pages
Pop quiz – HTML5 elements
Summary
4. Enhancing the Blog Appearance
Using CSS3
Creating rounded corners with CSS3
Creating drop shadow
CSS3 browser supports and the use of vendor prefix
Customizing to placeholder text styles
Using CSS libraries
Working with Koala
Time for action – integrating project directory into Koala and combining the style sheets
What just happened?
Have a go hero – renaming the output
Pop quiz – website performance rules
Thinking mobile first
Composing the blog styles
Time for action – composing the base style rules
What just happened?
Have a go hero – customizing the link color
Time for action – enhancing the header and the navigation appearance with CSS
What just happened?
Have a go hero – customizing the header
Time for action – enhancing the content section appearance with CSS
What just happened?
Have a go hero – improving the content section
Time for action – enhancing the footer section appearance with CSS
What just happened?
Optimize the blog for desktop
Time for action – composing style rules for desktop
What just happened?
Making Internet Explorer more capable with polyfills
Time for action – patch Internet Explorer with polyfills
What just happened?
Have a go hero – polish the blog for Internet Explorer
Summary
5. Developing a Portfolio Website with Bootstrap
The Bootstrap components
The Bootstrap responsive grid
Bootstrap buttons and forms
Bootstrap Jumbotron
Bootstrap third-party extensions
Jasny Bootstrap off-canvas
Digging into Bootstrap
Using font icons
Examining the portfolio website layout
Project directories, assets, and dependencies
Time for action – organizing project directories, assets, and installing project dependencies with Bower
What just happened?
Have a go hero – specifying Bower custom directory
Pop quiz – test your understanding on Bower commands
Updating Bower components
The portfolio website HTML structure
Time for action – building the website HTML structure
What just happened?
Have a go hero – extending the portfolio website
Pop quiz – Bootstrap button classes
Summary
6. Polishing the Responsive Portfolio Website with LESS
Basic LESS syntax
Variables
Nesting style rules
Mixins
Parametric mixins
Specify a default value in a parametric mixin
Merging mixins with extend syntax
Generating value with mathematical operations
Generating color with mathematical operations and LESS functions
Referential import
Using a variable in an import statement
Using source map for easier style debugging
More on LESS
External style sheet references
Time for action – creating style sheets and organizing external style sheet references
What just happened?
Have a go hero – name and organize the style sheets
Pop quiz – which of the following option is not LESS Import option?
Working with Koala
Time for action – compiling LESS into CSS using Koala
What just happened?
Polishing the portfolio website with LESS
Time for action – composing the website styles with LESS syntax
What just happened?
Have a go hero – being more creative
Pop quiz — using LESS function and extend syntax
Improve and make the website functioning with JavaScript
Time for action – compiling JavaScript with Koala
What just happened?
Summary
7. A Responsive Website for Business with Foundation
Examining the website layout
A look into Foundation
The grid system
The buttons
The navigation and top bar
The pricing tables
Moving around Orbit
How is Orbit constructed?
Adding add-ons, the font Icons
Further on Foundation
Additional required assets
The project directories, assets, and dependencies
Time for action – organizing the project directories, assets, and dependencies
What just happened?
Time for action – building the website's HTML structure
What just happened?
Summary
8. Extending Foundation
Syntactically Awesome Style Sheets
Nesting rules
Storing a value with a variable
Variable interpolation
Reusable code block with mixins
A brief on the Sass mixin library
Creating and using a Sass function
Manipulating color with Sass functions
Useful Foundation's own function
Em and Rem
Have a go hero – diving into Sass
Pop quiz – multiple parameters in Sass function
Pop quiz – Sass color manipulation
Project recap
Style sheet organizations
Time for action – organizing and compiling style sheets
What just happened?
The website's look and feel
Time for action – build on the website
What just happened?
Have a go hero – colors and creativities
Pop quiz – importing an external Sass style sheet
Fine-tuning the website
Time for action – compiling JavaScript and styling the website with media queries
What just happened?
Have a go hero – remove unnecessary Foundation components
Summary
A. Pop Quiz Answers
Chapter 1, Responsive Web Design
Pop quiz – responsive web design main components
Chapter 2, Web Development Tools
Pop quiz – web development tools and command lines
Chapter 3, Constructing a Simple Respons3ive Blog with Responsive.gs
Pop quiz – using polyfill
Pop quiz – HTML5 elements
Chapter 4, Enhancing the Blog Appearance
Pop quiz – website performance rules
Chapter 5, Developing a Portfolio Website with Bootstrap
Pop quiz – test your understanding on Bower commands
Pop quiz – Bootstrap button classes
Chapter 6, Polishing the Responsive Portfolio Website with LESS
Pop quiz – which of the following option is not LESS Import option?
Pop quiz – using LESS function and extend syntax
Chapter 8, Extend Foundation
Pop quiz – multiple parameters in Sass function
Pop quiz – Sass color manipulation
Pop quiz – importing external Sass style sheet
2. Module 2
1. The Essentials of Responsive Web Design
Beginning our quest
Defining responsive web design
Setting browser support levels
A brief note on tooling and text editors
Our first responsive example
Our basic HTML file
Taming images
Enter media queries
Amending the example for a larger screen
The shortcomings of our example
Summary
2. Media Queries – Supporting Differing Viewports
Why media queries are needed for a responsive web design
Basic conditional logic in CSS
Media query syntax
Media queries in link tags
Combining media queries
Media queries with @import
Media queries in CSS
What can media queries test for?
Using media queries to alter a design
Any CSS can be wrapped in a media query
Media queries for HiDPI devices
Considerations for organizing and authoring media queries
Linking to different CSS files with media queries
The practicalities of separating media queries
Nesting media queries 'inline'
Combine media queries or write them where it suits?
The viewport meta tag
Media Queries Level 4
Scripting media feature
Interaction media features
The hover media feature
Environment media features
Summary
3. Fluid Layouts and Responsive Images
Converting a fixed pixel design to a fluid proportional layout
Why do we need Flexbox?
Inline block and whitespace
Floats
Table and table-cell
Introducing Flexbox
The bumpy path to Flexbox
Browser support for Flexbox
Leave prefixing to someone else
Choosing your auto-prefixing solution
Getting Flexy
Perfect vertically centered text
Offset items
Reverse the order of items
How about if we want them laid out vertically instead?
Column reverse
Different Flexbox layouts inside different media queries
Inline-flex
Flexbox alignment properties
The align-items property
The align-self property
Possible alignment values
The justify-content property
The flex property
Simple sticky footer
Changing source order
Wrapping up Flexbox
Responsive images
The intrinsic problem of responsive images
Simple resolution switching with srcset
Advanced switching with srcset and sizes
Did you say the browser 'might' pick one image over another?
Art direction with the picture element
Facilitate new-fangled image formats
Summary
4. HTML5 for Responsive Web Designs
HTML5 markup – understood by all modern browsers
Starting an HTML5 page the right way
The doctype
The HTML tag and lang attribute
Specifying alternate languages
Character encoding
Easy-going HTML5
A sensible approach to HTML5 markup
All hail the mighty tag
New semantic elements in HTML5
The
The
The
The
The
The
The elements
The
The
The element
A note on h1-h6 elements
HTML5 text-level semantics
The element
The element
The element
Obsolete HTML features
Putting HTML5 elements to use
Applying common sense to your element selection
WCAG and WAI-ARIA for more accessible web applications
WCAG
WAI-ARIA
Don't use roles for semantic elements
If you only remember one thing
Taking ARIA further
Embedding media in HTML5
Adding video and audio the HTML5 way
Fallback capability for older browsers
Audio and video tags work almost identically
Responsive HTML5 video and iFrames
A note about 'offline first'
Summary
5. CSS3 – Selectors, Typography, Color Modes, and New Features
No one knows it all
Anatomy of a CSS rule
Quick and useful CSS tricks
CSS multi-column layouts for responsive designs
Fixed columns, variable width
Adding a gap and column divider
Word wrapping
Text ellipsis
Creating horizontal scrolling panels
Facilitating feature forks in CSS
Feature queries
Combining conditionals
Modernizr
Feature detection with Modernizr
New CSS3 selectors and how to use them
CSS3 attribute selectors
CSS3 substring matching attribute selectors
The 'beginning with' substring matching attribute selector
The 'contains an instance of' substring matching attribute selector
The 'ends with' substring matching attribute selector
Gotchas with attribute selection
Attribute selectors allow you to select IDs and classes that start with numbers
CSS3 structural pseudo-classes
The :last-child selector
The nth-child selectors
Understanding what nth rules do
Breaking down the math
nth-based selection in responsive web designs
The negation (:not) selector
The empty (:empty) selector
Do something with the :first-line regardless of viewport
CSS custom properties and variables
CSS calc
CSS Level 4 selectors
The :has pseudo class
Responsive viewport-percentage lengths (vmax, vmin, vh, vw)
Web typography
The @font-face CSS rule
Implementing web fonts with @font-face
A note about custom @font-face typography and responsive designs
New CSS3 color formats and alpha transparency
RGB color
HSL color
Alpha channels
Color manipulation with CSS Color Module Level 4
Summary
6. Stunning Aesthetics with CSS3
Text shadows with CSS3
Omitting the blur value when not needed
Multiple text shadows
Box shadows
An inset shadow
Multiple shadows
Understanding spread
Background gradients
The linear-gradient notation
Specifying gradient direction
Color stops
Adding fallback for older browsers
Radial background gradients
Breakdown of the radial-gradient syntax
Handy 'extent' keywords for responsive sizing
Repeating gradients
Background gradient patterns
Multiple background images
Background size
Background position
Background shorthand
High-resolution background images
CSS filters
Available CSS filters
Combining CSS filters
A warning on CSS performance
A note on CSS masks and clipping
Summary
7. Using SVGs for Resolution Independence
A brief history of SVG
The graphic that is a document
The root SVG element
Namespace
The title and desc tags
The defs tag
The g element
SVG shapes
SVG paths
Creating SVGs with popular image editing packages and services
Save time with SVG icon services
Inserting SVGs into your web pages
Using an img tag
Using an object tag
Insert an SVG as a background image
A brief aside on data URIs
Generating image sprites
Inserting an SVG inline
Re-using graphical objects from symbols
Inline SVGs allow different colors in different contexts
Make dual-tone icons that inherit the color of their parent
Re-using graphical objects from external sources
What you can do with each SVG insertion method (inline, object, background-image, and img)
Browser schisms
Extra SVG capabilities and oddities
SMIL animation
The end of SMIL
Styling an SVG with an external style sheet
Styling an SVG with internal styles
SVG properties and values within CSS
Animate an SVG with CSS
Animating SVG with JavaScript
A simple example of animating an SVG with GreenSock
Optimising SVGs
Using SVGs as filters
A note on media queries inside SVGs
Implementation tips
Further resources
Summary
8. Transitions, Transformations, and Animations
What CSS3 transitions are and how we can use them
The properties of a transition
The transition shorthand property
Transition different properties over different periods of time
Understanding timing functions
Fun transitions for responsive websites
CSS3 2D transforms
Scale
Translate
Using translate to center absolutely positioned elements
Rotate
Skew
Matrix
Matrix transformations for cheats and dunces
The transform-origin property
CSS3 3D transformations
The transform3d property
Use transforms with progressive enhancement
Animating with CSS3
The animation-fill-mode property
Summary
9. Conquer Forms with HTML5 and CSS3
HTML5 forms
Understanding the component parts of HTML5 forms
placeholder
Styling the placeholder text
required
autofocus
autocomplete
List and the associated datalist element
HTML5 input types
number
min and max ranges
Changing the step increments
url
tel
search
pattern
color
Date and time inputs
date
month
week
time
range
How to polyfill non-supporting browsers
Styling HTML5 forms with CSS3
Indicating required fields
Creating a background fill effect
Summary
10. Approaching a Responsive Web Design
Get designs in the browser as soon as possible
Let the design dictate the breakpoints
View and use the design on real devices
Embracing progressive enhancement
Defining a browser support matrix
Functional parity, not aesthetic parity
Choosing the browsers to support
Tiering the user experience
Practically delivering experience tiers
Linking CSS breakpoints to JavaScript
Avoid CSS frameworks in production
Coding pragmatic solutions
When a link becomes a button
Use the simplest code possible
Hiding, showing, and loading content across viewports
Let CSS do the (visual) heavy lifting
Validators and linting tools
Performance
The next big things
Summary
3. Module 3
1. Responsive Elements and Media
Introduction
Resizing an image using percent width
Getting ready
How to do it…
How it works…
See also
Responsive images using the cookie and JavaScript
Getting ready
How to do it…
How it works…
Making your video respond to your screen width
Getting ready
How to do it…
How it works…
Resizing an image using media queries
Getting ready
How to do it…
How it works...
Changing your navigation with media queries
Getting ready
How to do it…
How it works…
Making a responsive padding based on size
Getting ready
How to do it…
How it works…
Making a CSS3 button glow for a loading element
Getting ready
How to do it…
How it works…
2. Responsive Typography
Introduction
Creating fluid, responsive typography
Getting ready
How to do it...
How it works...
See also
Making a text shadow with canvas
Getting ready
How to do it...
How it works…
See also
Making an inner and outer shadow with canvas
Getting ready
How to do it…
How it works…
Rotating your text with canvas
Getting ready
How to do it…
How it works…
See also
Rotating your text with CSS3
Getting ready
How to do it…
How it works…
See also
Making 3D text with CSS3
Getting ready
How to do it…
How it works…
Adding texture to your text with text masking
Getting ready
How to do it…
How it works…
Styling alternating rows with the nth positional pseudo class
Getting ready
How to do it…
How it works…
Adding characters before and after pseudo elements
Getting ready
How to do it…
How it works…
Making a button with a relative font size
Getting ready
How to do it…
How it works…
Adding a shadow to your font
Getting ready
How to do it…
How it works...
Curving a corner with border radius
Getting ready
How to do it…
How it works…
3. Responsive Layout
Introduction
Responsive layout with the min-width and max-width properties
Getting ready
How to do it…
How it works…
Controlling your layout with relative padding
Getting ready
How to do it…
How it works…
Adding a media query to your CSS
Getting ready
How to do it…
How it works…
Creating a responsive width layout with media queries
Getting ready
How to do it…
How it works…
Changing image sizes with media queries
Getting ready
How to do it…
How it works…
Hiding an element with media queries
Getting ready
How to do it…
How it works…
Making a smoothly transitioning responsive layout
Getting ready
How to do it...
How it works...
There's more...
4. Using Responsive Frameworks
Introduction
Using the Fluid 960 grid layout
Getting ready
How to do it...
How it works...
Using the Blueprint grid layout
Getting ready
How to do it....
How it works...
Fluid layout using the rule of thirds
Getting ready
How to do it…
How it works...
There's more...
Trying Gumby, a responsive 960 grid
Getting ready
How to do it…
How it works...
The Bootstrap framework makes responsive layouts easy
Getting ready
How it works...
There's more...
5. Making Mobile-first Web Applications
Introduction
Using the Safari Developer Tools' User Agent switcher
Getting ready
How to do it…
How it works...
Masking your user agent in Chrome with a plugin
Getting ready
How to do it...
How it works...
There's more...
Using browser resizing plugins
Getting ready
How to do it...
How it works...
Learning the viewport and its options
Getting ready
How to do it…
How it works...
Adding tags for jQuery Mobile
Getting ready
How to do it...
How it works...
Adding a second page in jQuery Mobile
Getting ready
How to do it...
How it works...
Making a list element in jQuery Mobile
Getting ready
How to do it...
How it works...
Adding a mobile, native-looking button with jQuery Mobile
Getting ready
How to do it...
How it works...
Adding a mobile stylesheet for mobile browsers only using the media query
Getting ready
How to do it...
How it works...
Adding JavaScript for mobile browsers only
Getting ready
How to do it...
How it works...
6. Optimizing Responsive Content
Introduction
Responsive testing using IE's Developer Tools
Getting ready
How to do it...
How it works...
There's more...
Browser testing – using plugins
Getting ready
How to do it...
Development environments – getting a free IDE
Getting ready
How to do it…
How it works...
Virtualization – downloading VirtualBox
Getting ready
How to do it...
How it works…
Getting a browser resizer for Chrome
Getting ready
How to do it...
How it works...
7. Unobtrusive JavaScript
Introduction
Writing Hello World
unobtrusively
Getting ready
How to do it...
How it works...
Creating a glowing submit
button with the event listener
Getting ready
How to do it...
How it works...
Making a button stand out when you hover over it
Getting ready
How to do it...
How it works...
Resizing an element with unobtrusive jQuery
Getting ready
How to do it...
How it works...
Masking a password with unobtrusive JavaScript
Getting ready
How to do it...
How it works...
Using an event listener to animate an image shadow
Getting ready
How to do it...
How it works...
B. Bibliography
Index
HTML5 and CSS3: Building Responsive Websites
HTML5 and CSS3: Building Responsive Websites
Design robust, powerful, and above all, modern websites across all manner of devices with ease using HTML5 and CSS3
A course in three modules
BIRMINGHAM - MUMBAI
HTML5 and CSS3: Building Responsive Websites
Copyright © 2016 Packt Publishing
All rights reserved. No part of this course may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this course to ensure the accuracy of the information presented. However, the information contained in this course is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this course.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this course by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Published on: October 2016
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78712-481-3
www.packtpub.com
Credits
Authors
Thoriq Firdaus
Ben Frain
Benjamin LaGrone
Reviewers
Saumya Dwivedi
Gabriel Hilal
Joydip Kanjilal
Anirudh Prabhu
Taroon Tyagi
Esteban S. Abait
Christopher Scott Hernandez
Mauvis Ledford
Sophie Williams
Dale Cruse
Ed Henderson
Rokesh Jankie
Content Development Editor
Amedh Pohad
Graphics
Kirk D’Penha
Production Coordinator
Deepika Naik
Preface
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.
What this learning path covers
Module 1, Responsive Web Design Beginner’s Guide, is a step-by-step beginner’s guide, where you will learn to build engaging responsive 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. You’ll also find out which framework works best for your project specifications. The module teaches you how to build presentable, responsive websites through examples, tips, and best practices of code writing and project organization. Additionally, you will also learn how to use CSS preprocessors, LESS, and Sass, which allows you to compose leaner style rules.
Module 2, Responsive Web Design with HTML5 and CSS3, is packed with examples, and a thorough explanation of modern techniques and syntax, it 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 module covers every essential aspect of responsive web design. In addition, it extends the responsive design methodology by applying the latest and most useful techniques provided by HTML5 and CSS3, making designs leaner and more maintainable than ever before. It also explains common best practice methods of writing and delivering code, images, and files.
Module 3, HTML5 and CSS3 Responsive Web Design Cookbook, 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. The topics in this module include responsive elements and media, responsive typography, responsive layouts, using media queries, utilizing modern responsive frameworks, developing mobile-first web applications, optimizing responsive content, and achieving unobtrusive interaction using JavaScript and jQuery. Each recipe features actual lines of code that you can apply.
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.
What you need for this learning path
Module 1:
You need to have a basic understanding of HTML and CSS; at least, you should know what an HTML element is and how to style an HTML element with CSS in its fundamental form. Some degree of familiarity and experience with HTML5, CSS3, and command lines, though not essential, will be a great help to get the most out of this module. We will explain each step and all the techniques in full, along with some handy tips and references.
Furthermore, you will also need a computer running Windows, OS X, or Ubuntu; an Internet browser (preferably Google Chrome or Mozilla Firefox); and a code editor (in this module, we will use Sublime Text).
Module 2:
A text editor
An evergreen browser
Module 3:
You will need an IDE (Integrated Development Environment); NetBeans or Eclipse is recommended (there are instructions on how to get one inside), image editing software such as Photoshop or GIMP, a web host, and a local web server such as Apache or a local hosting application such as XAMPP or MAMPP.
Who this learning path is for
This course is for web developers who are familiar with HTML and CSS but want to understand the essentials of responsive web design. It is for those developers who are willing to seek innovative techniques that deliver fast, intuitive interfacing with the latest mobile Internet devices.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this course—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the course’s title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a course, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt course, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this course from your account at http://www.packtpub.com. If you purchased this course elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the course in the Search box.
Select the course for which you’re looking to download the code files.
Choose from the drop-down menu where you purchased this course from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the course’s webpage at the Packt Publishing website. This page can be accessed by entering the course’s name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the course is also hosted on GitHub at https://github.com/PacktPublishing/HTML5-and-CSS3-Building-Responsive-Websites. We also have other code bundles from our rich catalog of books, courses and videos available at https://github.com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our courses—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this course. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your course, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the course in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this course, you can contact us at <[email protected]>, and we will do our best to address the problem.
Part 1. Module 1
Responsive Web Design Beginner's Guide
Build powerful and engaging responsive websites with ease
Chapter 1. Responsive Web Design
I still remember, back when I was a kid, a mobile phone came with a mere tiny size monochromatic screen. All we could do at that time was make a phone call, text, and play a simple game. Today, mobile devices have drastically advanced in many ways.
New mobile devices are built with varying screen sizes; some even come with higher DPI or resolution. Most new mobile devices are now equipped with a touch-enabled screen, allowing us to interact with the device conveniently using a tap or a swipe of fingers. The screen orientation is switchable between portrait and landscape. The software is also more capable compared to older devices. The mobile browser, in particular, is now able to render and display web pages that are as good as a browser in a desktop computer.
In addition, the number of mobile users has exploded in the last couple of years. We can now see many people around spending countless hours facing their mobile devices, a phone, or a tablet, doing things such as running their businesses on the go or simple Internet browsing. The number of mobile users is likely to grow in the years to come and may even outnumber the total number of desktop users.
That is to say, mobiles have changed the Web and changed the way people use the Internet and enjoy websites. These advancements in mobile devices and the increasing mobile Internet usage prompts questions on a new paradigm to build websites that are accessible and function well in varying circumstances. This is where Responsive Web Design comes in.
In this chapter, we will cover the following topics:
Glance at the basics of responsive web design, viewport meta tag, and CSS3 media queries
Take a look at the responsive frameworks that we will use to build responsive websites in the following chapters
Responsive web design in a nutshell
Responsive web design is one of the most discussed topics in the web design and development community. So, I believe many of you have heard about it to a certain extent.
Ethan Marcotte was the one who coined the term responsive web design
. He suggests in his article Responsive Web Design (http://alistapart.com/article/responsive-web-design/), that the Web should seamlessly adjust and adapt to the environment where the users view the website rather than addressing it exclusively for a specific platform. In other words, the website should be responsive, it should be presentable on any screen size, regardless of the platform on which it is viewed.
Take the Time website (http://time.com/) as an example. The web page fits nicely on a desktop browser with a large screen size and also on a mobile browser with a limited viewable area. The layout shifts and adapts as the viewport size changes. As you can see in the following screenshot, on the mobile browser, the background color of the header is dark grey, the image is scaled down proportionally, and the Tap bar appears where Time hides the latest news, magazine, and videos sections:
There are two components to build responsive websites, namely, viewport meta tag and media queries.
Viewport meta tag
Before smartphones, such as the iPhone, became mainstream, every website was built to be around 1000 px in width or 980 px wide and it was zoomed out to fit into the mobile phone screen, which eventually made the website unreadable. Hence, the viewport> was created.
In a nutshell, the viewport meta tag is used to define the web page scale and its visible area (viewport) in the browser. The following code is an example of a viewport meta tag in action:
viewport content=width=device-width, initial-scale=1
>
The preceding viewport meta tag specification defines the web page viewport width to follow the device. It also defines the web page scale upon opening the web page for the first time at 1:1, in a way that the sizes and the dimensions of the web page content should be persistent; they should not be scaled up or scaled down.
In favor of comprehending how the viewport meta tag would affect a web page layout, I have created two web pages for comparison; one with the viewport meta tag added in and the other one without it. You can see the difference in the following screenshot:
The first website shown in the preceding image is issued with the viewport meta tag using the exact same specification as in our previous code example. As we have specified width=device-width, the browser acknowledges that the website viewport is at the same size as the device screen, so that it will not squish the web page to fit in the entire screen. The initial-scale=1 will retain the title and the paragraph in their original size.
In the second website's example, as we did not add the viewport meta tag, the browser assumed that the web page should be displayed entirely. So, the browser forced the whole website down to fit within the whole screen area, making the title and the text totally unreadable.
A word on screen size and viewport
You may have found on many web design forums or blogs that viewport and screen size are mentioned interchangeably quite often. But, as a matter of fact, they are two different things.
Screen size refers to the device's actual screen size. A 13-inch laptop, for example, commonly has a screen size of 1280*800 pixels. The viewport, on the other hand, describes the viewable area in the browser where it displays websites. The following diagram illustrates this:
Media queries
The media types module in CSS enables us to target style rules to specific media. If you have created a print style sheet before, you certainly are familiar with the concept of media types. CSS3 introduced a new media type called media queries, which allow us to apply styles within the specified range of the viewport width, also known as breakpoints.
The following is one simple example; we decrease the p font size from 16px to 14px of the website when the website's viewport size is at 480px or lower.
p {
font-size: 16px;
}
@media screen and (max-width: 480px) {
p {
font-size: 14px;
}
}
The following diagram illustrates the preceding code:
We can also combine multiple ranges of viewport widths by using the and operator. Following our preceding example, we can set the p font size to 14px when the viewport size is between 480px and 320px in the following manner:
@media screen and (min-width: 320px) and (max-width: 480px) {
p {
font-size: 11px;
}
}
Note
Viewport and media queries
We will be dealing with viewport meta tag and media queries while building responsive websites in Module 2, Responsive Web Design with HTML5 and CSS3, which covers these two in greater detail.
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 (http://960.gs/), which is 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 (http://jquery-ui-bootstrap.github.io/jquery-ui-bootstrap/) is perhaps a 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 module, 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 (http://responsive.gs/) is a lightweight, responsive framework, which is merely 1 KB in size when compressed. Responsive.gs is based on a width of 940 px, and made in three 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
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 {
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 module, 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 module, 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 (http://getbootstrap.com/) was originally built by Mark Otto (http://markdotto.com/) and initially 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 module, 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 (http://foundation.zurb.com/) is a framework created 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 module; 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 (http://lesscss.org/)—though the official support for Sass has just been released recently. Foundation, on the contrary, uses 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%);
}
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:
The Responsive Web Design article by Ethan Martcotte (http://alistapart.com/article/responsive-web-design), is where it all begins
Also a good place to start is Responsive Web Design by Rachel Shillcock (http://webdesign.tutsplus.com/articles/responsive-web-design--webdesign-15155)
Don't Forget the Viewport Meta Tag by Ian Yates (http://webdesign.tutsplus.com/articles/quick-tip-dont-forget-the-viewport-meta-tag--webdesign-5972)
How To Use CSS3 Media Queries To Create a Mobile Version of Your Website by Rachel Andrew (http://www.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/)
Read about the future standards on responsive image using HTML5 Picture Element Responsive Images Done Right: A Guide To by Eric Portis (http://www.smashingmagazine.com/2014/05/14/responsive-images-done-right-guide-picture-srcset/)
A roundup of methods to make data table responsive (http://css-tricks.com/responsive-data-table-roundup/)
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?
Viewport Meta Tag and CSS3 Media Queries.
Fluid grids, flexible images, and media queries.
Responsive images, breakpoints, and polyfills.
Q2. What is a viewport?
The screen size of the device.
The region where the web page is rendered.
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?
@media (max-width: 320px) { p{ font-size:11px; }}
@media screen and (max-device-ratio: 320px) { div{ color:white; }}
stylesheet media=(max-width: 320px)
href=core.css
/>
Responsive web design inspiration sources
Now, before we jump into the next chapters and start building responsive websites, it may be a good idea to spend some time looking for ideas and inspiration for responsive websites; to see how they are built and how the layout is organized on desktop browsers, as well as on mobile browsers.
It's a common thing for websites to be redesigned from time to time to stay fresh. So, instead of making a pile of website screenshots, which may no longer be relevant in the next several months because of the redesign, we're better going straight to the websites that curate websites, and the following are the places to go:
MediaQueries (http://mediaqueri.es/)
Awwwards (http://www.awwwards.com/websites/responsive-design/)
CSS Awards (http://www.cssawards.net/structure/responsive/)
WebDesignServed (http://www.webdesignserved.com/)
Bootstrap Expo (http://expo.getbootstrap.com/)
Zurb Responsive (http://zurb.com/responsive)
Summary
In this chapter, we glanced at the short story behind responsive web design, as well as the viewport meta tag and CSS3 media queries, which formulate responsive websites. This chapter also concluded that we are going to work on three projects by using the following frameworks: Responsive.gs, Bootstrap, and Foundation.
Using a framework is an easier and faster way to get responsive websites up and running, rather than building everything from scratch on our own. Alas, as mentioned, using a framework also has some negative aspects. If it is not done properly, the end result could all go wrong. The website could be stuffed and stuck with unnecessary styles and JavaScript, which at the end makes the website load slowly and hard to maintain.
We need to set up the right tools; not only will they facilitate the projects, but they will also help us in making the website more easy to maintain, and this is what we are going to do in the next chapter.
Chapter 2. Web Development Tools
Every professional has a set of tools that facilitates their work and gets the job done. Likewise, we will also need our own tools to do our bit of building responsive websites. So, before we start working on the projects in this module, the following are the tools we have to prepare.
Tools that we will have to prepare include:
A code editor for writing codes
A compiler that will compile the CSS preprocessor syntax into plain CSS
A local server to host the websites locally during the development stage
A bower to manage the website libraries
Choosing a code editor
As soon as we start writing code for HTML, CSS, and JavaScript, we need a code editor. A code editor is an indispensible tool to develop websites. Technically, you will only need text editors such as TextEdit in OS X or Notepad in Windows to write