Responsive Web Design With HTML5 and CSS3 - Second Edition - Sample Chapter
Responsive Web Design With HTML5 and CSS3 - Second Edition - Sample Chapter
Second Edition
The changing way in which we access the Web means
that there has never been a greater range of screen sizes
and user experiences to consider. Web pages built to be
responsive can adapt their content for not just today's
devices but tomorrow's too.
Learn how to build websites with a "responsive and mobile
first" methodology, allowing it to display effortlessly on
every device that accesses it. Packed with examples, and
a thorough explanation of modern techniques and syntax,
this book provides a comprehensive resource for all
things "responsive".
The second edition covers all the most up-to-date
techniques and tools needed to build great responsive
designs, ensuring that your projects are future-proof.
"Community
Experience
Distilled"
Ben Frain
Second Edition
ee
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Sa
m
Ben Frain
Before the web, he worked as an underrated (and modest) TV actor and technology
journalist, having graduated from Salford University with a degree in Media and
Performance.
He has written four equally underrated (his opinion) screenplays and still harbors
the (fading) belief he might sell one. Outside of work, he enjoys simple pleasures.
Playing indoor football while his body and wife still allow it, and wrestling with his
two sons.
His other book, Sass and Compass for Designers is available now. Visit Ben online at
www.benfrain.com and follow him on Twitter at twitter.com/benfrain.
Preface
A responsive web design provides a single solution that looks great on a phone,
desktop, and everything in-between. It will effortlessly respond to the size of the user's
screen, providing the best experience possible for both today's and tomorrow's devices.
This book 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.
If you can understand HTML and CSS, you can build a responsive web design.
Preface
Chapter 5, CSS3 Selectors, Typography, Color Modes, and New Features, gets to grips
with the endless possibilities of CSS: selectors, HSLA and RGBA colors, web
typography, viewport relative units, and a whole lot more.
Chapter 6, Stunning Aesthetics with CSS3, covers CSS filters, box shadows, linear and
radial gradients, multiple backgrounds, and how to target background images to
high-resolution devices.
Chapter 7, Using SVGs for Resolution Independence, explains everything we need to use
SVGs inside documents and as background images, as well as how to interact with
them using JavaScript.
Chapter 8, Transitions, Transformations, and Animations, our CSS gets moving as we
explore how to make interactions and animations using CSS.
Chapter 9, Conquer Forms with HTML5 and CSS3, web forms have always been
tough but the latest HTML5 and CSS3 features make them easier to deal with than
ever before.
Chapter 10, Approaching a Responsive Web Design, explores the essential considerations
before embarking on a responsive web design and also provides a few last minute
nuggets of wisdom to aid you in your responsive quest.
Chapter 1
In its infancy, it was typical for a responsive design to be built starting with the
'desktop', fixed-width design. Content was then reflowed, or removed so that the
design worked on smaller screens. However, processes evolved and it became apparent
that everything from design, to content and development, worked much better when
working in the opposite direction; starting with smaller screens and working up.
Before we get into this, there are a couple of subjects I'd like to address before we
continue; browser support and text editors/tooling.
You'll also find that practically, starting with the simplest 'base level' experience and
enhancing (an approach known as progressive enhancement) is easier than coming
at the problem from the opposite directionbuilding the ultimate experience first
then attempting to provide fall backs for less capable platforms (an approach known
as graceful degradation).
To exemplify why knowing this in advance matters, consider that if you were
unlucky enough to have 25% of your website visitors using Internet Explorer 9 (for
example), you'd need to consider what features that browser supports and tailor
your solution accordingly. The same caution would be required if large amounts of
your users are visiting with older mobile phone platforms such as Android 2. What
you can consider a 'base' experience will vary depending upon the project.
If suitable data isn't available, I apply a simple and crude piece of logic to determine
whether I should spend time developing a particular platform/browser version: if
the cost of developing and supporting browser X is more than the revenue/benefit
created by the users on browser X; don't develop specific solutions for browser X.
It's rarely a question of whether you could 'fix' an older platform/version. It's a
question of whether you should.
When considering which platforms and browser versions support which features,
if you aren't already, become familiar the http://caniuse.com website. It provides a
simple interface for establishing what browser support there is for the features we
will be looking at throughout.
[4]
Chapter 1
[5]
Chapter 1
<h3 class="SubHeader">Ingredients</h3>
<ul>
</ul>
</div>
<div class="HowToMake">
<h3 class="SubHeader">Method</h3>
<ol class="MethodWrapper">
</ol>
</div>
</body>
</html>
By default, web pages are flexible. If you were to open the example page, even as it is
at this point (with no media queries present), and resize the browser window you'll
see the text reflows as needed.
What about on different devices? With no CSS whatsoever, this is how that renders
on an iPhone:
[7]
As you can see, it's rendering like a 'normal' web page would on an iPhone. The
reason for that is that iOS renders web pages at 980px wide by default and shrinks
them down into the viewport.
The viewable area of a browser is known technically as the viewport. The viewport
is seldom equivalent to the screen size of a device, especially in instances where a
user can resize a browser window.
Therefore, from now on, we will generally use this more accurate term when we are
referring to the available space for our web page.
We can fix that prior problem easily by adding this snippet in the <head>:
<meta name="viewport" content="width=device-width">
This viewport meta tag is a non-standard (but de facto standard) way of telling the
browser how to render the page. In this case, our viewport meta tag is effectively
saying "make the content render at the width of the device". In fact, it's probably
easier to just show you the effect this line has on applicable devices:
[8]
Chapter 1
Great! The text is now rendering and flowing at a more 'native' size. Let's move on.
We will cover the meta tag and its various settings and permutations (and the
standards based version of the same functionality) in Chapter 2, Media Queries
Supporting Differing Viewports.
Taming images
They say a picture is worth a thousand words. All this writing about scones in our
sample page and there's no image of the beauties. I'm going to add in an image of a
scone near the top of the page; a sort of 'hero' image to entice users to read the page.
Oh! That nice big image (2000px wide) is forcing our page to render more than a little
wonky. We need to fix that. We could add a fixed width to the image via CSS but the
problem there is that we want the image to scale to different screen sizes.
For example, our example iPhone is 320px wide so we could set a width of 320px
to that image but then what happens if a user rotates the screen? The 320px wide
viewport is now 480px wide. Thankfully it's pretty easy to achieve fluid images that
will scale to the available width of their container with a single line of CSS.
[9]
I'm going to create the css/styles.css CSS file now that's linked in the head of the
HTML page.
Here is the first thing I'm adding. Ordinarily I'd be setting a few other defaults, and
we'll discuss those defaults in later chapters, but for our purposes I'm happy to open
with just this:
img {
max-width: 100%;
}
Now when the page is refreshed we see something more akin to what we
might expect.
All this max-width based rule does is stipulate that all images should be a maximum
of 100% of their width (in that they should expand to 100% of their size and no
more). Where a containing element (such as the body or a div it sits within) is
less than the intrinsic width of the image, it will simply scale up to the maximum
available space.
[ 10 ]
Chapter 1
Excellent. Everything is now laid out as expected. No matter the viewport size,
nothing is overflowing the page horizontally.
However, if we look at the page in larger viewports, the basic styles start to get
both literally and figuratively stretched. Take a look at the example page at a size
around 1400px:
Oh dear! In fact, even around 600px wide it's starting to suffer. Around this point
it would be handy if we could rearrange a few things. Maybe resize the image
and position it off to one side. Perhaps alter some font sizes and background colors
of elements.
[ 11 ]
Thankfully, we can achieve all this functionality quite easily by employing CSS
media queries to bend things to our will.
We will cover the entire gamut of CSS media queries in Chapter 2, Media Queries
Supporting Differing Viewports, inventively titled Media Queries.
However, for the purpose of whipping our basic example into shape, we will
concentrate on just one type of media query; a minimum width media query. CSS
rules within this type of media query only get applied if the viewport is a minimum
defined width. The exact minimum width can be specified using a raft of different
length units including percent, em, rem, and px. In CSS, a minimum width media
query is written like this:
@media screen and (min-width: 50em) {
/* styles */
}
[ 12 ]
Chapter 1
The @media directive tells the browser we are starting a media query, the screen
part (declaring 'screen' is technically not needed in this situation but we will deal
with that in detail in the next chapter) tells the browser these rules should be applied
to all screen types and the and (min-width: 50em) tells the browser that the rules
should be limited to all viewports above 50em of size.
I believe it was Bryan Rieger (http://www.slideshare.net/
bryanrieger/rethinking-the-mobile-web-by-yiibu) who first wrote that:
For now, simply be aware that this approach re-enforces our smallest screen first
mentality and allows us to progressively layer on detail as and when the design
necessitates it.
First off, we will stop that main 'hero' image getting too big and keep it over on the
right. Then the intro text can sit to the left.
We will then have the main portion of text, the 'method' that describes how to make
the scones, on the left below with a small boxed out section detailing the ingredients
over on the right.
[ 13 ]
It still looks essentially the same as it did before on smaller screens but adjusts to the
new layout as soon as the viewport is 50rem or wider.
Here are the layout styles that were added:
@media screen and (min-width: 50rem) {
.IntroWrapper {
display: table;
table-layout: fixed;
width: 100%;
}
.MoneyShot,
.IntroText {
display: table-cell;
width: 50%;
vertical-align: middle;
text-align: center;
}
[ 14 ]
Chapter 1
.IntroText {
padding: .5rem;
font-size: 2.5rem;
text-align: left;
}
.Ingredients {
font-size: .9rem;
float: right;
padding: 1rem;
margin: 0 0 .5rem 1rem;
border-radius: 3px;
background-color: #ffffdf;
border: 2px solid #e8cfa9;
}
.Ingredients h3 {
margin: 0;
}
}
That wasn't too bad was it? With only minimal code we have built a page that
responds to the viewport size and offers a preferable layout as needed. By adding
just a few more styles things look even easier on the eye. With those in place, our
basic responsive page now looks like this on an iPhone:
[ 15 ]
[ 16 ]
Chapter 1
Summary
Well done, you now know and understand the essential elements needed to create a
fully responsive web page. However, as we have just discovered, there are plenty of
places where things could be improved.
But that's fine. We don't just want the ability to make competent responsive web
designs, we want to be able to create 'best of breed' experiences. So let's press on.
First up, we will wrap our heads around all that Level 3 and Level 4 CSS Media
Queries have to offer. We have already seen how a web page can respond to
viewport width but there's so much more we can do right nowand a lot more fun
stuff coming to your browser soon. Let's go and take a look.
[ 17 ]
Get more information Responsive Web Design with HTML5 and CSS3
Second Edition
www.PacktPub.com
Stay Connected: