Chapter 17 Exercise 17
Responsive Web Design
Responsive Web Design
Home > Chapter Review and Exercises > Chapter 17 - Responsive Web Design > Chapter 17 Exercise
Follow the instructions on pages 508 to 511 of the Class Text.
I have already created a folder in your Ch17 folder with the necessary files you will need to edit.
You will start by using Sublime to edit the bakery-rwd.css file.
As reference, the Chapter 17 textbook slides are in the Class Textbook and Resources section.
This completed exercise is available on CopePen here: https://codepen.io/smithchr/pen/XwmPLB
1. Responsive Web Design (or RWD) is a design and production approach that allows a website to be comfortably viewed, and used, on all manner of devices.
2, To fit standard websites onto small screens, mobile browsers render the page on a canvas called the viewport and then shrink that viewport down to fit the width of the screen (device width).
3. Responsive web design has three main parts, a flexible grid, flexible images, and CSS media queries.
In a Flexible Grid (Fluid Layout), the page area and its grid resize proportionally to fill the available width of the screen or window (p. 488-489).
The style rule required to make images scale down to fit the size of their container: img { max-width: 100%; } When the layout gets smaller, the images in it scale down to fit the width of their respective containers. If the container is larger than the image - for example, in the tablet or desktop layouts - the image does not scale larger; it stops at 100% of its original size (p. 490-491).
Media queries apply different styles based on characteristics of the browser: its width, whether it is vertically or horizontally oriented, its resolution, and more (p. 491-495).
4. The structure of a media query as used within a style sheet goes like this:
@media type and (feature: value) {
/* styles for browsers that meet this criteria */
}
5. A breakpoint is the point at which we use a media query to introduce a style change (p. 495-498)
IMPORTANT: Updates to out-of-date information found in the How Wide Is the Viewport? sidebar on page 498.
On page 498, the class text references a website mqtest.io. That site no longer exists, but https://www.mydevice.io/ does appear to provide the same information.
To start Chrome's Developer Tools, right-click the element and select Inspect. Or press Command+Option+C (Mac) or Control+Shift+C (Windows, Linux, Chrome OS).
You can open the Firefox Developer Tools with Ctrl + Shift + I or F12 on Windows and Linux, or Cmd + Opt + I on macOS.
On the Mac you need to enable the Developer menu. Click Safari → Preferences, and then click the Advanced tab. Enable the Show Develop menu in menu bar option in the Preferences dialog box.
6. Designing responsive websites involves:
Rearranging content into different layouts (p. 500-502).
Selecting the right typefaces (p. 502-503).
Make the navigation menus responsive (p. 504-506).
Images require special attention in responsive designs (p. 506).
Handling special content like forms, tables, and interactive elements (p. 506-507).
GETTING STARTED
Open the HTML file (bakery.html) in a browser with a Responsive View. This will allow you to try different viewport dimensions. The image on the left is how that website looks when it is only 320 pixels wide.
The bakery.html file has the following several modifications.
Layout: he page has a one-column layout for small screens. There are no borders around the main text area, and the Hours section has a scalloped edge on the top instead of the side. That maintains the look and feel, but is more appropriate when the sections are stacked.
Navigation: The navigation menu, which was created with Flexbox, couldn’t flex small enough to fit across a small screen. To make it fit, wrapping is used (flex-wrap: wrap;), and the width of each li element is set to 50% so there can be two on each row. They can both grow and shrink as needed (flex: 1 1 50%).
Conditional header text: The tagline (<p>The delicious baked goods you love at Black Goose Bistro...now available to go!</p>) takes up a lot of vertical space, and doesn't really need to be there. Using (display: none;), makes the tagline paragraph hidden, and it can be made visible again when there is more room.
Typography: On small screens, the very legible sans-serif font is used for the text rather than the web font because it is likely to be difficult to read at small sizes.
Images: The <img> elements for the bread and muffin images are set to display: block so they have the full width of the viewport to themselves with no text sneaking in next to them. Setting the side margins to auto keeps them centered horizontally.
Miscellaneous: The award appears at the bottom of the page because there is not enough space for it to be positioned at the top. A span of text (see below) is highlighted in yellow from the 45th to 75th characters to reveal when the line lengths get too long.
FIXING THE NAVIGATION
The design can now be tailored for other screen sizes. Using a Responsive View tool, you can resize the viewport and get an instant readout of the dimensions of the window. Try this on your browser with the bakery.html document. Keep making it wider, and you’ll see that some things look okay, and some things start looking awkward pretty quickly. In particular, notice what happens to the navigation menu at the top.
We want to make the stacked navigation at the top switch to a single centered line when the viewport is 400 pixels wide. A media query is used to make this happen.
Your first breakpoint: Media queries need to come after all the other CSS rules for the same declaration, so add them at the very end of the style sheet. Add this query as you see it here.
/* misc styles */
footer {
color: #EADDC4;
text-align: center;
font-size: .8em;
padding: 1em;
}
.day {
color: #783F27;
font-family: verdana, sans-serif;
font-size: .8em;
text-transform: uppercase;
}
.linelength {
background: yellow;
}
@media screen and (min-width: 400px) {
nav ul li {
flex: none;
}
nav ul {
justify-content: center;
}
}
This tells the browser that when the page is on a device that has a viewport that is 400 pixels or wider, to set the “flex” of menu list items to “none.” The none keyword is equivalent to flex: 0 0 auto;, so the items are not allowed to grow or shrink and will be sized based on their content.
The flexbox container is centered by setting justify-content: center.
Save the bakery-rwd.css style sheet and reload bakery.html in the browser. Try resizing the viewport to see how it works at wider sizes.
At 400 pixels wide, this is what the navigation menu used to look like:
And now, after adding this breakpoint, the navigation menu looks like this at 400 pixels wide:
FLOATING IMAGES - Part One
Notice that as the viewport gets gradually wider, that the main images (check out the bread image to the right) start looking very lonely on a line alone, and that there is room to start wrapping text around them again at about 480 pixels wide.
A second breakpoint at 480 pixels will fix this. To take care of that awkward whitespace, you can float the images to the far left once the screen reaches 480 pixels or more. Add this breakpoint at the very bottom of the bakery-rwd.css style sheet:
/* misc styles */
footer {
color: #EADDC4;
text-align: center;
font-size: .8em;
padding: 1em;
}
.day {
color: #783F27;
font-family: verdana, sans-serif;
font-size: .8em;
text-transform: uppercase;
}
.linelength {
background: yellow;
}
@media screen and (min-width: 400px) {
nav ul li {
flex: none;
}
nav ul {
justify-content: center;
}
}
/* floating images */
@media screen and (min-width: 480px) {
main img {
float: left;
margin: 0 1em 1em 0;
}
}
Before: A breakpoint is needed to fill in the awkward space around the image.
After making this change, save the bakery-rwd.css style sheet, and reload bakery.html in the browser.
At 480 pixels in width, bakery.html should look like the image to the right. Try resizing the viewport to see how it works at wider sizes.
After: At 480 pixels wide, the image is floated to the left .
FLOATING IMAGES - Part Two
OPTIONAL: These additions are not given in Exercise 17 in the text, but the Exercise does mention that this can be done on page 509:
NOTE: If you like, you can include the CSS shapes from Chapter 15, Floating and Positioning Exercise 15-2, on page 404, for a more interesting text wrap. The author omitted this here because of limited browser support.
HOWEVER: If you want, you can the two img elements from Exercise 15-2 (they are already in your images subfolder) that wrap the text around the images. You will insert these two img elements into the space shown below the 480 pixel breakpoint you just entered above:
/* floating images */
@media screen and (min-width: 480px) {
main img {
float: left;
margin: 0 1em 1em 0;
}
img[src*="muffin"] {
margin-left: 50px;
-webkit-shape-outside: circle(110px);
shape-outside: circle(110px);
-webkit-shape-margin: 1em;
shape-margin: 1em;
}
img[src*="bread"] {
margin-left: 50px;
-webkit-shape-outside: ellipse(130px 95px at 50% 50%);
shape-outside: ellipse(130px 95px at 50% 50%);
-webkit-shape-margin: 1em;
shape-margin: 1em;
}
}
After making this change, save the bakery-rwd.css style sheet and reload bakery.html in the browser.
At 480 pixels in width, bakery.html should look like the image to the right. Try resizing the viewport to see how it works at wider sizes.
Floating Images - Part Two
This is what you get:
TEXT AND TYPOGRAPHY
Once the screen gets to be about 600 pixels wide, there is enough room to add some embellishments. First, there is room for the tagline in the header, so you can set that to display again.
(Do not add these additions to the style sheet just yet)
header p {
display: block;
margin-top: -1.5em;
font-family: Georgia, serif;
font-style: italic;
font-size: 1.2em;
}
Second, the Stint Ultra Expanded web font, is a font that is not key to the company’s brand, so it was omitted on the narrow layout because of line length issues. However, at this breakpoint, it can be used because it is more legible and will result in comfortable line lengths.
(Do not add these additions to the style sheet just yet)
main, h2, h3 {
font-family: 'Stint Ultra Expanded', Georgia, serif;
}
h2, h3 {
font-weight: bold;
}
Third, the line height can be loosened up a little. You can take advantage of the extra space to add a rounded border around the main text area to bring it closer to the original brand identity for the site.
(Do not add these additions to the style sheet just yet)
main {
line-height: 1.8em;
padding: 1em;
border: double 4px #EADDC4;
border-radius: 25px;
margin: 2.5%;
}
You are now ready to make your third breakpoint.
The following is the media query for the 600-pixel breakpoint. Add this to the very bottom of the bakery-rwd.css style sheet after the other two media queries (the 400 and 480 pixel width breakpoints):
@media screen and (min-width: 600px) {
header p {
display: block;
margin-top: -1.5em;
font-family: Georgia, serif;
font-style: italic;
font-size: 1.2em;
}
main, h2, h3 {
font-family: 'Stint Ultra Expanded', Georgia, serif;
}
h2, h3 {
font-weight: bold;
}
main {
line-height: 1.8em;
padding: 1em;
border: double 4px #EADDC4;
border-radius: 25px;
margin: 2.5%;
}
}
The result is an enhanced one-column layout that is well suited for tablet-sized devices.
After making this change, save the bakery-rwd.css style sheet and reload bakery.html in the browser.
At 600 pixels in width, bakery.html should look like the image to the right. Try resizing the viewport to see how it works at wider sizes.
MULTICOLUMN LAYOUT
As the viewport gets wider, the text line is growing longer than 75 characters (as indicated by the yellow highlighted span of characters). This is a good point to introduce a second column to the layout. If you aren’t targeting a specific device, the exact breakpoint is subjective (meaning it's really a matter of taste, not an exact measurement).
The next breakpoint will be at 940 pixels, as the point above which the page gets a multi-columned layout. The grid layout styles from the previous chapter are reapplied here.
In the aside element, the scalloped background graphic is moved to the left edge.
A maximum width of 1200px is set for the container, and its side margins are set to auto, so if the browser window is wider than 1,200 pixels, the layout will stay a fixed width and get centered in the viewport.
The award graphic that was at the bottom is now going to be absolutely positioned at the top of the page, now that there’s enough room.
You are now ready to make your fourth and final breakpoint.
Add this final media query at the very end of the style sheet:
@media screen and (min-width: 940px) {
#container {
display: grid;
grid-template-rows: auto min-height 5em;
grid-template-columns: minmax(25em, 1fr) 16em;
grid-template-areas:
"banner banner"
"main hours"
"footer footer";
max-width: 1200px;
margin: 0 auto;
position: relative;
}
header {
grid-area: banner;
}
main {
grid-area: main;
}
aside {
grid-area: hours;
background: url(images/scallop.png) repeat-y left top;
background-color: #F6F3ED;
padding: 1em;
padding-left: 45px;
}
footer {
grid-area: footer;
}
#award {
position: absolute;
top: 30px;
left: 50px;
}
}
After making this change, save the bakery-rwd.css style sheet and reload bakery.html in the browser. The two-column grid layout is appropriate for viewports over 940 pixels. On very wide screens, as shown here, the container stops expanding at 1,200 pixels wide and is centered horizontally.