Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Responsive Web Design with HTML5 and CSS3
Responsive Web Design with HTML5 and CSS3

Responsive Web Design with HTML5 and CSS3: Web pages that respond immediately to different screen sizes and devices is one of today's essentials. Packed with screenshots and examples, this book will teach you the professional approach using just HTML5 and CSS3.

eBook
$9.99 $25.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Responsive Web Design with HTML5 and CSS3

Chapter 2. Media Queries: Supporting Differing Viewports

As we noted in the last chapter, CSS3 consists of a number of bolt-on modules. Media queries is just one of these CSS3 modules. Media queries allow us to target specific CSS styles depending upon the display capabilities of a device. For example, with just a few lines of CSS we can change the way content displays based upon things such as viewport width, screen aspect ratio, orientation (landscape or portrait), and so on.

In this chapter, we shall:

  • Learn why media queries are needed for a responsive web design

  • Learn how a CSS3 media query is constructed

  • Understand what device features we can test for

  • Write our first CSS3 media query

  • Target CSS style rules to specific viewports

  • Learn how to make media queries work on iOS and Android devices

You can use media queries today


Media queries are already widely used and enjoy a broad level of browser support (Firefox 3.6+, Safari 4+, Chrome 4+, Opera 9.5+, iOS Safari 3.2+, Opera Mobile 10+, Android 2.1+, and Internet Explorer 9+). Furthermore, there are easy to implement (albeit JavaScript based) fixes for common aged browsers such as Internet Explorer versions 6, 7, and 8. If you need to grab the fixes for Internet Explorer versions 6, 7, and 8 now, you'll need to look at Chapter 9, Solving Cross-browser Responsive Challenges. In short, there's no good reason why we can't get using media queries today!

Note

Specifications at the W3C go through a ratification process (if you have a spare day, knock yourself out with the official explanation of the process at http://www.w3.org/2005/10/Process-20051014/tr), from Working Draft (WD ), to Candidate Recommendation (CR ), to Proposed Recommendation (PR ) before finally arriving, many years later, at W3C Recommendation (REC ). So modules at...

Why responsive designs need media queries?


Without the CSS3 media query module, we would be unable to target particular CSS styles at particular device capabilities, such as the viewport width. If you head over to the W3C specification of the CSS3 media query module (http://www.w3.org/TR/css3-mediaqueries/), you'll see that this is their official introduction to what media queries are all about:

HTML 4 and CSS2 currently support media-dependent style sheets tailored for different media types. For example, a document may use sans-serif fonts when displayed on a screen and serif fonts when printed. 'screen' and 'print' are two media types that have been defined. Media queries extend the functionality of media types by allowing more precise labeling of style sheets.

A media query consists of a media type and zero or more expressions that check for the conditions of particular media features. Among the media features that can be used in media queries are 'width', 'height', and 'color'. By using...

Our first responsive design


I don't know about you but I'm itching to get started with a responsive web design! Now we understand the principles of media queries, let's test drive them and see how they work in practice. And I have just the project we can test them on. Indulge me a brief digression…

I like films. However, I commonly find myself disagreeing with others (perhaps that is a contributing factor of me spending my days writing code… alone!), specifically about what is and what isn't a good film. When the Oscar nominees are announced I often have a strong feeling of revulsion in the pit of my stomach. I can't help feeling that different films should be picking up the accolades. I'd like to launch a small site called And the winner isn't…, which you'll be able to view online at http://www.andthewinnerisnt.com/ on the Web. It will celebrate the films that should have won, berate the ones that did (and shouldn't have) and have video clips, quotes, images, and quizzes thrown in to illustrate...

Stopping modern mobile browsers from auto-resizing the page


Both iOS and Android browsers are based on WebKit (http://www.webkit.org/). These browsers, and a growing number of others (Opera Mobile, for example), allow the use of a specific meta viewport element to override that default canvas shrinking trick. The <meta> tag is simply added within the <head> tags of the HTML. It can be set to a specific width (which we could specify in pixels, for example) or as a scale, for example 2.0 (twice the actual size). Here's an example of the viewport meta tag set to show the browser at twice (200 percent) the actual size:

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

Let's stick that into our HTML as done in the following code snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type...

Fixing the design for different viewport widths


With our meta viewport problem fixed, no browsers are now zooming the page, so we can set about fixing the design for different viewports. In the CSS, we'll add a media query for devices such as tablets (for example, iPad) that have a viewport width of 768 pixels in portrait view (as the landscape viewport width is 1024 pixels, it renders the page fine when loaded in Landscape view).

@media screen and (max-width: 768px) {
  #wrapper { 
    width: 768px; 
  }
  #header,#footer,#navigation {
    width: 748px; 
  }  
}

