Learn to add and position images alongside text using the float property in this comprehensive tutorial that covers topics such as adding a hero image, fluid images, floating images, class selectors, and margins in HTML and CSS.
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.
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 HTML & CSS tutorial:
Adding a hero image, Fluid images, Floating images, Class selectors, Margins
Exercise Preview
Exercise Overview
In this exercise, you’ll add some images and learn how to position them alongside text by using the float property. Additionally, you’ll review the CSS class selector.
-
If you completed the previous exercise, sanfran.html should still be open, and you can skip the following sidebar. If you closed sanfran.html, re-open it now. We recommend you finish the previous exercises (3B–3C) before starting this one. If you haven’t finished them, do the following sidebar.
If You Did Not Do the Previous Exercises (3A–3B)
- Close any files you may have open.
- On the Desktop, go to Class Files > Web Dev Class.
- Delete the Revolution Travel folder if it exists.
- Duplicate the Revolution Travel Ready for Floats folder.
- Rename the folder to Revolution Travel.
Adding a Hero Image
In web design lingo, a hero image is a large banner-style image prominently placed at the top of a webpage. Its purpose is to provide a sort of visual overview of the page’s content. Let’s add a hero image to our page about visiting San Francisco.
-
In sanfran.html, add an image inside main, above the h1 tag:
<main> <img src="images/san-fran-lombard.jpg" alt="San Francisco Landmark: Lombard Street"> <h1>Featured Location: San Francisco, California</h1>
- Save the file and preview it in a browser to see the image.
- Looks good! Resize the window from wide to narrow and notice the image does not scale down to fit within the window. Let’s fix that.
- Return to your code editor.
-
In the style tag at the top, below the body rule, add the following new rule:
body { font-family: sans-serif; } img { max-width: 100%; }
This max-width ensures images will not be wider than 100% of the width of its parent container. It will not scale images up beyond their native width, but will scale them down to fit within their container. For this to work properly, you should remove the width and height attributes on any img tags that might be scaled.
- Save the file and preview it in a browser.
Resize the window and make sure the image scales down to fit within the browser window. Perfect!
Adding More Images
- Return to sanfran.html in your code editor.
- A couple paragraphs below the hero image, find the h2 for Things to Do. We’ll add three images to this section of the page.
-
Add the first image inside the paragraph that starts with Being on Alcatraz Island:
<p><img src="images/logo-alcatraz.jpg" alt="Alcatraz Cruises"> Being on Alcatraz Island is a unique experience...
-
In the next paragraph (which begins with Fisherman’s Wharf) add the following image:
<p><img src="images/logo-fishermans-wharf.jpg" alt="Fisherman's Wharf"> Fisherman's Wharf is one of San Francisco's most...
-
In the next paragraph (which begins with For a great outdoor activity) add the following image:
<p><img src="images/logo-blazing-saddles.jpg" alt="Blazing Saddles"> For a great outdoor activity that involves exercise, rent a bike!...
- Save the file, return to the browser, and reload the page.
There should be three images, each at the beginning their respective paragraphs. Notice the awkward alignment. We’d like the images to “float” alongside the text on the left and right (like text wrap in a print layout), so we’ll use CSS float property.
Floating the Images with Class Selectors
-
Return to sanfran.html in your code editor.
If the goal was to float all images on the page to the left of the text (or, for that matter, to the right of the text), we could create a new rule for the img tag. Using a tag selector allows us to target all of the same type of element with the same style.
The goal here, however, is to float just some of the images on the page. What’s more, we’d like some to float left and others to float right. Class selectors are the best selector for the job. We can assign them just to specific elements and we can reuse them as many times we need, wherever we like on the page.
-
Right before the closing
</style>
tag, after the rule for footer, add the following new rule. (Remember the dot before img-left is what makes it a class selector.)footer { width: 90%; max-width: 800px; } .img-left { float: left; } </style>
-
Now that you created a new rule, you need to assign the class to the images you actually want to float left. Find the image tag for the Alcatraz Cruises logo, and add the following bold class attribute and value:
<img src="images/logo-alcatraz.jpg" class="img-left" alt="Alcatraz Cruises">
- Save the file, return to the browser, and reload the page.
- Scroll down to the Things to Do section. The Alcatraz Cruises logo should be to the left of the text. Let’s add another rule to float the next image to the right.
-
Return to sanfran.html in your code editor and just after the rule for .img-left, add the following new rule (in bold):
.img-left { float: left; } .img-right { float: right; } </style>
-
Now let’s assign the class to the image you want to float right. Find the code for the Fisherman’s Wharf logo and add the following bold code:
<img src="images/logo-fishermans-wharf.jpg" class="img-right" alt="Fisherman's Wharf">
-
While you’re here, let’s make the third image float left to match the first image. On the Blazing Saddles image, add the following bold code:
<img src="images/logo-blazing-saddles.jpg" class="img-left" alt="Blazing Saddles">
- Save the file, return to the browser, and reload the page.
Scroll down to see the three images floating nicely beside their paragraphs. The graphics are close to the text, so let’s add a margin to improve the spacing.
Adding Margin to the Floated Images
- Return to sanfran.html in your code editor.
-
Add the following margin property to the .img-left rule:
.img-left { float: left; margin-right: 15px; }
-
Also add margin to the .img-right rule as follows:
.img-right { float: right; margin-left: 15px; }
Save the file, return to the browser, and reload the page to see the improved layout.