🧭 CSS Flexbox (Full Guide)

Last Updated: November 2025


The Flexbox (Flexible Box Layout) is a modern CSS layout system that helps you align and distribute space among elements efficiently, even when their size is unknown or dynamic.

🗣 Hinglish Tip: Flexbox se layout banana easy hota hai — bina float ya position ke, responsive design simple ho jaata hai.

To use Flexbox, set the parent container’s display to flex.

.container {
  display: flex;
}

All direct child elements become flex items.

<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

🧩 Flex Container Properties

PropertyDescriptionExample
displayActivates flex layoutdisplay: flex;
flex-directionDefines main axis direction (row or column)flex-direction: row | column | row-reverse | column-reverse;
flex-wrapControls wrapping of itemsflex-wrap: nowrap | wrap | wrap-reverse;
flex-flowShorthand for flex-direction + flex-wrapflex-flow: row wrap;
justify-contentAligns items along main axisjustify-content: center | space-between | flex-end;
align-itemsAligns items along cross axisalign-items: center | flex-start | stretch;
align-contentAligns rows when wrappingalign-content: space-around | center | stretch;

Example: Container Control

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  height: 200px;
  background-color: #f0f0f0;
}
.item {
  background-color: lightcoral;
  padding: 20px;
  font-size: 20px;
}

🎯 Flex Item Properties

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

Example: Item Control

.container {
  display: flex;
  height: 200px;
  background-color: #eee;
}
.item {
  flex: 1;
  padding: 20px;
  background-color: lightblue;
  margin: 5px;
}
.item:nth-child(2) {
  flex-grow: 2;
  background-color: lightgreen;
  align-self: flex-end;
}
.item:nth-child(3) {
  order: -1;
  background-color: lightcoral;
}

💡 Flexbox Shorthand Summary

ShorthandExpands ToExample
flex-flowflex-direction + flex-wrapflex-flow: row wrap;
flexflex-grow + flex-shrink + flex-basisflex: 1 1 auto;