Master the art of creating responsive web designs and enhance your skillset with our comprehensive tutorial on Mobile & Responsive Web Design covering topics like adding SVG sprites, styling logos, and fixing opacity inheritance.
This exercise is excerpted from Noble Desktop’s past web design training materials. We now teach Figma as the primary tool for web and UX & UI design. To learn current skills in web design, check out our Figma Bootcamp and graphic design classes in NYC and live online.
Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.
Topics covered in this Mobile & Responsive Web Design tutorial:
Adding SVG Sprites, Styling the logo & nav for various sizes/devices, Fixing opacity inheritance
Exercise Preview
Exercise Overview
Continuing with the Jive Factory site, in this exercise we’ll finish styling the header for tablets and desktops. Along the way you’ll learn how to use SVG for sprites.
-
If you completed the previous exercises, you can skip the following sidebar. We recommend you finish the previous exercises (2D–3C) before starting this one. If you haven’t finished them, do the following sidebar.
If You Did Not Do the Previous Exercises (2D–3C)
- Close any files you may have open.
- On the Desktop, go to Class Files > yourname-Mobile and Responsive Class.
- Delete the Jive folder if it exists.
- Select the Jive Header Partially Done folder.
- Duplicate the folder.
- Rename the folder to Jive.
- In your code editor, open index.html (from the Jive folder).
- Preview index.html in a browser.
- Resize the browser window to the breakpoint for tablets (green border).
The header is almost done for tablets. The only thing left to do is add an icon above each of the four nav links, which we’ll do using sprites. If you’re already familiar with what sprites are and why we use them, skip over the following sidebar. If you’re new to sprites, read the following sidebar to learn more.
What CSS Sprites Are & Why We Use Them
Taken from video games, this concept puts many small graphics (called sprites) together into a single graphic. We’ll use one large SVG (which contains all four icons) as the background image for all the icons in the navbar. Using the CSS background-position property we can position it to show just one sprite at a time.
Sprites require a bit more work, so why don’t we just use separate SVG files as images? For users, loading four files would take longer than loading one. By reducing the number of files required, users will have a faster loading page. Sprites are better for performance, so that’s why we use them!
Adding SVG Icons as Sprites
- Switch back to index.html in your code editor.
- Starting around line 23, take a look at the nav and the links it contains.
-
We need an element to which we can add the sprite as a background image. We can use a span or div. We want the icons above the text, so div is a better choice. It’s a block element, so it will stack. Span is inline, so it would be next to the text and we’d have to add CSS to change the display to block, which is more work.
Add the div tags shown below in bold:
<li class="nav-shows"> <a href="index.html" class="active"> <div class="icon"></div> Shows </a> </li> <li> <a href="menu.html"> <div class="icon"></div> Menu </a> </li> <li> <a href="directions.html"> <div class="icon"></div> Directions </a> </li> <li> <a href="photos.html"> <div class="icon"></div> Photos </a> </li>
-
We need a way to target each icon with CSS, so add the classes shown below in bold:
<li class="nav-shows"> <a href="index.html" class="active"> <div class="icon"></div>Shows </a> </li> <li class="nav-menu"> <a href="menu.html"> <div class="icon"></div>Menu </a> </li> <li class="nav-directions"> <a href="directions.html"> <div class="icon"></div>Directions </a> </li> <li class="nav-photos"> <a href="photos.html"> <div class="icon"></div>Photos </a> </li>
- Open main.css from the css folder.
- Let’s start by applying the SVG as a background image for all icons, and giving it the size of an individual icon within the SVG file. In the min-width: 740px media query, find the header .logo rule, around line 112.
-
Below the header .logo rule, add the following:
header .icon { background-image: url(../img/nav-icon-sprite.svg); height: 45px; width: 43px; }
- Save the file.
- Preview index.html in Chrome, Firefox, or Safari.
-
Look at the icons, and you’ll see the SVG sprite has scaled to fit our defined area. It also repeats because it’s a background image, so we see it multiple times. In it you can see all four icons.
Currently there’s no width or height defined in the SVG file (because it was exported from Adobe Illustrator with the Responsive option checked on). Browsers don’t know how large to display it, so it scales up or down. We don’t want this scaling, so let’s add the original width and height (at which we designed the graphic) into the SVG file.
- Switch back to your code editor.
- From the img folder, open nav-icon-sprite.svg.
-
On line 5, add the following code shown in bold:
viewBox="0 0 43 180" width="43" height="180" enable-background="new 0 0 43 180"
NOTE: We get those dimensions by looking at the viewBox attribute.
- Save the file and close it.
- Preview index.html in a browser to see the icons all now show the same musical note (which is the first icon in our sprite).
- Switch back to main.css in your code editor.
-
To center the icons above the text, add the following code shown below in bold:
header .icon { background-image: url(../img/nav-icon-sprite.svg); height: 45px; width: 43px; margin: auto; }
- We need to adjust the background position of the sprite in order to show the appropriate icon for each link. This requires doing some math and knowing the layout of the sprite you made. We’ve already coded this up for you. From the snippets folder, open icon-bg-position.css.
- Notice the rules change the position of the background. Only three icons need styling, because the first icon is already positioned properly!
- Select and copy all the code.
- Close the file.
- You should now be back in main.css. Paste the new rules below the header .icon rule. (That’s around line 116, inside the min-width: 740px media query).
- Save the file.
Preview index.html in a browser to see the icons should now be different.
Styling the Header for Desktop
-
Resize to desktop size (blue band), but keep the window narrow enough so the logo still has the text under it (although partially covering the logo).
NOTE: As the layout stretches beyond 1300px wide, at some point the logo and address will fit next to each other, which isn’t actually bad, but in a later exercise we’ll be putting limits on the width of the page.
- The logo still has a negative bottom margin that we used to make it overhang the navbar in the tablet layout. At this size, the negative margin pulls the text up onto the logo. In the min-width: 1024px media query, find the header rule, around line 161.
-
Below the header rule, add the following new rule to remove the bottom margin:
header .logo { margin-bottom: 0; }
-
Let’s center everything in the header section. Add text-align to the header rule (directly above the header .logo rule) as shown below in bold:
header { float: left; width: calc(25% - 20px); text-align: center; }
-
Save the file and preview index.html in a browser.
The nav links can fit next to each other, which we don’t want. We want to make them stack with their icons on the left. The list items are currently set to display as inline-block from the tablet layout. Changing them to display as block will get them to stack.
- Switch back to main.css and in the min-width: 1024px media query, find the header .logo rule around line 166.
-
Below the header .logo rule add the following new rule:
nav ul li { display: block; margin-top: 15px; }
Save the file and preview index.html in a browser.
The nav has moved to the right because it’s still floating right from the tablet layout. We’ll have to remove that float.
- Switch back to main.css and in the min-width: 1024px media query, find the nav ul li rule around line 169.
-
Above the nav ul li rule, add the following new rule:
header nav { float: none; }
- Save the file and preview index.html in Chrome, Firefox, or Safari. We’re getting closer. Next let’s move the icons to the left of the text.
- Switch back to main.css and in the min-width: 1024px media query, find the nav ul li rule around line 172.
-
Below the nav ul li rule, add the following new rule:
nav ul li .icon { display: inline-block; margin-right: 10px; vertical-align: middle; }
- Save the file and preview index.html in Chrome, Firefox, or Safari. We’re almost done! We want these nav links left aligned.
- Switch back to main.css and in the min-width: 1024px media query, find the nav ul li rule around line 172.
-
Above the nav ul li rule add the following new rule:
nav ul { text-align: left; }
- Save the file and preview index.html in Chrome, Firefox, or Safari. The nav items are left aligned, but we want that whole section centered. Because the unordered list is a block item, it fills the width of its container. If we changed it to inline-block though its width would collapse to just the content. That inline-block item would then honor the text alignment of the header container, which is currently set to center!
-
Switch back to main.css and add the following to the nav ul you just created (around line 172):
nav ul { text-align: left; display: inline-block; }
Save the file and preview index.html in Chrome, Firefox, or Safari. The desktop now has a nice looking nav. Resize through down to mobile and see how the nav looks great at any size!
Bonus: Fixing the Opacity of Nav Icons
- Preview index.html in Chrome or Safari. (We’re going to see a weird issue that we’ve noticed in these browsers.)
- Resize the layout to the desktop size (blue border) and notice that the active link (SHOWS) icon and text are white, while the other text/icons are gray. That’s exactly as we intended.
- Resize the window to the tablet size (green border).
-
Notice that the nav icons here are all white. The right three icons should be gray, so what’s going on?
When the links and their nested icon divs are both inline (desktop layout), the opacity is properly inherited and applied to the nested div. When the nested divs do not match the parent’s display type (tablet layout), the opacity is not being inherited. We’ll need to explicitly tell it to be inherited.
- Switch to main.css in your code editor.
- In the general styles, find the header nav li a rule around line 53.
-
Below the header nav li a rule, add the following new rule:
header nav li a div { opacity: inherit; }
Save the file and preview index.html in Chrome or Safari. The icons should now properly inherit the opacity and match the color of the text!
Bonus: Fixing a Bug in IE & Edge
- If possible, preview index.html in Internet Explorer or Microsoft Edge.
-
Notice that the logo looks fine at the mobile and phablet sizes, but at tablet and desktop size it gets squished (as shown below)!
We set the CSS width directly on the SVG image, which typically works in IE and Edge, but not this time! We’ll need to specify the proper height to help them out. We could do this either in CSS or in the SVG file directly. We’ve already seen how to edit the SVG file, so this time let’s fix it via CSS.
- Switch to main.css in your code editor.
In the min-width: 740px media query find the header .logo rule around line 115.
- Add the height as shown below in bold:
header .logo { width: 150px; height: 150px; margin: -35px 0 }
Save the file and preview index.html in Internet Explorer or Microsoft Edge to see the logo is now the correct height again!