Flexbox Layout
Create flexible, responsive layouts with Flexbox. Container properties, item properties, and real-world patterns.
Flexbox is a one-dimensional layout method for arranging items in rows or columns. Items flex to fill available space and shrink to fit smaller spaces.
.flex-container {
display: flex; /* Enables flexbox */
flex-direction: row; /* Main axis: horizontal */
flex-wrap: wrap; /* Wrap on overflow */
justify-content: center; /* Main axis alignment */
align-items: stretch; /* Cross axis alignment */
gap: 16px; /* Gap between items */
}These properties apply to the parent element (flex container):
displayflex | inline-flexEnables flex context for direct children.flex-directionrow | row-reverse | column | column-reverseSets main axis direction.flex-wrapnowrap | wrap | wrap-reverseControls wrapping behavior.justify-contentflex-start | center | space-between | space-around | space-evenlyMain axis alignment.align-itemsstretch | center | flex-start | flex-end | baselineCross axis alignment (single line).align-contentstretch | center | space-between | space-around | flex-start | flex-endCross axis alignment (multiple lines with wrap).gap<length>Shorthand for row-gap and column-gap./* Flex direction — changes main axis */
.row { display: flex; flex-direction: row; } /* Default — horizontal */
.column { display: flex; flex-direction: column; } /* Vertical stacking */
/* Alignment combos */
.center-both { display: flex; justify-content: center; align-items: center; }
.navbar { display: flex; justify-content: space-between; align-items: center; }
.wrap-grid { display: flex; flex-wrap: wrap; gap: 1rem; align-content: flex-start; }These properties apply to individual children of a flex container:
order<integer> (default 0)Controls visual order. Negative moves earlier.flex-grow<number> (default 0)Ability to grow. 1 = can grow, 0 = no grow.flex-shrink<number> (default 1)Ability to shrink. 0 = no shrink.flex-basis<length> | auto (default auto)Initial size before grow/shrink.flexgrow shrink basisShorthand. flex: 1 = 1 1 0%.align-selfauto | stretch | center | flex-start | flex-end | baselineOverrides align-items for this item./* Flex grow — items expand to fill space */
.item { flex: 1; } /* All items grow equally */
.item:first-child { flex: 2; } /* This item grows twice as much */
/* Flex basis — starting size */
.item { flex: 0 0 200px; } /* Fixed 200px — no grow, no shrink */
.item { flex: 1 1 150px; } /* Start at 150px, grow/shrink as needed */
/* Order — rearrange visually */
.item:last-child { order: -1; } /* Moves to first position */
/* Align-self — individual alignment */
.item:first-child { align-self: flex-start; }
.item:last-child { align-self: flex-end; }Practical layout patterns you'll use every day:
display: flex; justify-content: center; align-items: center; height: 100vh;
Modals, cards, login screens
display: flex; justify-content: space-between; align-items: center;
Navbars, toolbars
display: flex; flex-wrap: wrap; gap: 1.5rem; > * { flex: 1 1 250px; }
Card grids, galleries
body { display: flex; flex-direction: column; min-height: 100vh; } .content { flex: 1; }
Page layouts
body { display: flex; flex-direction: column; min-height: 100vh; } .main { display: flex; flex: 1; }
Header + sidebar + content + footer
Practice Flexbox by creating different layouts below.
Create flexbox-layout.html.
Use the starter code and experiment:
<div class="container">
<div class="item" style="flex:2">Flex: 2</div>
<div class="item">Flex: 1</div>
<div class="item">Flex: 1</div>
</div>
Try these experiments:
Done — Key Points:
- ✅
flex-directionsets main axis (row/column).flex-wrapcontrols multi-line. - ✅
justify-content= main axis.align-items= cross axis (1 line).align-content= cross axis (wrap). - ✅
flex: 1= grow+shrink+basis.flex: 0 0 200px= fixed.flex: 1 1 150px= responsive. - ✅
orderchanges visual order.align-selfoverrides per item. - ✅
gapadds spacing between items — works in both row and column directions. - ✅ Patterns: centered layout, navbar, responsive grid, sticky footer, holy grail.
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login