CSS LAYOUTS /// FLOAT /// CLEARFIX /// FLEXBOX /// JUSTIFY-CONTENT /// ALIGN-ITEMS /// CSS LAYOUTS /// FLOAT /// CLEARFIX /// FLEXBOX /// JUSTIFY-CONTENT ///

CSS Layouts: Float & Flexbox

Master CSS Layouts: from the legacy Float property and clearfix hacks to the modern, powerful 1-dimensional Flexbox engine.

layout.css
1 / 15
12345
🏗️

Tutor:Layouts on the web have evolved drastically. We'll start with the legacy 'float' property, and move to the modern powerhouse: Flexbox.


Layout Matrix

UNLOCK NODES BY MASTERING BOX ALIGNMENT.

Concept: Float & Clear

Floats remove elements from the normal document flow. To prevent parent containers from collapsing, we apply clear fixes.

System Check

Why do parent containers sometimes collapse to 0 height when using floats?


Community Holo-Net

Showcase Your Layouts

ACTIVE

Built an insane responsive grid using purely Flexbox? Share your codepens and get feedback!

CSS Layouts: Overcoming Hacks with Flexbox

Frontend Instructor in Code Syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

The history of CSS Layouts is a history of hacks. From HTML Tables to Floats, developers forced the browser to do things it wasn't meant to do. Flexbox changed everything by giving us a native, 1-dimensional layout engine.

The Dark Ages: Floats

Originally, the float property was created simply so text could wrap around an image. But developers realized that if they floated boxes left and right, they could build grid systems.

This introduced the "Collapsed Parent" bug: parents don't recognize the height of floated children. The solution was the notorious clearfix hack, appending an invisible element using `::after` with `clear: both` to force the parent to stretch.

The Revolution: Flexbox Basics

By applying display: flex; to a parent container, a Flex Formatting Context is established. All direct children become flex items.

Flexbox operates on two axes. By default, the Main Axis is horizontal (row), and the Cross Axis is vertical (column).

Alignment & Distribution

  • Justify Content: Controls alignment on the Main Axis. Values like space-between, center, and flex-end distribute remaining space.
  • Align Items: Controls alignment on the Cross Axis. align-items: center; is the secret to perfect vertical centering.
  • Flex Wrap: By default, flex items try to cram into one line. flex-wrap: wrap; allows them to drop to a new line, creating responsive multi-row layouts.
View Parent vs Child Properties+

Properties for the Parent (Container):

  • display: flex;
  • flex-direction (row, column)
  • justify-content
  • align-items
  • flex-wrap

Properties for the Children (Items):

  • flex-grow (how much it can stretch)
  • flex-shrink (how much it can shrink)
  • flex-basis (initial size)
  • align-self (override parent's align-items)

Frequently Asked Questions

Should I still use Float to create layouts?

**No.** Today, Float should only be used for its original purpose: making text flow around an image. For page structures, alignments, and components, Flexbox or CSS Grid are the correct industry-standard tools.

What is the difference between justify-content and align-items?

It depends on the flex-direction.

If it is **row** (default): justify-content aligns horizontally, and align-items aligns vertically.

If it is **column**: the axes are inverted. justify-content aligns vertically, and align-items aligns horizontally.

How do I center a div perfectly with Flexbox?

Known as the "Holy Grail" of centering, you only need 3 lines of code in the parent container:

.parent {display: flex; justify-content: center; align-items: center;}

Layout Glossary

float
Removes an element from the normal document flow, pushing it to the left or right, allowing text to wrap around it.
snippet.css
clear
Specifies whether an element must be moved below (cleared) preceding floated elements.
snippet.css
display: flex
Initiates a flex formatting context for its direct children, enabling the Flexbox model.
snippet.css
flex-direction
Establishes the main-axis, defining the direction flex items are placed in the flex container (row or column).
snippet.css
justify-content
Defines the alignment along the main axis. Helps distribute extra free space left over.
snippet.css
align-items
Defines the default behavior for how flex items are laid out along the cross axis.
snippet.css