Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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.

Arrow left icon
Profile Icon Ben Frain
Arrow right icon
₱579.99 ₱1796.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (100 Ratings)
eBook Apr 2012 324 pages 1st Edition
eBook
₱579.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial
Arrow left icon
Profile Icon Ben Frain
Arrow right icon
₱579.99 ₱1796.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4 (100 Ratings)
eBook Apr 2012 324 pages 1st Edition
eBook
₱579.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial
eBook
₱579.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
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

Billing Address

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

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 : 9781849693196
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
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

Billing Address

Product Details

Publication date : Apr 10, 2012
Length: 324 pages
Edition : 1st
Language : English
ISBN-13 : 9781849693196
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 ₱260 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 ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 4,491.98
Responsive Web Design with HTML5 and CSS3
₱2245.99
Learning jQuery - Fourth Edition
₱2245.99
Total 4,491.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.