Flexbox: One-Dimensional Layout

Flexbox — একমাত্রিক লেআউট

Read: ~32 min Advanced 5 practice problems Live preview runner

1. Thinking in One Dimension

Flexbox is a layout mode built for arranging items along a single line — either a row or a column — and controlling how extra space (or missing space) is shared between them. It replaced decades of hacky float- and table-based layout tricks with a small, predictable set of properties. The single most important habit to build: before writing any Flexbox code, decide which direction is your main axis. Every other property's meaning depends on that one decision.

Flexbox হলো এমন একটি লেআউট মোড, যা এলিমেন্টগুলোকে একটি একক লাইন বরাবর সাজায় — হয় row নয়তো column — এবং অতিরিক্ত জায়গা (বা কম জায়গা) কীভাবে ভাগ হবে তা নিয়ন্ত্রণ করে। এটি দশকের পুরনো float ও table-ভিত্তিক লেআউট কৌশলগুলোর বদলে একটি ছোট, পূর্বানুমানযোগ্য প্রপার্টি সেট নিয়ে এসেছে। সবচেয়ে গুরুত্বপূর্ণ অভ্যাস: যেকোনো Flexbox কোড লেখার আগে ঠিক করুন আপনার main axis কোন দিকে। বাকি সব প্রপার্টির অর্থ এই একটি সিদ্ধান্তের ওপর নির্ভর করে।

2. display: flex & flex-direction

Adding display: flex to a container turns its direct children into "flex items" laid out along a main axis. flex-direction chooses that axis: row (default, left to right), row-reverse, column (top to bottom), or column-reverse. Whichever direction you pick, the cross axis is always perpendicular to it — this distinction drives everything that follows.

একটি container-এ display: flex যোগ করলে তার সরাসরি child-গুলো "flex item"-এ পরিণত হয় এবং main axis বরাবর সাজানো হয়। flex-direction সেই axis ঠিক করে: row (ডিফল্ট, বাম থেকে ডানে), row-reverse, column (ওপর থেকে নিচে), অথবা column-reverse। যেদিকেই বেছে নিন, cross axis সবসময় তার সাথে লম্ব হবে — এই পার্থক্যটি বাকি সবকিছুকে নিয়ন্ত্রণ করে।
flex-direction: row main axis (row) → cross flex-direction: column main axis (column) ↓ cross axis Figure 16.1 — row-এ main axis অনুভূমিক, column-এ main axis উল্লম্ব; cross axis সবসময় তার লম্ব।
flex-direction.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>flex-direction</title>
</head>
<body>
  <div class="row-box">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>
style.css
.row-box {
  display: flex;
  flex-direction: row; /* try changing to column */
  gap: 10px;
  padding: 10px;
  background: #f8f9fa;
}

