Enhance your understanding of how to refine the spacing and width of a webpage layout using HTML & CSS. This tutorial covers various topics such as removing extra space below an image, setting a max-width, understanding the difference between ID & class selectors, and more.
Key Insights
- The tutorial provides a detailed guide on fine-tuning the layout of a website, specifically focusing on a site called 'Hipstirred'. This involves modifying the HTML and CSS code to refine the spacing and width of the layout.
- It includes instructions on how to add a logo to a webpage header and how to remove the extra space that usually appears below an image on a webpage.
- The tutorial also provides insights on how to set an outer-wrapper and max-width for webpage content, and how to set an inner-wrapper for specific sections of a webpage.
- It explains the difference between ID and class selectors in CSS. While ID selectors signify uniqueness and are meant to be used once per element per page, class selectors are meant to style multiple elements in the same way.
- The tutorial recommends setting a max-width value to a standard resolution such as 1280 pixels for real-world design. This allows the content to have an edge-to-edge look for users with standard resolution, while capping off the content when it gets too wide for users with a higher resolution.
- Finally, it suggests using resources like gs.statcounter.com and Google Analytics to understand web analytics and gain insights into site visitors and their behaviors.
Grasp the nuances of front-end web development with this detailed HTML & CSS tutorial that covers topics like removing extra space below an image, setting a max-width, understanding the difference between ID and class selectors, and more.
This exercise is excerpted from Noble Desktop’s past front-end web development training materials and is compatible with updates through 2022. To learn current skills in web development, check out our coding bootcamps in NYC and live online.
Topics Covered in This HTML & CSS Tutorial:
Removing the Extra Space Below an Image, Setting a Max-width, Outer & Inner Wrappers, the Difference Between ID & Class Selectors
Exercise Preview
Exercise Overview
In this exercise, you will refine the spacing and width of the layout for the Hipstirred site. Along the way, you’ll investigate wrapping content in container divs and firm up your understanding of how an ID selector differs from a class selector.
- We’ll be using a new folder of provided files for this exercise. Close any files you may have open in your code editor to avoid confusion.
- For this exercise we’ll be working with the Hipstirred Box Model folder located in Desktop > Class Files > Web Dev Class. You may want to open that folder in your code editor if it allows you to (like Visual Studio Code does).
- Open index.html from the Hipstirred Box Model folder.
-
Preview index.html in Chrome to see what we have so far. (We will be using Chrome’s DevTools.)
Let’s start by adding the logo to the header, and then styling the footer.
NOTE: We recommend leaving index.html open in Chrome as you work, so you can reload the page to see the changes you make in the code.
Adding the Logo to the Header
- Return to index.html in your code editor.
-
In the header add the following image:
<header> <img src="images/logo.png" height="36" width="105" ALT="Hipstirred"> </header>
NOTE: We’re coding a width and height for this logo because it’s always going to be a fixed size in the layout. If the size was going to be flexible, we’d omit the height and width.
- Save the file.
Return to the browser to reload the page. The logo is way too tight against the top-left edge, but before we add space around it, notice that there’s a little extra space below the logo. Why is that?
Removing the Extra Space Below an Image
Images are rendered on a line of text as though they’re part of a paragraph, even though they’re not in a paragraph tag. By default, text (and images) are vertically aligned to the text baseline (where the bottom of each letter is positioned as you would when writing on lined paper).
Baseline makes sense for text, including lowercase characters like j and g. However, for images, which don’t have descenders, the default vertical-align: baseline creates extra, unfilled space below them. A simple fix is to vertically align images to the bottom (middle will also work in this case), rather than the baseline. The following diagram illustrates this:
- Return to index.html in your code editor.
-
Add a class of logo to the image in the header, as follows:
<header> <img src="images/logo.png" class="logo" height="36" width="105" ALT="Hipstirred"> </header>
- Save index.html.
- Open main.css from the Hipstirred Box Model folder.
-
Add the following new rule below the rule for p:
.logo { vertical-align: middle; }
NOTE: Instead of middle we could also use bottom (as previously explained).
- Save main.css.
- Switch back to Chrome and reload index.html to see that the extra space below the logo is now gone.
- Now let’s add space around the logo. Switch to main.css in your code editor.
-
Add the following new rule above the .logo rule:
header { padding: 20px; }
- Save main.css.
Return to the browser and reload index.html. The header looks better with space around the logo.
Styling the Footer
- Switch to main.css in your code editor.
-
Let’s add a background color to the footer to help separate it from the rest of the page. At the bottom of the file, just below the main rule, add the following new rule:
footer { background-color: #e9d8c8; padding: 20px; }
- Save main.css.
- Return to Chrome and reload index.html. We like how the footer is looking with the background color, but the copyright paragraph doesn’t look centered vertically. What’s going on? Let’s inspect.
- CTRL–click (Mac) or Right–click (Windows) on the footer and choose Inspect.
-
In the Elements panel, select the
<p>
tag in the footer. You’ll see it highlighted in the browser and you should also see the p rule in the Styles tab of the DevTools window:Bottom margin is good for typical paragraphs (like those in the main section of the page), but not when we only have this one paragraph in the footer. Luckily descendant selectors allow us to target an element when it’s inside of another element. We want to style the p tags only inside the footer.
- Return to main.css in your code editor.
-
Let’s remove the space below the copyright, and make it smaller while we’re at it. At the bottom of the file, just below the footer rule, add the following new rule:
footer p { margin: 0; font-size: 14px; }
- Save main.css.
- Return to the browser and reload index.html. Now the footer and copyright text look better, and copyright text is vertically centered in the footer’s background color.
Keep the browser window open, but close the DevTools.
Setting Limits with an Outer-Wrapper & Max-Width
-
Resize your browser window as wide as it will go. This hero image looks good at 1280 pixels or less. If you make the window wider than 1280 pixels, the hero image starts to become pixelated. We could use a bigger image, but another option is to limit the width of the hero area.
To control the overall width of the content on the page, we can wrap a
<div>
around all the content and then set limits to its width in the CSS. - Return to your code editor and switch to index.html.
-
Wrap a div tag around all the content of the document inside the body tag.
TIP: If you’re using Visual Studio Code (and set up the wrap keystroke using the instructions in the Before You Begin section near the start of this book), you can wrap a tag around a selection by pressing Option–W (Mac) or ALT–W (Windows). Then type in the wrapper (div in this case) and hit Return (Mac) or Enter (Windows).
-
When you’re finished, check that you have an opening
<div>
(highlighted below in bold) just below the opening body tag.<body> <div> <header>
-
Check that you have a closing
</div>
(highlighted below in bold) just above the closing body tag.</footer> </div> </body> </html>
-
Give the new
<div>
an ID of wrapper:<body> <div id="wrapper"> <header> <img src="images/logo.png" height="36" width="105" ALT="Hipstirred">
- Save index.html.
- Switch to main.css.
-
Below the p rule, add the following new rule:
#wrapper { max-width: 1100px; }
- Save main.css.
- Return to the browser and reload index.html. Resize your browser to make it narrow and wide. You’ll see the content stop scaling up when you get to 1101 pixels or wider. Our max-width works, but the content isn’t centered.
- Return to main.css in your code editor.
-
Add the following new properties to the #wrapper rule:
#wrapper { max-width: 1100px; margin-right: auto; margin-left: auto; }
- Save main.css.
-
Return to the browser and reload index.html. Now the hero does not get too wide, and when the window is 1101 pixels or wider, the hero area is centered within the page.
The top part of the page looks, but scroll down to the footer. Although we’d like the copyright content to have a max-width, the background color would look nicer stretching edge-to-edge.
Setting an Inner-Wrapper
- Return to your code editor and switch to index.html.
-
What we need to do is end the current wrapper’s
</div>
just after the</main>
section and leave the footer out of the wrapper. Move the</div>
as follows:</main> </div> <footer> <p>© Hipstirred LLC</p> </footer> </body>
-
Save the file, return to the browser and reload the page to see that the footer is no longer constrained.
We’d still like to have the copyright align on the left with the Hipstirred logo. To accomplish this, we need to place a wrapper around the content inside of the footer.
- Return to index.html in your code editor.
-
As shown in bold, wrap a
<div>
tag around the<p>
inside the<footer>
and give it an ID of wrapper:<footer> <div id="wrapper"> <p>© Hipstirred LLC</p> </div> </footer>
-
Save the file, return to the browser and reload the page.
This worked, but only because the browser is giving us a pass right now. We used the same ID twice, which goes against spec. We’re seeing a “silent failure”, which means that we have written invalid code, but the browser will try to guess what our intent was and handle it accordingly.
ID Selectors Vs. Class Selectors
IDs signify uniqueness. They are meant to be used just once, per element, per page. Although the browser fails “silently” right now, this issue can come back to cause us problems down the road. Since the class selector was created specifically to style multiple elements the same way, let’s use a class for wrapper instead of an ID.
- Return to index.html in your code editor.
-
Scroll up to the first wrapper and edit the attribute as follows:
<body> <div class="wrapper"> <header>
-
Down in the footer, change id to class as well:
<footer> <div class="wrapper"> <p>© Hipstirred LLC</p> </div> </footer>
- Save the file.
- Switch to main.css.
-
Change #wrapper to .wrapper (which changes it from an ID selector to a class selector), so you end up with the following:
. wrapper { max-width: 1100px; margin-right: auto; margin-left: auto; }
Save the file, return to the browser, and reload the page. If all goes well, you won’t see any change but you can bask in the glow of your valid HTML and CSS.
Real-World Widths
We set the max-width value to 1100 pixels to make it easy to see max-width in action, even if you are working with a smaller resolution. For real-world design, though, we recommend setting a max-width to a more standard resolution, like 1280 pixels. This gives these users the edge-to-edge look we desire but also caps off the content when it gets too wide for users who have a higher resolution.
- Return to main.css in your code editor. We’ll set the max-width to be a more generous size.
-
Edit the value for the max-width property for .wrapper as follows:
.wrapper { max-width: 1280 px; margin-right: auto; margin-left: auto; }
- Save main.css.
-
Return to the browser and reload index.html. If your screen is 1280px wide or less, the hero image should fill the page. If your screen is wider than 1280px, test out the new max-width value. Nice.
NOTE: We’re not saying you always have to limit the width of your hero images, but in case you do, we wanted to show you how.
Screen Resolution Statistics
Gs.statcounter.com is a good resource for web analytics. It’s free and gives you a global picture of what’s happening on the web.
The best way to find out who’s looking at your site, what browser they’re using, what their screen resolution is, where they live, and almost anything else you’d like to know, is to use Google Analytics, the industry-standard for data. Google Analytics is free, though they have tiered pay plans for “enterprise-scale” data. Find out more at Google.com/analytics