files/en-us/web/css/reference/properties/float/index.md
The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).
{{InteractiveExample("CSS Demo: float")}}
float: none;
float: left;
float: right;
float: inline-start;
float: inline-end;
<section class="default-example" id="default-example">
<div class="example-container">
<div class="transition-all" id="example-element">Float me</div>
As much mud in the streets as if the waters had but newly retired from the
face of the earth, and it would not be wonderful to meet a Megalosaurus,
forty feet long or so, waddling like an elephantine lizard up Holborn Hill.
</div>
</section>
.example-container {
border: 1px solid #c5c5c5;
padding: 0.75em;
text-align: left;
width: 80%;
line-height: normal;
}
#example-element {
border: solid 10px #efac09;
background-color: #040d46;
color: white;
padding: 1em;
width: 40%;
}
A floating element is one where the computed value of float is not none.
As float implies the use of the block layout, it modifies the computed value of the {{cssxref("display")}} values, in some cases:
| Specified value | Computed value |
|---|---|
inline | block |
inline-block | block |
inline-table | table |
table-row | block |
table-row-group | block |
table-column | block |
table-column-group | block |
table-cell | block |
table-caption | block |
table-header-group | block |
table-footer-group | block |
inline-flex | flex |
inline-grid | grid |
| other | unchanged |
[!NOTE] When accessing a CSS property in JavaScript through the {{domxref("HTMLElement.style")}} object, single-word property names are used as is. Although
floatis a reserved keyword in JavaScript, the CSSfloatproperty is accessed asfloatin modern browsers. In older browsers, you must usecssFloatto access thefloatproperty. (This is similar to how the "class" attribute is accessed as "className" and the "for" attribute of a<label>element is accessed as "htmlFor".)
/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;
/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
The float property is specified as a single keyword, chosen from the list of values below.
left
right
none
inline-start
ltr scripts, and the right side with rtl scripts.inline-end
ltr scripts, and the left side with rtl scripts.{{cssinfo}}
{{csssyntax}}
As mentioned above, when an element is floated, it is taken out of the normal flow of the document (though still remaining part of it). It is shifted to the left, or right, until it touches the edge of its containing box, or another floated element.
In this example, there are three colored squares. Two are floated left, and one is floated right. Note that the second "left" square is placed to the right of the first. Additional squares would continue to stack to the right, until they filled the containing box, after which they would wrap to the next line.
A floated element is at least as tall as its tallest nested floated children. We gave the parent width: 100% and floated it to ensure it is tall enough to encompass its floated children, and to make sure it takes up the width of the parent so we don't have to clear its adjacent sibling.
<section>
<div class="left">1</div>
<div class="left">2</div>
<div class="right">3</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi tristique
sapien ac erat tincidunt, sit amet dignissim lectus vulputate. Donec id
iaculis velit. Aliquam vel malesuada erat. Praesent non magna ac massa
aliquet tincidunt vel in massa. Phasellus feugiat est vel leo finibus
congue.
</p>
</section>
section {
box-sizing: border-box;
border: 1px solid blue;
width: 100%;
float: left;
}
div {
margin: 5px;
width: 50px;
height: 150px;
}
.left {
float: left;
background: pink;
}
.right {
float: right;
background: cyan;
}
{{EmbedLiveSample('How_floated_elements_are_positioned','400','190')}}
Sometimes you may want to force an item to move below any floated elements. For instance, you may want paragraphs to remain adjacent to floats, but force headings to be on their own line. See {{cssxref("clear")}} for examples.
{{Specifications}}
{{Compat}}