CHAPTER 09 · LAYOUT

CSS Flexbox

Flexbox ek one-dimensional layout system hai jo row ya column me items ko arrange, align, space aur resize karna easy banata hai.

English + Hinglish25 min readPractice included

What is Flexbox?

Flexbox works on one primary axis at a time. Parent par display: flex lagate hi direct children flex items ban jaate hain.

flex-start.cssCSS
.container {
  display: flex;
}

Main axis and cross axis

Main axis flex-direction follow karta hai; cross axis uske perpendicular hota hai. justify-content main axis aur align-items cross axis par kaam karta hai.

flex-direction

direction.cssCSS
.row {
  display: flex;
  flex-direction: row;
}

.column {
  display: flex;
  flex-direction: column;
}

justify-content

justify.cssCSS
.nav {
  display: flex;
  justify-content: space-between;
}

Common values: flex-start, center, flex-end, space-between, space-around and space-evenly.

align-items

align.cssCSS
.toolbar {
  display: flex;
  align-items: center;
}

Perfect centering

center.cssCSS
.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

gap

gap repeated flex items ke beech clean spacing deta hai.

gap.cssCSS
.button-group {
  display: flex;
  gap: 0.75rem;
}

flex-wrap

wrap.cssCSS
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

flex-basis, flex-grow and flex-shrink

flex-sizing.cssCSS
.card {
  flex-basis: 240px;
  flex-grow: 1;
  flex-shrink: 1;
}

flex-basis starting size, flex-grow extra space growth aur flex-shrink tight space me shrinking control karta hai.

flex shorthand

flex-shorthand.cssCSS
.card {
  flex: 1 1 240px;
}
Read it like this

1 1 240px = grow allowed, shrink allowed, preferred basis 240px.

Responsive card row

cards.cssCSS
.course-list {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.course-card {
  flex: 1 1 260px;
}

Auto margins

auto-margin.cssCSS
.nav {
  display: flex;
  align-items: center;
}

.sign-in {
  margin-left: auto;
}

order and source order

order visual order change karta hai, DOM reading order nahi. Accessibility ke liye source order ko logical rakho.

order.cssCSS
.featured {
  order: -1;
}

align-content

Multiple wrapped lines ke beech cross-axis spacing ko control karta hai when extra space exists.

min-width: 0 gotcha

min-width.cssCSS
.row {
  display: flex;
}

.content {
  flex: 1;
  min-width: 0;
}

Long content overflow me flexible child ko properly shrink karane ke liye min-width: 0 useful ho sakta hai.

Flexbox and accessibility

  • Source order logical rakho.
  • row-reverse, column-reverse aur order ka overuse avoid karo.
  • Narrow viewport aur high zoom par layout test karo.
  • Focus states visible rakho.

Best practices

  • Alignment se pehle main/cross axis identify karo.
  • Repeated spacing ke liye gap prefer karo.
  • Wrapping cards ke liye flexible basis use karo.
  • Two-dimensional layout problems me CSS Grid consider karo.

Common beginner mistakes

  • justify-content ko always horizontal samajhna.
  • align-items ko always vertical samajhna.
  • flex-wrap bhoolna.
  • order se semantic order replace karna.
  • Long content overflow me min-width: 0 issue miss karna.

Practice Task

Responsive “Course Cards Row” banao.

  1. Parent par display: flex lagao.
  2. gap: 1rem add karo.
  3. flex-wrap: wrap enable karo.
  4. Cards ko flex: 1 1 260px do.
  5. Toolbar me align-items: center test karo.
  6. Button ko margin-left: auto se push karo.
  7. DevTools Flexbox overlay inspect karo.