CSS: Text Flow Around Image

By Xah Lee. Date: . Last updated: .

The float property can make text flow around an element such as an image.

Text Flow Around Image Example

tile B
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 flow around a image. This is done by making the image float with attribute float.

Here's HTML:

<div id="x01759">

<img id="xfloat947" src="i/tile_B.png" alt="tile B" width="128" height="128" />

The Time Traveller (for so it will be convenient to speak of him) was expounding a recondite matter to us…


Here's CSS:

#xfloat947 {

#x01759 {

The float can have a value of left or right. When it's left, it aligns to the left.

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 Property])

Multiple consecutive HTML elements with float:left behave as sequence of inline-block elements such as sequence of HTML: Image Tag , flowing from left to right.

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">

CSS Layout

CSS Layers

JavaScript Using Css Layers