Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

3. Flexbox: Navbars, Cards & Real Layouts

Lesson 3 of 10 in the free Tailwind CSS: Zero to Production notes on Siksha Sarovar, written by Rohit Jangra.

Flexbox in one sentence

Add flex to a parent and its children stop stacking and start sitting in a row, and you get two superpowers: control over spacing along the row (justify-) and alignment across it (items-).

90% of everyday layout — navbars, toolbars, list rows, button groups, centering — is flexbox. Learn these utilities cold.

The core utilities

UtilityCSSControls
flexdisplay: flexturns on flexbox (row by default)
flex-colflex-direction: columnstack children vertically
justify-start/center/endjustify-contentposition along the main axis
justify-betweenspace pushed between itemsnavbars: logo left, menu right
justify-around / justify-evenlydistributed spacingequal gaps around items
items-start/center/endalign-itemsalignment on the cross axis
items-baselinealign text baselinesprice + "/month" pairs
gap-4gap: 1remspace between children (both axes)
flex-wrapflex-wrap: wrapallow items to wrap to next line
The most important class combo in Tailwind: flex items-center justify-between. Say it like a mantra. It builds navbars, card headers, list rows, footers — anything with "stuff on the left, stuff on the right, all vertically centered".

Perfect centering (finally easy)

Centering a child both ways used to be a running joke in CSS. Now:

<div class="flex items-center justify-center min-h-screen">
  <div>I am perfectly centered</div>
</div>

justify-center centers horizontally (main axis), items-center centers vertically (cross axis). Note: in flex-col, the axes swap — justify- becomes vertical and items- horizontal. If alignment ever "does the wrong thing", check the direction first.

How children grow and shrink

UtilityMeaning
flex-1grow to fill all leftover space (equal share)
flex-nonenever grow, never shrink — stay natural size
growallow growing only
shrink-0never shrink (critical for avatars & icons!)
basis-1/3starting size before grow/shrink

The classic media object — avatar left, text right, text takes the rest:

<div class="flex items-center gap-4">
  <img class="size-12 rounded-full shrink-0" src="avatar.jpg" alt="">
  <div class="flex-1 min-w-0">
    <p class="font-semibold text-slate-900">Priya Sharma</p>
    <p class="text-sm text-slate-500 truncate">Completed the Flexbox lesson and started…</p>
  </div>
  <span class="text-xs text-slate-400 shrink-0">2m ago</span>
</div>

Two subtle pro details in there:

  • shrink-0 on the avatar — without it, long text squishes the image into an oval.
  • min-w-0 on the text block — flex children refuse to shrink below their content's width by default, which breaks truncate. min-w-0 fixes the single most confusing flexbox bug you will ever meet.

gap vs space-x

Both create spacing between children. gap-4 is the flexbox-native way and handles wrapping correctly (space-x puts margin on wrapped lines' first items where you don't want it). Rule: inside flex or grid, always prefer gap-*.

Pattern 1 — the navbar

<nav class="flex items-center justify-between px-6 py-4 bg-white shadow-sm">
  <span class="font-extrabold text-slate-900">Brand</span>
  <div class="flex items-center gap-6 text-sm text-slate-600">
    <a href="#" class="hover:text-teal-600">Courses</a>
    <a href="#" class="hover:text-teal-600">Pricing</a>
    <a href="#" class="bg-teal-600 text-white px-4 py-2 rounded-lg hover:bg-teal-500">Sign up</a>
  </div>
</nav>

One outer justify-between splits brand from menu; one inner flex gap-6 spaces the links. Nested flex containers — outer for macro layout, inner for micro — is how every real interface is built.

Pattern 2 — sticky footer

Page shorter than the screen, but the footer must sit at the bottom:

<body class="flex flex-col min-h-screen">
  <nav>…</nav>
  <main class="flex-1">…</main>   <!-- flex-1 absorbs the leftover height -->
  <footer>…</footer>
</body>

Pattern 3 — equal-height cards

Cards in a flex row are equal height automatically (items-stretch is the default). To pin the button of each card to its bottom regardless of text length: make each card flex flex-col and give the description flex-1, which pushes the button down.

Utilities for individual children

  • self-end, self-center — override the parent's items-* for one child
  • order-first, order-last — reorder visually without moving the HTML
  • ml-auto — the flexbox "push everything after me to the right" trick (e.g. one button pushed to the row's end)

Common beginner mistakes

  • Forgetting the axes swap in flex-col — vertical centering suddenly needs justify-center, not items-center.
  • Squished avatars — add shrink-0 to any fixed-size child sitting next to long text.
  • truncate not truncating — the flex child needs min-w-0.
  • Margins for spacing between flex children — use gap-*; margins fight with wrapping and justify-between.

Practice task

In the editor: (1) make the chat rows reverse for the second message (sender on the right) using flex-row-reverse, (2) pin the "Send" button to the right edge of the composer with ml-auto instead of justify-between, (3) add a third chat row and watch the equal spacing come from space-y-4, not margins.