CSS Text Flow Around Image

By Xah Lee. Date: . Last updated: .

This page shows you how to use CSS so that text wraps around image, using CSS float property.

Floating Example

Here's a example:

lilies-s The Time Traveller (for so it will be convenient to speak of him) was expounding a recondite matter to us. His grey eyes shone and twinkled, and his usually pale face was flushed and animated. The fire burned brightly, and the soft radiance of the incandescent lights in the lilies of silver caught the bubbles that flashed and passed in our glasses. Our chairs, being his patents, embraced and caressed us rather than submitted to be sat upon, and there was that luxurious after-dinner atmosphere when thought runs gracefully free of the trammels of precision. And he put it to us in this way—marking the points with a lean forefinger—as we sat and lazily admired his earnestness over this new paradox (as we thought it) and his fecundity. (from Time Machine.)

You can see the text flowing around a image. This is done using CSS with float.

Here's the HTML:

<p>
<img id="floatMe" src="lilies.png" alt="lilies" width="167" height="106">
The Time Traveller …
</p>

The CSS code is this:

#floatMe {float:left}

The float can have a value of left or right. When it's left, it floats starting from left. If any ancestor element doesn't have any width set, then the bounding box is the window.

When a element is floating, anything will go around it to avoid collision or overlap. (except elements that have their own layer with position. 〔➤see CSS Position: static, relative, fixed, absolute〕)

You can have multiple consecutive HTML elements that are all float:right, then they'll behave as a sequence of <img …> elements, flowing from left to right. For a example, see: Flowing List Items.

Stop Flowing

If you have many floating elements, the position of the last item will be the position the next non-floating item begin. For example, you might have:

floatElement floatElement floatElement … <h2></h2>

The <h2> will be shown at the position right after the last flow, as if it is part of the flow.

If you don't want that, you need to stop the flow with clear:left in the element that comes after the float. Like this:

<h2 style="clear:both">A New Beginning</h2>

The clear can have values of left, right, both.

Usually, it is best to use this element to clear the floats:

<hr style="visibility:hidden; clear:both">

Reference

Visual formatting model#propdef-float

Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.