CSS: Float Layout. (text flow around image)

By Xah Lee. Date: . Last updated: .

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

Example. text flow around image

xah java logo
Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”.

Code

<div class="box-V62P3">
 <img class="img-float-hV3Rx" src="../java-a-day/java_logo/xah_java_logo.svg" alt="xah java logo" style="width:80px" />

 <div class="text-qRwFJ">Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”.</div>
</div>

<div class="break-x982ed"></div>
.box-V62P3 {
 max-width: 300px;
}

.text-qRwFJ {
 line-height: 1.6;
}

.img-float-hV3Rx {
 float: left;
 margin: 8px;
}

.break-x982ed {
 clear: both;
}

Float property values

The float can have a value of left or right.

.my-float {
 float: left;
 /* float: right; */
 margin: 8px;
}

When a element is floating, other elements goes around it to avoid collision or overlap.

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

Clear float

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:

<div style="clear: both"></div>

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

/* values for clear property */
.x {
 clear: left;
 clear: right;
 clear: both;
}

Property shape-outside (flow around a circle, polygon, etc)

note, there is a shape-outside property that defines a shape such as circle, polygon, etc and text flow around that.