05.03 · Advanced CSS / Learn + practice · 20 min + video
Wrap text around a floated image
Move a photo to one side, let the words wrap around it, and stop the next section from drifting beside it. Follow Rob’s video or practice in six small steps.
Activity 3 of 5 in Advanced CSS
CSS Basics / 05.03
Wrap text around a floated image
Not started
Use float for its original purpose: wrapping text around a picture.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

ORIGINAL WALKTHROUGH · Code-along
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Open video & code editor (opens in a new tab)Let text wrap around a picture
A photo in an article can sit on the left or right while the following words flow around it. The text continues across the available width once it is below the picture. An ordinary image is inline by default; a large image can still occupy most of a line.
The float property is useful for this print-style text wrapping. Changing a photo to float does not turn every element on the page into a float. Later content can wrap alongside it until its position, clearing or formatting context prevents that.
Explore float and clear
Choose None, Left or Right. Watch the first paragraph and the next heading beside the photo. Then turn on Clear the next heading and look for that heading below the picture. The long paragraph shows how text can continue below the image when clearing is off.
Choose a float direction, then clear the next heading. Compare the highlighted CSS with the photo, surrounding text and heading in the preview.
.photo {
float: none;
width: 140px;
margin: 0 16px 16px;
}
.next-heading {
clear: none;
}.photo {
float: none;
width: 140px;
margin: 0 16px 16px;
}
.next-heading {
clear: both;
}.photo {
float: left;
width: 140px;
margin: 0 16px 16px;
}
.next-heading {
clear: none;
}.photo {
float: left;
width: 140px;
margin: 0 16px 16px;
}
.next-heading {
clear: both;
}.photo {
float: right;
width: 140px;
margin: 0 16px 16px;
}
.next-heading {
clear: none;
}.photo {
float: right;
width: 140px;
margin: 0 16px 16px;
}
.next-heading {
clear: both;
}YOUR PAGE PREVIEW
No float: the text starts after the image. Choose Left or Right to wrap the following text.
Put the gap between the photo and words
For a left-floated photo, the words sit on the right, so margin-right creates their gap. For a right-floated photo, use margin-left. A bottom margin separates the picture from content below it. Margins add outside space; they do not change the image’s own width.
.article-photo {
float: right;
width: 140px;
margin-left: 16px;
margin-bottom: 16px;
}Clear a following heading or footer
A short paragraph can end before its floated image does. The following heading or footer may then sit beside the same image. Give that following block clear: both to move it below relevant earlier floats on either side. Clearing does not remove float from the image.
In the video, Rob clears h2 and footer. The right photo needs margin-left for its text gap, as the narration explains.
.next-section,
.page-footer {
clear: both;
}Practice a newsletter layout
Work through six checked steps using a different study-club newsletter. Size and float the first photo, add a gap, clear the next heading, float the second photo right, then keep the footer below it. Your own code carries forward and saves when signed in.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)6 steps · Saved work · Instant feedback
Contain the float inside its parent
A parent may not enclose the height of its floated children in normal flow. Setting display: flow-root on the parent creates a formatting context that contains those floats. That is different from clear on a heading inside that parent. You may still need to clear a following heading when you want it below an earlier photo.
The repair ticket below already gives the article flow-root. Keep it while fixing the photo direction, text gap and following blocks. For full-page layouts, later Flexbox and Grid lessons provide more suitable tools.
.article {
display: flow-root;
}Your repair ticket
The newsletter below has the right content, but its photos float on the wrong side and the following blocks do not clear them. Use the checklist to repair the CSS. Check challenge tests both photos and measures the heading, footer and article at narrow and wide preview sizes.
YOUR TURN
Repair the newsletter layout
The photos float on the wrong side, the text has no gap, and the next heading and footer sit beside earlier photos. Repair the CSS using the checklist.
- In styles.css, find the rule with selector
.story img. Set itsfloatproperty toleft. - In that same rule, set
margin-rightto16px. Keep its width at120px, height at180pxand bottom margin at16px. - Add a rule for
.story h2and a rule for.story footer. Set theclearproperty tobothin each. - Keep the story container display set to
flow-root, both photos and all page content.
Edit styles.css and watch the preview update after you pause typing. Open index.html to inspect or repair its stylesheet link. Run or Check saves immediately. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Fix the image rule
Both images share .story img. Float left and put the text-side gap on their right.
Clear the following blocks
Keep the photos floated. Apply clear: both to the following h2 and footer, rather than to the images.
Containment is different from clearing
The article already uses flow-root to contain its floats. That does not make headings inside it clear a previous image.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Float places a picture to one side so following text can wrap. Margins separate the picture from words. Clear moves a following block below earlier floats, while flow-root lets the parent contain its floated children.
/* Repair only the float, text gap and clearing. Keep the supplied sizes and content. */
body {
font-family: Arial, sans-serif;
line-height: 1.5;
margin: 24px;
}
.story {
display: flow-root;
max-width: 520px;
padding: 16px;
border: 2px solid navy;
}
.story img {
float: left;
width: 120px;
height: 180px;
object-fit: cover;
margin-right: 16px;
margin-bottom: 16px;
}
.story h2,
.story footer {
clear: both;
}
.story footer {
background-color: #e0f2fe;
padding: 12px;
}Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)