stunningCSS C06thru219
stunningCSS C06thru219
stunningCSS C06thru219
Size, Different
6
Design
It’s no secret or surprise that the variety of ways people
browse the web is increasing. People may view your web pages
on widescreen TVs, desktop computers, netbooks, mobile
phones—even a refrigerator. While you can’t make a site that
looks identical on every single device at every screen size
and text size, you can make a site that adapts to the users’
settings so that it looks good and works well in the screen space
available. In this chapter, you’ll learn how to use CSS3 media
queries to tailor a web page’s design to various screen sizes on
the fly, making your web pages more dynamic, responsive,
and usable.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
206 Chapter 6: Different Screen Size, Different Design
W h at You’ll Le a rn
We’ll be restyling an entire page layout to work with different screen sizes and devices using these
pieces of CSS3:
Media queries to apply styles selectively based on the visitor’s device properties
Multi-columns to flow text into side-by-side columns
F igure 6.1
The fictional Little Pea
Bakery home page, as
seen in a browser win-
dow that’s 1024 pixels
wide.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
The Base Page 207
Fi gu r e 6 . 2 The design looks OK, but not great, in very narrow and
very wide windows.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
208 Chapter 6: Different Screen Size, Different Design
The above media query starts with the @media rule, and then speci-
Ti p: If you want to use
the media type all in fies a media type (in this case, screen). Next there’s the word and,
your media query, you followed by the characteristic we want to match against, called the
can make your CSS media feature. This particular media feature, max-width: 600px, tells
shorter by leaving out the browser that the styles for this media query, which are contained
the media type entirely, within a set of curly brackets for the media query as a whole, should
as well as the word apply only up to a maximum width of 600 pixels. If the viewport
and, like so: @media
width exceeds 600 pixels, the browser will ignore the styles inside
(max-width:600px).
the media query.
This media query can be dropped right into your main style sheet,
keeping all your styles in one place for easy debugging and mainte-
nance, as well as saving an HTTP request. If you want, however, you
can apply media queries to separate style sheets on the link element
or @import rule:
@import url(narrow.css) only screen and (max-width:600px);
Here, I’ve added the keyword only in front of the media type screen
to keep some older browsers that don’t understand media queries
from downloading and applying the style sheets universally. Most
non-supporting browsers will not use the sheet anyway, but this is
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
Changing the Layout for Large Screens 209
extra insurance. The only keyword isn’t needed when you place the
@media rule directly in your main style sheet.
This media query has to be at the end of the styles so that it will over-
ride the earlier styles, using the cascade of CSS. It tells the browser
that we want the styles within this media query to apply to screen
media types, but only if the user’s viewport width is 1200 pixels at a
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
210 Chapter 6: Different Screen Size, Different Design
minimum. Of course, right now there are no styles in the media query,
just empty brackets waiting to be filled. Since we have so much extra
space in viewports over 1200 pixels wide, how about we fill those
brackets with styles to change the layout from two columns to three?
To do this, we’ll change the positioning of the navigation div, as well
as the widths and margins of the two content divs. Here are the cur-
rent styles of these three divs, outside the media query:
#nav-main {
float: right;
margin: 40px 0 0 0;
}
#content-main {
overflow: hidden;
float: left;
width: 70%;
margin-bottom: 40px;
}
#content-secondary {
float: right;
width: 25%;
margin-bottom: 40px;
}
Modify these styles for viewports over 1200 pixels wide by adding new
Not e: Opera 10.6
has a strange bug that rules within the media query you just created:
makes the navigation @media screen and (min-width: 1200px) {
div disappear when #nav-main {
you first expand the position: fixed;
window past 1200 pixels. top: 136px;
When you hover over width: 13%;
the area where it should margin: 0;
be, it shows up. There’s }
no workaround for this #content-main {
right now; hopefully the width: 58%;
Opera team will fix this margin-left: 18%;
bug soon. }
#content-secondary { width: 20%; }
}
This positions the navigation div under the logo, creating a third col-
umn. To make room for it, it was necessary to decrease the width of
the content-secondary div from 25 percent to 20 percent, decrease
the width of the content-main div from 70 percent to 58 percent,
and add a left margin to content-main.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
Changing the Layout for Large Screens 211
Let’s also change the widths of the about and credits divs in the footer
to match the widths of the columns above them. Add their IDs onto
the #content-main and #content-secondary rules in the media query:
#content-main, #about {
width: 58%;
margin-left: 18%;
}
#content-secondary, #credits { width: 20%; }
Now all the page elements are better positioned to work well in the
width available (Figure 6.3). Save your page, and view it in an up-to-
date browser. Resize your window to see how the layout automatically
changes when you get past 1200 pixels wide.
Fig u re 6.3 The elements of the page now make better use of the space in wide windows.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
212 Chapter 6: Different Screen Size, Different Design
You can include more than one media feature in a single media query, such as @media screen
and (min-width:320px) and (max-width:480px). You can also include more than one media
query in the same @media rule, separated by commas, such as @media screen and (color),
projection and (color), similar to a grouped selector.
You can write the word not at the start of a media query to apply its styles only when the media
query is not true, such as @media not print and (max-width:600px).
Other than changing layout at different screen sizes, you might want to use media queries for:
Adjusting text size and leading to keep text more readable at different line lengths; see
http://forabeautifulweb.com/blog/about/proportional_leading_with_css3_media_queries
Increasing text size of buttons, tabs, and links on mobile devices to make these elements easier
to activate with your finger on touch screens
Decreasing body-text size on small mobile screens since the user is effectively zoomed in, making
the text seem larger than on desktop screens
Revealing in-page links to jump to content down the page on small mobile screens
Swapping in higher resolution images on high-resolution devices, such as the iPhone 4;
see http://dryan.com/articles/posts/2010/6/25/hi-res-mobile-css-iphone-4 as well as the example
later in this chapter
Swapping in differently sized images for different viewport sizes
Applying different print styles for different sizes of paper
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
Changing the Layout for Large Screens 213
Now each link is on its own line and takes up the full width of the
menu (Figure 6.4).
Next, let’s apply some styling to the menu as a whole to make it look
more similar to the email newsletter box on the other side of the
page, which has a semitransparent background, slightly rounded cor-
ners, and a soft drop shadow:
#nav-main {
position: fixed;
top: 136px;
width: 13%;
margin: 0; F i g u r e 6 . 4 Each link
-moz-box-shadow: 0 0 8px hsla(0,0%,0%,.1); now takes up the full
-webkit-box-shadow: 0 0 8px hsla(0,0%,0%,.1); width of the menu.
box-shadow: 0 0 8px hsla(0,0%,0%,.1);
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
background: hsla(0,0%,100%,.3);
text-align: right;
}
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
214 Chapter 6: Different Screen Size, Different Design
Since the menu has its own background color now, tone down the
semitransparent gradients on the links within it, so that the two col-
ors layered over each other don’t get too opaque:
#nav-main a {
-moz-border-radius: 0;
-webkit-border-radius: 0;
border-radius: 0;
background: -moz-linear-gradient(hsla(0,0%,100%,.3),
¬ hsla(0,0%,100%,0) 15px);
background: -webkit-gradient(linear, 0 0, 0 15,
¬ from(hsla(0,0%,100%,.3)), to(hsla(0,0%,100%,0)));
}
#nav-main a:hover {
background: -moz-linear-gradient(hsla(0,0%,100%,.6),
¬ hsla(0,0%,100%,.2) 15px);
F igure 6.5 The menu background: -webkit-gradient(linear, 0 0, 0 15,
in the left column ¬ from(hsla(0,0%,100%,.6)), to(hsla(0,0%,100%,.2)));
has been restyled to }
resemble the newsletter
subscription box in the These changes complete the navigation’s transformation from hori-
right column. zontal bar to vertical menu (Figure 6.5).
Multi-column Text
One of the main complaints people have with layouts that adjust to
Not e: You can learn
more about line length viewport width is that the length of lines of text can become either
in Chapter 1 of Flexible too short or too long to be read comfortably or look attractive. Some
Web Design, which you of this fear of “non-standard” line lengths is based on assumption
can download for free at and myth. In reality, there is no magic line length that is ideal for
www.flexiblewebbook. everyone; a person’s age, reading level, native language, disability, and
com/bonus.html. other factors all influence which line length he finds easiest to read.
However, it’s true that line lengths on the extreme ends of the range
don’t work well for the majority of readers and don’t always look very
attractive. One way that we can now control line lengths is with the
new multi-column properties in CSS3. These properties allow you
to flow the content of a single HTML element into multiple columns,
similar to a newspaper layout.
You create the columns using either the column-count or column-
width properties; in the latter case, the browser will decide how many
columns to make based on the available space. (You can also use both
properties together, though you may get unexpected results; see “The
lowdown on multi-columns” for more information.)
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
Changing the Layout for Large Screens 215
Let’s break the introductory paragraph into two columns in both the
regular layout and the wide layout. Find the existing h1 + p rule in
the styles outside of the media query; it should be on line 102, about
a third of the way down the style element. Add the column-count
property, plus the three browser-specific versions, to the rule:
h1 + p {
-moz-column-count: 2;
-o-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
color: #7F4627;
text-shadow: -1px -1px 0 hsla(0,0%,100%,.6);
font-size: 120%;
}
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
216 Chapter 6: Different Screen Size, Different Design
explicitly setting the value you want. Here, we’ve used a value in ems
so that the gap will grow larger as the text grows larger, keeping the
text more readable.
Now the introductory paragraph is broken into two columns in both
the regular layout and the wide layout created with the media query
(Figure 6.6). This completes all the styling for the wide version of the
bakery page (Figure 6.7).
F igure 6.7
The completed design
for wide viewports
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
Changing the Layout for Large Screens 217
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
218 Chapter 6: Different Screen Size, Different Design
But some problems with multi-columns are more inherent to the idea
of columns on the web to begin with. Having to scroll down to read
a column and then back up to read the next column, over and over
again, is just plain annoying and tiresome. This isn’t a technical prob-
lem—it’s a usability problem with breaking up content that’s taller
than a constrained screen. Treating the web like print often doesn’t
work well. For more on the usability and design problems inherent to
CSS3 multi-columns, see “Multicolumn layout considered harmful”
by Roger Johanssen (www.456bereastreet.com/archive/200509/css3_
multicolumn_layout_considered_harmful), “More on multi-column
layouts” by Richard Rutter (www.clagnut.com/blog/1590), and “CSS3
Multi-Column Thriller” by Andy Clarke (www.stuffandnonsense.
co.uk/archives/css3_multi-column_thriller.html).
Because of all of these problems, I strongly recommend only using
multiple columns in a limited manner. I think they’re fine for a cou-
ple paragraphs or a list, for instance. I don’t think they work very well
for long blocks of body copy or content that is complex, with several
paragraphs, types of elements, or images within it. Just keep this in
mind, and use multi-columns wisely.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.
Changing the Layout for Large Screens 219
You can use the column-gap property to create spaces between the col-
umns, and the column-rule property to create a vertical line within each
gap as a visual separator. The column-span property allows elements to
span across multiple columns, but is not yet supported by any browser.
The break-before, break-after, and break-inside properties con-
trol where content is broken across columns, but they are not yet sup-
ported by any browser.
Other than breaking short pieces of body text into multiple columns,
as we’ve done in this chapter, I think the only safe use for multi-columns
currently is to break a single list of short items into multiple columns;
see http://trentwalton.com/2010/07/19/css3-multi-column-layout-
column-count for an example.
Excerpted from Stunning CSS3: A project-based guide to the latest in CSS by Zoe Gillenwater.
Copyright © 2011. Used with permission of Pearson Education, Inc. and New Riders.