liveBook · Manning

Float property

As explained in Web Design Playground

A CSS property that takes an element out of normal document flow and places it to the left or right so surrounding content can wrap around it.

The CSS float property nudges an element out of the usual top-to-bottom page flow and sends it as far left or right as it can go inside its parent. The fun part: nearby content flows around it, rather like text wrapping around a photo in a magazine. section 8.2 section 8.23

Start with the three values: `float: left` places the element on the left, `float: right` places it on the right, and `float: none` leaves it un-floated. Add a margin so the wrapping content has breathing room. section 8.2

Float a table of contents

A table of contents can sit at the right edge while quotations and other content wrap beside it: `.toc {` ` float: right;` ` margin-left: 2em;` ` margin-bottom: 2em;` `}` A float can be applied to an inline element such as `span`, too. The browser turns that inline element into a block-level element before floating it; give it a width so the browser knows how much room to reserve. section 8.3 section 8.4

Floats are handy for small layout and typographic effects:

  • Create a drop cap by selecting a paragraph’s first letter with `::first-letter`, floating it left, then adjusting its size, line height, and padding. section 8.9
  • Set off a pull quote by placing the excerpt in an element, usually floating it right, and styling its margins, type, and borders. section 8.12 section 8.13
  • Place items side by side for navigation, headers, or columns by floating multiple elements, usually left, and assigning widths where needed. section 11.12 section 11.18

A float keeps affecting later content until something clears it. Use `clear: left`, `clear: right`, or `clear: both` on the element that should begin after the float. For example, a heading with `clear: right` starts below a right-floated table of contents instead of wrapping alongside it. section 8.5

Watch for container collapse: when a parent contains only floated children, those children are out of normal flow, so the parent can appear to have no height. A common repair is a self-clearing `::after` pseudo-element that inserts an empty block and clears both sides. section 8.6

.first-paragraph::first-letter {    #1  
    float: left;    #2  
    padding-top: .1em;    #3  
    padding-right: .1em;    #3  
    color: darkred;    #3  
    font-size: 5em;    #3  
    line-height: .6em;    #3  
}

Try this compact drop-cap rule: it floats the opening letter left, then uses padding, font size, and line height to let paragraph text nestle around it. section 8.10

For a narrow screen, remove floated columns with `float: none` and give them full width so they stack again. Float-based layouts remain useful to understand, but Flexbox and CSS Grid are presented as the more straightforward direction for layout. section 13.13 section 11.12

How Web Design Playground covers it

How deep it goes
Explains values, wrapping, margins, clearing, container collapse, and responsive removal, while deferring modern layout methods to Flexbox and CSS Grid.
Examples
Uses a floated table of contents, drop cap, pull quote, navigation items, article-and-aside columns, and responsive stacked columns.
What it assumes
Teaches floating from default HTML page flow and assumes only elements, CSS rules, and basic page content.
What you can do after
Apply it via CSS to create wrapping effects and side-by-side layouts, then clear floats and prevent collapsing parents.
How it connects
Connects floats with normal flow, margins, clear, pseudo-elements, media queries, Flexbox, and CSS Grid.

Also explained in

Compare how 5 books explain float property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground. The section links point to the parts of the book it draws on.