thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

CSS Basics / 05.03

Wrap text around a floated image

Not started

Use float for its original purpose: wrapping text around a picture.

More options

Your lesson progress

Reading & video: not startedNewsletter floats practice: not startedFloat repair: not started

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.

Rob places a small photo at left within a newspaper-like article. Multiple lines of text visibly wrap to RIGHT of image, then full width BELOW its bottom. Article outline encloses floated image. Clear correct float behavior.
Rob’s comic strip · Advanced CSS

ORIGINAL WALKTHROUGH · Code-along

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

index.html
index.htmlhtml · example
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Floats</title>
    <style>
      * {
        /* outline: 1px solid red; */
      }
      body {
        font-family: Arial, sans-serif;
        line-height: 1.6;
      }

      .float-right {
      }

      .float-left {
      }

      h2 {
      }
      footer {
        text-align: center;
        margin-top: 20px;
        font-size: 0.8em;
        color: #555;
      }
    </style>
  </head>
  <body>
    <h2>Float Left</h2>

    <p>
      In this example, the image will float to the left in the paragraph, and
      the text in the paragraph will wrap around the image.
    </p>

    <img
      src="https://files.reimage.dev/frontline-designs/6916447d0ee3/original"
      alt=""
      class="float-left"
    />
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus
      imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae
      scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec
      congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet.
      Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent
      convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis
      dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc
      venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida
      venenatis. Integer fringilla congue eros non fermentum. Sed dapibus
      pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.
    </p>

    <!-- Float Right -->
    <h2>Float Right</h2>
    <p>
      In this example, the image will float to the right in the paragraph, and
      the text in the paragraph will wrap around the image.
    </p>
    <img
      class="float-right"
      src="https://files.reimage.dev/frontline-designs/36953e531b54/original"
      alt="float right"
    />

    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor iste ullam,
      voluptate praesentium voluptatum sint pariatur. Harum eius laboriosam sit
      saepe, nostrum officiis expedita illo! Lorem ipsum dolor sit amet
      consectetur adipisicing elit. Fuga nulla consectetur vero corporis tempora
      quia unde vitae dolores officiis iste. Culpa optio quasi fugit fuga omnis
      placeat repellat nobis voluptatum iste eligendi earum aspernatur vitae
      voluptatem quaerat suscipit, voluptates odio ducimus minima ut dolorem at
      esse laudantium deserunt dolores? Corrupti.
    </p>

    <footer>Created By PreCodeCamp</footer>
  </body>
</html>

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.

styles.csscss · example
.photo {
  float: none;
  width: 140px;
  margin: 0 16px 16px;
}
.next-heading {
  clear: none;
}

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.

A text-side gap for a right float
styles.csscss · example
.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.

Start following blocks below floats
styles.csscss · example
.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.

Code example
styles.csscss · example
.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.

To pass this challenge
  • In styles.css, find the rule with selector .story img. Set its float property to left.
  • In that same rule, set margin-right to 16px. Keep its width at 120px, height at 180px and bottom margin at 16px.
  • Add a rule for .story h2 and a rule for .story footer. Set the clear property to both in 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.

Desired result

Compare this expected page with your preview. Your code stays in your editor.

Ready to checkRun the checks to see your results here.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Use float for text wrapping, margin for the text-side gap, and clear for following blocks. A flow-root parent contains its floats. Use Flexbox or Grid for general page layouts.