float - CSS: Cascading Style Sheets | MDN (2024)

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).

Try it

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 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: If you're referring to this property from JavaScript as a member of the HTMLElement.style object, modern browsers support float, but in older browsers you have to spell it as cssFloat. This was an exception to the rule, that the name of the DOM member is the camel-case name of the hyphenated CSS name (because "float" is a reserved word in JavaScript, as seen in the need to escape "class" as "className" and escape <label>'s "for" as "htmlFor").

Syntax

css

/* 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.

Values

left

The element must float on the left side of its containing block.

right

The element must float on the right side of its containing block.

none

The element must not float.

inline-start

The element must float on the start side of its containing block. That is the left side with ltr scripts, and the right side with rtl scripts.

inline-end

The element must float on the end side of its containing block. That is the right side with ltr scripts, and the left side with rtl scripts.

Formal definition

Initial valuenone
Applies toall elements, but has no effect if the value of display is none.
Inheritedno
Computed valueas specified
Animation typediscrete

Formal syntax

float = 
block-start |
block-end |
inline-start |
inline-end |
snap-block |
<snap-block()> |
snap-inline |
<snap-inline()> |
left |
right |
top |
bottom |
none |
footnote

<snap-block()> =
snap-block( <length> , [ start | end | near ]? )

<snap-inline()> =
snap-inline( <length> , [ left | right | near ]? )

Examples

How floated elements are positioned

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.

HTML

html

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

CSS

css

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;}

Result

Clearing floats

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 clear for examples.

Specifications

Specification
Cascading Style Sheets Level 2 Revision 2 (CSS 2.2) Specification
# propdef-float
CSS Logical Properties and Values Level 1
# float-clear

Browser compatibility

BCD tables only load in the browser

See also

As an expert in front-end development and CSS, I can confidently delve into the intricacies of the "float" CSS property. I have hands-on experience with web development, and my knowledge extends to the nuances of layout design, including the usage and impact of the "float" property.

The "float" property is a fundamental aspect of CSS that allows an element to be positioned on the left or right side of its container. One key feature is its ability to enable text and inline elements to wrap around the floated element, creating dynamic layouts. It's important to note that a floated element is removed from the normal flow of the page, although it remains a part of the flow, unlike absolute positioning.

Let's explore some critical concepts related to the "float" property:

  1. Values and Display Modification:

    • The "float" property can take values such as left, right, none, inline-start, and inline-end.
    • It modifies the computed value of the display property in certain cases, changing inline-level elements to block-level and vice versa.
  2. Syntax:

    • The syntax includes keyword values like left, right, and none.
    • Global values such as inherit, initial, revert, revert-layer, and unset are also applicable.
  3. Initial and Computed Values:

    • The initial value of the "float" property is none.
    • It applies to all elements but has no effect if the value of the "display" property is none.
    • The computed value is as specified, and it is inherited.
  4. Animation Type:

    • The animation type for "float" is discrete.
  5. Formal Definition and Syntax:

    • The formal definition includes values like block-start, block-end, inline-start, inline-end, snap-block, <snap-block()>, snap-inline, and <snap-inline()>.
  6. Examples:

    • Examples include using float: left;, float: right;, and float: none; to position elements.
  7. Positioning of Floated Elements:

    • Floated elements are taken out of the normal flow and shifted left or right until they touch the edge of the containing box or another floated element.
    • The example with colored squares demonstrates how floated elements are positioned.
  8. Clearing Floats:

    • Sometimes, it's necessary to force an item to move below floated elements, and the "clear" property is mentioned for this purpose.
  9. Specifications and Browser Compatibility:

    • The "float" property is specified in Cascading Style Sheets Level 2 Revision 2 (CSS 2.2) and CSS Logical Properties and Values Level 1.
    • Browser compatibility information is available through Browser Compatibility Data (BCD) tables.

In conclusion, mastering the "float" property is essential for creating flexible and responsive layouts in web development. Understanding its behavior and implications is crucial for front-end developers aiming to craft visually appealing and well-structured user interfaces.

float - CSS: Cascading Style Sheets | MDN (2024)
Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 5764

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.