Our media query is re-sizing the width of the wrapper, header, footer, and navigation elements if the viewport size is no larger than 768 pixels. The following screenshot shows how this looks like on our iPad:

I'm actually quite encouraged by that. The content now fits on the iPad display (or any other viewport no larger than 768 pixels) with no clipping. However, we need to fix the Navigation area as the links are extending off the...

With responsive designs, content should always come first


We want to retain as many features of our design across multiple platforms and viewports (rather than hiding certain parts with display: none or similar) but it's also important to consider the order in which things appear. At present, due to the order of the sidebar and main content sections of our markup, the sidebar will always want to display before the main content. It's obvious that a user with a more limited viewport should get the main content before the sidebar, otherwise they'll be seeing tangentially related content before the main content itself.

We could (and perhaps should) move our content above our navigation area, too. So that those with the smallest viewports get the content before anything else. This would certainly be the logical continuation of adhering to a "content first" maxim. However, in most instances, we'd like some navigation atop each page, so I'm happier simply swapping the order of the sidebar and content...

Media queries—only part of the solution


Oh… best put that ice back in the freezer. Clearly our work is far from over; that looks horrible on the smaller 320 pixel wide viewport of our iPhone. Our media query is doing exactly what it should, applying styles dependent upon the features of our device. The problem is however, that the media query covers a very narrow spectrum of viewports. Anything with a viewport under 768 pixels is going to experience clipping and anything between 768 and 960 pixels will experience clipping as it will get the non-media query version of the CSS styles which, as we already know, doesn't adapt once we take it below 960 pixels wide (your author rests his head in his hands and lets out a long sigh).

We need a fluid layout

Using media queries alone to change a design is fine if we have a specific known target device; we've already seen how easy it is to adapt a device to the iPad. But this strategy has severe shortcomings; namely, it isn't really future-proof. At...

Summary


In this chapter, we've learned what CSS3 media queries are, how to include them in our CSS files, and how they can help our quest to create a responsive web design. We've also learned how to make modern mobile browsers render our pages in the same manner as their desktop counterparts and touched upon the need to consider a "content first" policy when structuring our markup. We've also learned the data economies that can be made when we use images in our design in the most economical way.

However, we've also learned that media queries can only provide an adaptable web design, not a truly responsive one. Media queries are an essential component in a responsive design but a fluid layout that allows our design to flex between the break points that the media queries handle is also essential. Creating a fluid base for our layout to smooth the transition between our media query break points is what we'll be covering in the next chapter.

Left arrow icon Right arrow icon

Key benefits

  • Everything needed to code websites in HTML5 and CSS3 that are responsive to every device or screen size
  • Learn the main new features of HTML5 and use CSS3's stunning new capabilities including animations, transitions and transformations
  • Real world examples show how to progressively enhance a responsive design while providing fall backs for older browsers

Description