.item {
  width: 70px;
  height: 70px;
  background: #39b549;
  color: white;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

3. justify-content — Distributing Space on the Main Axis

justify-content decides how leftover space along the main axis is distributed between and around flex items. It has no effect if the items already fill the container completely. Change only the value below and press Run again to feel the difference between all six common values.

justify-content ঠিক করে main axis বরাবর বাকি থাকা জায়গা flex item-গুলোর মধ্যে ও চারপাশে কীভাবে ভাগ হবে। item-গুলো যদি container সম্পূর্ণ ভরে ফেলে, তাহলে এর কোনো প্রভাব নেই। নিচে শুধু value বদলে আবার Run চাপুন — ছয়টি সাধারণ মানের পার্থক্য নিজে অনুভব করুন।
justify-content.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>justify-content</title>
</head>
<body>
  <div class="row">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
  </div>
</body>
</html>
style.css
.row {
  display: flex;
  justify-content: space-between; /* try: flex-start, center, flex-end, space-around, space-evenly */
  background: #f8f9fa;
  border: 1px dashed #d1d5db;
  padding: 10px;
}

.item {
  width: 70px;
  height: 60px;
  background: #2563eb;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}
justify-content — Full Reference
ValueEffectবাংলায়
flex-start (default)Items packed at the start of the main axis.Item-গুলো main axis-এর শুরুতে জমা হয়।
centerItems packed in the center.Item-গুলো মাঝখানে জমা হয়।
flex-endItems packed at the end.Item-গুলো শেষে জমা হয়।
space-betweenEqual gaps between items; no gap at the outer edges.item-গুলোর মধ্যে সমান ফাঁক; শুরু ও শেষে ফাঁক নেই।
space-aroundEqual space around each item (edges get half a gap).প্রতিটি item-এর চারপাশে সমান জায়গা।
space-evenlyPerfectly equal space between items and at both edges.item ও দুই প্রান্তে সম্পূর্ণ সমান জায়গা।

4. align-items — Aligning on the Cross Axis

While justify-content controls the main axis, align-items controls how items line up along the cross axis — for the default row direction, that means vertical alignment. This is the property most beginners reach for to vertically center something, and for good reason: it solves in one line what used to take a hacky trick.

justify-content main axis নিয়ন্ত্রণ করে, আর align-items নিয়ন্ত্রণ করে item-গুলো cross axis বরাবর কীভাবে সাজানো হবে — ডিফল্ট row দিকের ক্ষেত্রে এর মানে উল্লম্ব alignment। কোনো কিছু vertically center করার জন্য বেশিরভাগ নতুনরা প্রথমে এই প্রপার্টিই খোঁজেন — কারণ এটি এক লাইনেই সমাধান করে যা আগে একটি জটিল কৌশল লাগত।
align-items.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>align-items</title>
</head>
<body>
  <div class="row">
    <div class="item short">short</div>
    <div class="item tall">tall item</div>
    <div class="item short">short</div>
  </div>
</body>
</html>
style.css
.row {
  display: flex;
  align-items: center; /* try: flex-start, flex-end, stretch, baseline */
  gap: 10px;
  height: 140px;
  background: #f8f9fa;
  border: 1px dashed #d1d5db;
  padding: 10px;
}

.item {
  width: 90px;
  background: #7c3aed;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.short { height: 50px; }
.tall  { height: 100px; }
align-items — Full Reference
ValueEffectবাংলায়
stretch (default)Items stretch to fill the container's cross-axis size (if no height is set).height সেট না থাকলে item-গুলো cross axis পুরো ভরে দেয়।
flex-startAligned to the start of the cross axis (top, for row).cross axis-এর শুরুতে (row-এর জন্য উপরে)।
centerCentered on the cross axis.cross axis বরাবর মাঝখানে।
flex-endAligned to the end of the cross axis (bottom, for row).cross axis-এর শেষে (row-এর জন্য নিচে)।
baselineAligned so each item's text baseline lines up.প্রতিটি item-এর টেক্সট baseline মিলিয়ে সাজায়।
Key insight To perfectly center anything both ways, combine justify-content: center with align-items: center on the same flex container.

যেকোনো কিছু দুই দিকেই perfectly center করতে একই flex container-এ justify-content: center এবং align-items: center একসাথে ব্যবহার করুন।

5. flex-wrap & gap

By default, flex items try to squeeze onto a single line, shrinking as needed. flex-wrap: wrap lets items overflow onto additional lines instead of shrinking indefinitely — essential for card grids and tag lists on narrow screens. gap adds consistent spacing between items (and between wrapped rows) without needing margin tricks that get messy at the edges.

ডিফল্টভাবে flex item-গুলো একটি লাইনেই ধরার চেষ্টা করে, প্রয়োজনে ছোট হয়ে যায়। flex-wrap: wrap item-গুলোকে অসীমভাবে ছোট না হয়ে বরং পরের লাইনে চলে যাওয়ার সুযোগ দেয় — ছোট স্ক্রিনে card grid বা ট্যাগ লিস্টের জন্য এটি অত্যন্ত জরুরি। gap item-গুলোর মধ্যে (এবং wrap হওয়া সারিগুলোর মধ্যেও) একইরকম স্পেসিং যোগ করে, margin দিয়ে করলে যেমন কিনারায় গোলমাল হয় তেমনটা এড়িয়ে।
flex-wrap.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>flex-wrap + gap</title>
</head>
<body>
  <div class="tags">
    <span class="tag">HTML</span>
    <span class="tag">CSS</span>
    <span class="tag">Flexbox</span>
    <span class="tag">Grid</span>
    <span class="tag">Responsive</span>
    <span class="tag">Bangla</span>
    <span class="tag">ABCL TECH</span>
  </div>
</body>
</html>
style.css
.tags {
  display: flex;
  flex-wrap: wrap;   /* try removing this — it squeezes into one line */
  gap: 10px;
  max-width: 320px;
  padding: 12px;
  background: #f8f9fa;
}

.tag {
  padding: 6px 14px;
  background: #39b549;
  color: white;
  border-radius: 999px;
  font-size: 14px;
}

6. flex-grow, flex-shrink & flex-basis — Individual Item Sizing

These three control how a specific item behaves when there's extra or missing space, and are usually written together as the flex shorthand: flex: grow shrink basis;. flex-basis is the item's starting size before space is distributed. flex-grow is a ratio of how much extra space this item should claim relative to its siblings (0 means don't grow). flex-shrink is the same idea but for when there isn't enough space. flex: 1 is an extremely common shorthand meaning "grow and shrink freely, ignore your natural size."

এই তিনটি প্রপার্টি ঠিক করে একটি নির্দিষ্ট item-এর জন্য অতিরিক্ত বা কম জায়গা থাকলে কী হবে, এবং সাধারণত একসাথে flex শর্টহ্যান্ড হিসেবে লেখা হয়: flex: grow shrink basis;। flex-basis হলো জায়গা ভাগ হওয়ার আগে item-এর শুরুর সাইজ। flex-grow হলো ভাইবোন item-গুলোর তুলনায় এই item কতটা অতিরিক্ত জায়গা নেবে তার অনুপাত (0 মানে বাড়বে না)। flex-shrink একই ধারণা কিন্তু জায়গা কম থাকলে। flex: 1 অত্যন্ত সাধারণ একটি শর্টহ্যান্ড, যার অর্থ "স্বাধীনভাবে বাড়ো ও ছোট হও, নিজের স্বাভাবিক সাইজ উপেক্ষা করো।"
flex-grow-shrink.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>flex-grow, shrink, basis</title>
</head>
<body>
  <div class="panel">
    <div class="sidebar">Sidebar (fixed 120px)</div>
    <div class="content">Content (flex: 1 — takes all remaining space)</div>
  </div>
</body>
</html>
style.css
.panel {
  display: flex;
  gap: 10px;
  height: 120px;
}

.sidebar {
  flex: 0 0 120px; /* grow:0 shrink:0 basis:120px — never resizes */
  background: #0f172a;
  color: white;
  padding: 10px;
}

.content {
  flex: 1; /* grow:1 shrink:1 basis:0 — fills whatever is left */
  background: #e6f4e8;
  padding: 10px;
}
ShorthandEquivalent toবাংলায়
flex: 1;flex-grow: 1; flex-shrink: 1; flex-basis: 0;নিজের সাইজ ভুলে বাকি সব জায়গা সমানভাবে ভাগ করে নেয়।
flex: auto;flex-grow: 1; flex-shrink: 1; flex-basis: auto;নিজের কনটেন্ট সাইজ থেকে শুরু করে তারপর বাড়ে/কমে।
flex: none;flex-grow: 0; flex-shrink: 0; flex-basis: auto;একদম স্থির — কখনো বাড়ে না, কখনো ছোট হয় না।
flex: 0 0 120px;flex-grow: 0; flex-shrink: 0; flex-basis: 120px;নির্দিষ্ট 120px থেকে কখনো নড়ে না — যেমন একটি sidebar।

7. Real-World Flexbox Patterns (বাস্তব ব্যবহার)

Navbars
লোগো বাম দিকে, লিংক ডান দিকে — justify-content: space-between।
Centering anything
justify-content ও align-items দুটোই center করলেই সম্পূর্ণ সেন্টার।
Card rows
flex-wrap দিয়ে ছোট স্ক্রিনে card-গুলো পরের সারিতে চলে যায়।
Tag / chip lists
gap দিয়ে সমান স্পেসিং সহ ট্যাগ সাজানো হয়।
Sidebar layouts
fixed-width sidebar + flex: 1 content — সবচেয়ে সাধারণ প্যাটার্ন।
Form rows
label ও input পাশাপাশি align করতে flexbox ব্যবহৃত হয়।

8. Practice Problems

Every problem has a Show Answer button with real, runnable CSS. Try it yourself first.

প্রতিটি প্রশ্নের সাথে Show Answer বাটন আছে, যেখানে চালু-করার-যোগ্য CSS দেওয়া আছে। প্রথমে নিজে চেষ্টা করুন।
  1. Turn a <div> with three child boxes into a flex row and add a 12px gap between them.
    তিনটি child বক্সসহ একটি <div>-কে flex row বানান এবং তাদের মধ্যে 12px gap দিন।
    ✨ Show Answer (উত্তর দেখুন)
    ans1.html
    <div class="row">
      <div class="box">1</div>
      <div class="box">2</div>
      <div class="box">3</div>
    </div>
    style.css
    .row { display: flex; gap: 12px; }
    .box { width: 60px; height: 60px; background: #39b549; }
  2. Perfectly center a single "Login" button both horizontally and vertically inside a full-height container.
    একটি full-height container-এর ভেতরে একটি "Login" বাটনকে অনুভূমিক ও উল্লম্ব দুই দিকেই perfectly center করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans2.html
    <div class="screen">
      <button class="login">Login</button>
    </div>
    style.css
    .screen {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 200px;
      background: #f8f9fa;
    }
    
    .login {
      padding: 12px 26px;
      border: none;
      border-radius: 8px;
      background: #39b549;
      color: white;
      font-weight: 700;
    }
  3. Build a navbar with a logo on the far left and three nav links pushed to the far right.
    একটি navbar বানান যেখানে লোগো বাম প্রান্তে এবং তিনটি nav link ডান প্রান্তে থাকবে।
    ✨ Show Answer (উত্তর দেখুন)
    ans3.html
    <nav class="navbar">
      <div class="logo">ABCL TECH</div>
      <div class="links">
        <a href="#">Home</a>
        <a href="#">Courses</a>
        <a href="#">Contact</a>
      </div>
    </nav>
    style.css
    .navbar {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 14px 20px;
      background: #0f172a;
      color: white;
    }
    
    .links { display: flex; gap: 18px; }
    .links a { color: white; text-decoration: none; }
  4. Create a two-column layout: a 200px fixed sidebar and a content area that fills all remaining space.
    দুই-কলাম লেআউট বানান: একটি 200px fixed sidebar এবং বাকি সব জায়গা নেওয়া একটি content area।
    ✨ Show Answer (উত্তর দেখুন)
    ans4.html
    <div class="layout">
      <aside class="sidebar">Sidebar</aside>
      <main class="content">Content fills the rest</main>
    </div>
    style.css
    .layout { display: flex; height: 150px; }
    
    .sidebar {
      flex: 0 0 200px;
      background: #0f172a;
      color: white;
      padding: 12px;
    }
    
    .content {
      flex: 1;
      background: #f8f9fa;
      padding: 12px;
    }
  5. In two sentences, explain why flex: 1 on multiple sibling items splits the remaining space evenly between them.
    দুই বাক্যে ব্যাখ্যা করুন — একাধিক sibling item-এ flex: 1 দিলে বাকি জায়গা কীভাবে সমানভাবে ভাগ হয়।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: flex: 1 sets flex-grow to 1 on every item, and flex-grow values act as ratios of the leftover space after basis sizes are accounted for. Since all items share the same ratio (1), the leftover space is split into equal parts.

    flex: 1 প্রতিটি item-এ flex-grow: 1 সেট করে, এবং flex-grow মানগুলো basis সাইজ বাদ দেওয়ার পর বাকি জায়গার অনুপাত হিসেবে কাজ করে। সব item-এর অনুপাত সমান (1) হওয়ায়, বাকি জায়গা সমান ভাগে ভাগ হয়ে যায়।

Summary — Module 16

display: flex plus flex-direction establishes a main axis and a perpendicular cross axis. justify-content distributes space on the main axis; align-items aligns items on the cross axis. flex-wrap lets items flow onto new lines, and gap spaces them cleanly. flex-grow, flex-shrink, and flex-basis — usually written as the flex shorthand — control how individual items resize.

display: flex ও flex-direction মিলে একটি main axis এবং তার লম্ব cross axis তৈরি করে। justify-content main axis-এ জায়গা ভাগ করে; align-items cross axis-এ item align করে। flex-wrap item-গুলোকে নতুন লাইনে যেতে দেয়, আর gap পরিষ্কারভাবে স্পেসিং দেয়। flex-grow, flex-shrink, ও flex-basis — সাধারণত flex শর্টহ্যান্ড হিসেবে লেখা — প্রতিটি item কীভাবে সাইজ বদলাবে তা নিয়ন্ত্রণ করে।

Next Module → CSS Grid: Two-Dimensional Layout — একসাথে row ও column নিয়ন্ত্রণ করে বাস্তব পেজ লেআউট তৈরি করা।