CSS float Property
The float property places an element along the left or right side of its containing block, allowing surrounding inline content, such as text, to flow around it.
Floats are commonly used to wrap text around images and other content. Although they were historically used to build entire page layouts, CSS Flexbox and Grid are generally better choices for arranging major layout sections.
float Syntax
The float property accepts a keyword that determines whether an element floats and, if so, which side of its containing block it occupies.
Basic Syntax
selector {
float: left;
}
Available Values
.element-one {
float: none;
}
.element-two {
float: left;
}
.element-three {
float: right;
}
.element-four {
float: inline-start;
}
.element-five {
float: inline-end;
}
CSS-Wide Keywords
.element {
float: inherit;
}
.element {
float: initial;
}
.element {
float: unset;
}
.element {
float: revert;
}
.element {
float: revert-layer;
}
The initial value is none, meaning the element does not float.
The left and right values use physical directions. The inline-start and inline-end values follow the document's writing mode and text direction.
Property Values
The following values control the floating behavior of an element.
| Value | Description |
|---|---|
none |
The element does not float. This is the initial value. |
left |
Floats the element toward the left side of its containing block. |
right |
Floats the element toward the right side of its containing block. |
inline-start |
Floats the element toward the inline-start side, based on writing mode and direction. |
inline-end |
Floats the element toward the inline-end side, based on writing mode and direction. |
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed float value of the parent element. |
initial |
Restores the initial value, none. |
unset |
Restores none because the property is not inherited. |
revert |
Reverts the property according to the CSS cascade origins. |
revert-layer |
Reverts the property according to the CSS cascade layers. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | All elements, subject to layout restrictions |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
The property is not inherited and does not apply floating behavior to flex items or grid items in their respective layout contexts.
How It Works
A floated element is taken out of normal block flow, but it still affects the placement of surrounding content. Inline content can flow around the space occupied by the float.
Step 1: Add an Element
<div class="content">
<img src="image.jpg" alt="Sample illustration">
<p>This paragraph flows around the image.</p>
</div>
Step 2: Apply a Float
.content img {
float: left;
width: 150px;
}
The image moves toward the left side of its containing block, and the paragraph's inline text can occupy the space to its right.
Step 3: Add Spacing
.content img {
float: left;
width: 150px;
margin: 0 15px 10px 0;
}
The right and bottom margins create separation between the image and surrounding text.
Step 4: Contain the Float
.content {
display: flow-root;
}
The flow-root value establishes a new block formatting context, allowing the container to include the floated image in its height calculation.
Important Distinction
A floated element is not absolutely positioned. Its placement is constrained by its containing block, preceding content, and other floats.
float: left
The left value moves an element toward the left side of its containing block and allows surrounding inline content to flow along its right side.
Syntax
.image {
float: left;
}
Example
.article-image {
float: left;
width: 160px;
margin: 0 20px 10px 0;
}
HTML Example
<article>
<img class="article-image" src="image.jpg" alt="Example graphic">
<p>The paragraph wraps around the floated image.</p>
</article>
How It Works
The image occupies the left side, while text can flow into the remaining horizontal space. Once the text reaches the bottom of the float, subsequent lines can extend across the full available width.
Important Consideration
A left float uses a physical direction. For layouts that must adapt to right-to-left languages or vertical writing modes, consider inline-start.
float: right
The right value moves an element toward the right side of its containing block and allows surrounding inline content to flow along its left side.
Syntax
.image {
float: right;
}
Example
.article-image {
float: right;
width: 160px;
margin: 0 0 10px 20px;
}
HTML Example
<article>
<img class="article-image" src="image.jpg" alt="Example graphic">
<p>The paragraph wraps along the left side of the image.</p>
</article>
How It Works
The floated image occupies the right side, and the text flows into the available space to its left.
Important Consideration
A right float does not reverse the document order or reading order of the content. It changes visual placement within the layout.
float: none
The none value disables floating and returns the element to the normal layout behavior determined by its other CSS properties.
Syntax
.image {
float: none;
}
Example
.article-image {
float: left;
width: 160px;
}
@media (max-width: 600px) {
.article-image {
float: none;
display: block;
max-width: 100%;
height: auto;
}
}
How It Works
The image floats on wider screens. On narrower screens, the float is removed and the image becomes a block-level element.
Important Consideration
Setting float: none does not automatically remove margins, widths, or other styles previously assigned to the element. Adjust those properties separately when needed.
Logical Float Values
The logical values inline-start and inline-end position floats according to the document's writing mode and text direction rather than fixed physical sides.
Using inline-start
.article-image {
float: inline-start;
margin-inline-end: 20px;
}
In a typical left-to-right horizontal writing mode, inline-start corresponds to the left side. In a right-to-left horizontal writing mode, it corresponds to the right side.
Using inline-end
.article-image {
float: inline-end;
margin-inline-start: 20px;
}
The element floats toward the inline-end side, and the logical margin creates spacing on the opposite side.
Direction Comparison
| Value | LTR Horizontal Writing | RTL Horizontal Writing |
|---|---|---|
left |
Left | Left |
right |
Right | Right |
inline-start |
Left | Right |
inline-end |
Right | Left |
Important Consideration
Logical float values are useful for multilingual layouts. Check browser compatibility when supporting older browsers, particularly for logical values.
Text Wrapping Around Floats
Text wrapping is one of the primary uses of the float property. A floated image occupies space along one side while nearby inline content flows around it.
Basic Text Wrapping
.article-image {
float: left;
width: 180px;
margin: 0 20px 15px 0;
}
HTML Example
<article class="article-content">
<img class="article-image" src="image.jpg" alt="Example illustration">
<p>This paragraph wraps around the image as space allows.</p>
<p>Additional text continues below the image when needed.</p>
</article>
Using shape-outside
.round-image {
float: left;
width: 160px;
height: 160px;
border-radius: 50%;
shape-outside: circle(50%);
margin: 0 20px 15px 0;
}
The shape-outside property allows surrounding inline content to follow a circular shape rather than only the rectangular float boundary.
Important Consideration
The shape-outside property requires a floated element and an appropriate shape reference box or shape definition. It does not itself float the element.
Multiple Floated Elements
Multiple elements can float within the same containing block. Their placement depends on available space, float directions, and preceding content.
Multiple Left Floats
.box {
float: left;
width: 120px;
margin: 0 15px 15px 0;
}
The boxes are placed toward the left and can appear beside one another when sufficient horizontal space exists.
Left and Right Floats
.left-box {
float: left;
width: 120px;
}
.right-box {
float: right;
width: 120px;
}
One box moves toward the left side and the other toward the right side of the containing block.
When Floats Cannot Fit
If a float cannot fit beside preceding floats, it may be positioned lower until sufficient space is available.
Important Consideration
Float positioning is not a replacement for Flexbox or Grid when exact alignment, equal columns, or consistent spacing between layout components is required.
Clearing Floats
The clear property controls whether an element must be positioned below preceding floats on specified sides.
Clear Both Sides
.next-section {
clear: both;
}
The element is moved below preceding left and right floats that would otherwise affect its position.
Clear Left Floats
.next-section {
clear: left;
}
Clear Right Floats
.next-section {
clear: right;
}
HTML Example
<div class="content">
<img class="floated-image" src="image.jpg" alt="Example image">
<p>This text wraps around the image.</p>
<h3 class="next-section">Next Section</h3>
<p>This content begins below the float.</p>
</div>
CSS Example
.floated-image {
float: left;
width: 150px;
margin-right: 15px;
}
.next-section {
clear: both;
}
Important Consideration
The clear property affects the placement of the clearing element. It does not automatically make a parent container enclose all its floated children.
Containing Floats
A parent containing only floated children may not expand to include their height because floats are removed from normal block flow.
Using display: flow-root
.container {
display: flow-root;
}
.image {
float: left;
width: 150px;
}
The parent establishes a block formatting context and encloses its floated descendants.
HTML Example
<div class="container">
<img class="image" src="image.jpg" alt="Example image">
<p>Text wraps around the floated image.</p>
</div>
Traditional Clearfix
.clearfix::after {
content: "";
display: block;
clear: both;
}
The clearfix technique inserts a generated clearing box after the container's content, helping the parent include preceding floats in its height.
Using overflow
.container {
overflow: auto;
}
Some non-visible overflow values establish a block formatting context that contains floats. However, they can also introduce scrolling or clipping behavior.
Recommended Approach
For modern layouts, display: flow-root is usually the clearest way to establish a block formatting context specifically for containing floats.
float and display
Floating an element can change how its display type is interpreted. For example, an inline element with a non-none float value is blockified for layout purposes.
Floating a span
<p>
<span class="label">Note</span>
This text flows around the floated label.
</p>
CSS Example
.label {
float: left;
padding: 10px;
margin-right: 15px;
background-color: #e2e8f0;
}
Although span is normally inline, floating it changes its layout behavior so it can accept dimensions and participate as a float.
Floats and Flex Items
.container {
display: flex;
}
.item {
float: left;
}
The float declaration does not float an element that is a flex item. Flexbox controls its placement instead.
Floats and Grid Items
.container {
display: grid;
}
.item {
float: right;
}
Likewise, floating does not affect the placement of an element that is a grid item.
Important Consideration
Floats are also ineffective on absolutely positioned elements. Use the layout mechanism appropriate to the element's formatting context.
Floats vs. Modern Layout
Floats were widely used for page layouts before Flexbox and Grid became available. Modern CSS provides more predictable tools for arranging major layout components.
Float-Based Layout
.sidebar {
float: left;
width: 25%;
}
.main-content {
float: right;
width: 75%;
}
This approach can require clearing, careful width calculations, and additional handling when content changes.
Flexbox Alternative
.layout {
display: flex;
gap: 20px;
}
.sidebar {
flex: 0 0 25%;
}
.main-content {
flex: 1;
min-width: 0;
}
Flexbox provides direct control over item sizing, alignment, and available space without requiring floats or clearfix techniques.
Grid Alternative
.layout {
display: grid;
grid-template-columns: 1fr 3fr;
gap: 20px;
}
Grid is particularly useful for layouts that require defined columns or coordinated rows and columns.
Choosing a Layout Method
| Layout Method | Best Use |
|---|---|
float |
Wrapping text around images or other content. |
| Flexbox | Arranging items along one main axis with flexible sizing and alignment. |
| Grid | Creating layouts with defined rows and columns. |
Important Consideration
Floats remain useful for text wrapping. They are not obsolete, but they are generally unnecessary for modern page structure and component alignment.
Responsive Floats
Floated elements can adapt to smaller screens using flexible dimensions and media queries.
Flexible Image Width
.article-image {
float: left;
width: 35%;
max-width: 220px;
height: auto;
margin: 0 20px 15px 0;
}
The image width responds to the available space while remaining limited by its maximum width.
Remove the Float on Small Screens
@media (max-width: 600px) {
.article-image {
float: none;
display: block;
width: 100%;
max-width: 320px;
margin: 0 0 15px;
}
}
The image becomes a block-level element on smaller screens, allowing the text to appear below it rather than wrapping into a narrow column.
Logical Margins
.article-image {
float: inline-start;
margin-inline-end: 20px;
margin-block-end: 15px;
}
Logical properties help the layout adapt to different writing modes and text directions.
Important Consideration
Text wrapping can become difficult to read when the available space beside a floated image is too narrow. Consider removing the float at smaller container or viewport sizes.
float Example
This example compares a left-floated element, a right-floated element, and an element with floating disabled. The surrounding text demonstrates how each value affects normal content flow.
HTML Example
<div class="float-demo">
<h3>float: left</h3>
<div class="float-section">
<div class="float-box float-left">Left</div>
<p>This paragraph wraps around the box on its right side. The text continues beside the float until there is no longer enough vertical space for wrapping.</p>
</div>
<h3>float: right</h3>
<div class="float-section">
<div class="float-box float-right">Right</div>
<p>This paragraph wraps around the box on its left side. The floated element occupies the right edge of its containing block.</p>
</div>
<h3>float: none</h3>
<div class="float-section">
<div class="float-box float-none">None</div>
<p>This paragraph follows the box in normal block layout without wrapping beside it.</p>
</div>
</div>
CSS Example
.float-demo .float-section {
display: flow-root;
padding: 12px;
margin-bottom: 20px;
background-color: #f1f5f9;
}
.float-demo .float-box {
box-sizing: border-box;
width: 110px;
height: 80px;
padding: 10px;
background-color: #336699;
color: #fff;
text-align: center;
font-weight: bold;
}
.float-demo .float-left {
float: left;
margin: 0 15px 10px 0;
}
.float-demo .float-right {
float: right;
margin: 0 0 10px 15px;
}
.float-demo .float-none {
float: none;
margin-bottom: 10px;
}
.float-demo p {
margin: 0;
line-height: 1.6;
}
float: left
This paragraph wraps around the box on its right side. The text continues beside the float until there is no longer enough vertical space for wrapping.
float: right
This paragraph wraps around the box on its left side. The floated element occupies the right edge of its containing block.
float: none
This paragraph follows the box in normal block layout without wrapping beside it.
In the first example, text wraps to the right of the floated box. In the second, text wraps to the left. In the third, the box remains in normal block flow and the paragraph begins below it.
Each section uses display: flow-root so the container encloses its floated content and does not overlap the following example.
Common Mistakes
Several common misunderstandings can lead to unexpected float behavior.
Expecting the Parent to Contain Floats Automatically
A parent containing floated children may not expand to their height. Use display: flow-root or another appropriate containment technique.
Using Floats for Complete Page Layouts
Floats can require additional clearing and width calculations. Flexbox and Grid are usually better choices for major page layout structures.
Forgetting Margins Around Floated Images
Without spacing, text may appear too close to a floated image. Add appropriate physical or logical margins.
Expecting float to Work on Flex Items
The float property does not affect the layout of flex items or grid items.
Confusing float with position
Floats participate in a specialized formatting model. They do not behave like absolutely positioned elements.
Ignoring Clear Behavior
Following content may continue beside a float unless it is cleared or otherwise separated from the float.
Using Physical Directions for Multilingual Layouts
The left and right values do not adapt to text direction. Consider logical values when the design should follow the inline axis.
Important Considerations
Keep the following points in mind when using float:
- Purpose: Positions elements along a side of their containing block and allows inline content to wrap around them.
- Initial value:
none. - Inheritance: The property is not inherited.
- Accepted values:
none,left,right,inline-start, andinline-end. - left: Floats an element toward the physical left side.
- right: Floats an element toward the physical right side.
- inline-start: Floats toward the logical inline-start side.
- inline-end: Floats toward the logical inline-end side.
- none: Disables floating.
- Normal flow: Floats are removed from normal block flow but influence surrounding content.
- Text wrapping: Inline content can flow beside floated elements.
- Margins: Provide spacing between floats and nearby content.
- Multiple floats: Can occupy the same line when space permits.
- clear: Controls placement below preceding floats.
- flow-root: Establishes a block formatting context that contains floats.
- Clearfix: A traditional technique for containing floated descendants.
- display: Floating can cause an element's display type to become blockified.
- Flexbox and Grid: Floats do not control the placement of flex or grid items.
- Absolute positioning: Floating does not affect absolutely positioned elements.
- shape-outside: Can customize the shape around which text flows.
- Responsive design: Floats can be removed or adjusted with media queries.
- Modern layouts: Flexbox and Grid are generally preferable for page structure.
- Animation: Changes between float values are discrete.
Use float primarily when content should wrap around an element. For larger layout structures, prefer Flexbox or Grid, and use display: flow-root when a container needs to enclose its floated descendants.
Browser Support
The CSS float property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.
The physical values left, right, and none have longstanding browser support. The logical values inline-start and inline-end are newer and may require additional compatibility consideration for older browsers.
Supported Example
.image {
float: left;
margin-right: 15px;
}
Logical Value Example
.image {
float: left;
float: inline-start;
margin-inline-end: 15px;
}
Browsers that do not recognize inline-start can retain the earlier left declaration. Supporting browsers use the logical value.
Feature Detection
@supports (float: inline-start) {
.image {
float: inline-start;
}
}
Compatibility Considerations
When supporting older browsers, test logical float values and related logical margin properties. The physical float values remain suitable for layouts that intentionally use fixed left or right positioning.
For current compatibility information, visit Can I Use? and search for the specific float feature or value.
Specifications
The float property is defined in CSS specifications covering the traditional float model and newer logical directions.
The CSS Display Module describes the formatting behavior associated with floating and blockification.
CSS Display Module Level 3: Box Layout Transformations
For logical floating directions, see the CSS Logical Properties and Values specification.
CSS Logical Properties and Values Level 1: Floating and Clearing
