Skip to main content

Command Palette

Search for a command to run...

Learn CSS Flexbox in 10 Minutes

Updated
2 min read
Learn CSS Flexbox in 10 Minutes

Flexbox is a powerful module in CSS that provides an efficient way to arrange elements in a container — even when their size is unknown or dynamic. Unlike traditional layouts that rely heavily on floats and positioning, Flexbox focuses on distributing space along a single axis (either horizontal or vertical) while allowing flexible alignment and spacing.

1. The Magical Property Flex

With Flexbox, you define a flex container and then control how its flex items behave inside it — including their size, order, and alignment.

With a parent container with class ‘container‘ and child items inside it. The magic starts with just setting the display to flex:

.container {
  display: flex;
}

2. Flexbox Properties

Flex container properties:

PropertyDescriptionExample
displayDefines a flex container (flexorinline-flex)display: flex;
flex-directionDefines the direction of the main axisflex-direction: row-reverse;
flex-wrapAllows items to wrap onto multiple linesflex-wrap: wrap;
flex-flowShorthand for flex-direction and flex-wrapflex-flow: column wrap;
justify-contentAligns items along the main axisjustify-content: space-between;
align-itemsAligns items along the cross-axisalign-items: center;
align-contentAligns rows within the container when wrappingalign-content: space-around;

Flex item properties:

PropertyDescriptionExample
orderControls the order of the itemsorder: 2;
flex-growDefines how much a flex item can grow relative to othersflex-grow: 1;
flex-shrinkDefines how much a flex item can shrink relative to othersflex-shrink: 0;
flex-basisDefines the initial size of a flex item before distributing spaceflex-basis: 200px;
flexShorthand for flex-grow, flex-shrink, and flex-basisflex: 1 0 100px;
align-selfOverrides align-items for an individual itemalign-self: flex-end;

3. Example of a Responsive Two-Column Layout

.container {
  display: flex;
  gap: 1rem;
}
.sidebar {
  flex: 1;
}
.main {
  flex: 3;
}

More from this blog

UI Dev

18 posts