Tablets, smart phones and even televisions are being used increasingly to view the web. There's never been a greater range of screen sizes and associated user experiences to consider. Web pages built to be responsive provide the best possible version of their content to match the viewing devices of not just today's devices but tomorrow's too.Learn how to design websites according to the new "responsive design"ù methodology, allowing a website to display beautifully on every screen size. Follow along, building and enhancing a responsive web design with HTML5 and CSS3. The book provides a practical understanding of these new technologies and techniques that are set to be the future of front-end web development. Starting with a static Photoshop composite, create a website with HTML5 and CSS3 which is flexible depending on the viewer's screen size.With HTML5, pages are leaner and more semantic. A fluid grid design and CSS3 media queries means designs can flex and adapt for any screen size. Beautiful backgrounds, box-shadows and animations will be added ñ all using the power, simplicity and flexibility of CSS3.Responsive web design with HTML5 and CSS3 provides the necessary knowledge to ensure your projects won't just be built "right" for today but also the future.

Who is this book for?

Are you writing two websites ñ one for mobile and one for larger displays? Or perhaps you've heard of Responsive Design but are unsure how to bring HTML5, CSS3, or responsive design all together. If so, this book provides everything you need to take your web pages to the next level ñ before all your competitors do!

What you will learn

  • Responsive web design is the hottest topic in web design. Understand what it is and why it s essential to master
  • HTML5 is leaner, faster and more semantically rich. You ll learn how to write HTML5 and understand all the key features
  • CSS3 media queries allow different styles for different media, learn how to integrate them into a design
  • Make web pages and the media within them "fluid" - allowing them to flex when needed
  • Ditch images and use CSS3 to create background gradients, text shadows, box shadows and more
  • Use CSS3 3D transformations to flip elements in 3D space. Animate elements with CSS3 keyframes
  • Create smooth CSS3 transitions between default and hover states with differing durations and timing functions
  • Conquer forms ñ add validation and useful interface elements like date pickers and range sliders with HTML5 alone
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 10, 2012
Length: 324 pages
Edition : 1st
Language : English
ISBN-13 : 9781849693189
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Apr 10, 2012
Length: 324 pages
Edition : 1st
Language : English
ISBN-13 : 9781849693189
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 87.98
Responsive Web Design with HTML5 and CSS3
$43.99
Learning jQuery - Fourth Edition
$43.99
Total $ 87.98 Stars icon
Banner background image

Table of Contents

9 Chapters
Getting Started with HTML5, CSS3, and Responsive Web Design Chevron down icon Chevron up icon
Media Queries: Supporting Differing Viewports Chevron down icon Chevron up icon
Embracing Fluid Layouts Chevron down icon Chevron up icon
HTML5 for Responsive Designs Chevron down icon Chevron up icon
CSS3: Selectors, Typography, and Color Modes Chevron down icon Chevron up icon
Stunning Aesthetics with CSS3 Chevron down icon Chevron up icon
CSS3 Transitions, Transformations, and Animations Chevron down icon Chevron up icon
Conquer Forms with HTML5 and CSS3 Chevron down icon Chevron up icon
Solving Cross-browser Responsive Challenges Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(100 Ratings)
5 star 58%
4 star 27%
3 star 8%
2 star 6%
1 star 1%
Filter icon Filter
Top Reviews

Filter reviews by




Stuart J McIntosh Jul 31, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I bought the Kindle edition. This book had everything I needed to take my existing HTML 4, flash and spry site and convert to a mobile aware responsive site using HTML5 and JQUERY(mobile). The pointer on how to re-size images on the server side was useful. I was a bit rusty on HTML and CSS when I began, but this book had clear explanations and good examples.
Amazon Verified review Amazon
James I dahle Jan 10, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book is written in a careful way to take you to the best results, by first explaining what benefits you gain, and what mistakes you will avoid. I found this to be excellent and worth reading, then starting again to re-read all the benefits.
Amazon Verified review Amazon
L. Coia Dec 01, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Simple and concise lessons and good references to tools. This well written book will quickly bring you up to speed on responsive design.
Amazon Verified review Amazon
Bruce G Symons Jul 10, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Easy to read, great information. A real handy volume to have around while learning this technique. Highly recommend this to all.
Amazon Verified review Amazon
Joel Jan 19, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am a novice web designer, which means I only design my sites and not for pay, (Yet). This book took me step by step on how to tackle the new features in HTML5 CSS3 and Responsive Design. I highly recommend this book to novices like me and pros out there.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela