✅ CSS & Layouts Part-1: Flexbox and Grid
🚧 Problem Layouts Solve
- HTML stacks elements vertically by default
- Real websites need alignment and spacing
- Navbars break
- Cards misalign
- Pages look unstructured
Layouts help you control:
- Direction
- Alignment
- Spacing
Modern CSS gives you two tools:
➡️ Flexbox
➡️ Grid
🔹 Flexbox
Flexbox controls layout in one direction.
- Horizontal or vertical
- Best for components
- Parent controls children
🧠 Mental Model:
- One container
- Multiple items
- Items follow a single axis
🧭 Flexbox Axes
- Main axis: Direction items move
- Cross axis: Perpendicular direction
If direction is row:
- Main axis → horizontal
- Cross axis → vertical
If direction is column:
- Main axis → vertical
- Cross axis → horizontal
🎛️ Key Flexbox Properties
📦 Container controls layout:
-
display: flex: Turns on Flexbox-
flex-direction: row, column-
justify-content: Aligns items on main axis (start, center, space-between)-
align-items: Aligns items on cross axis (center, stretch)📌 Where Flexbox Works Best
- Navigation bars
- Buttons with icons
- Cards in a row
- Centering content
🎯 Classic use case:
- Vertical centering
- Horizontal centering
- Both together
🔹 Grid
Grid controls layout in two directions.
- Rows
- Columns
You design structure first.
🧠 Mental Model:
- Page divided into cells
- Items placed inside cells
- Layout stays stable
❓ Why Grid Exists
- Flexbox struggles with full page layout
- Multiple rows become messy
- Uneven spacing appears
Grid solves this cleanly.
🎛️ Key Grid Concepts
-
display: grid- Columns
- Rows
- Gap
You decide:
- Number of columns
- Column widths
- Row behavior
📌 Where Grid Works Best
- Page layouts
- Dashboards
- Galleries
- Admin panels
🧩 Example Structure:
- Header full width
- Sidebar left
- Content center
- Footer bottom
Grid handles this without hacks.
⚖️ Flexbox vs Grid. Simple Rule
Use Flexbox when:
- You align items
- You control flow
- You build components
Use Grid when:
- You design structure
- You control rows and columns
- You build page skeletons
🚫 Common Beginner Mistakes
- Using Flexbox for full page layout
- Deep nesting of Flexbox
- Ignoring Grid for dashboards
✅ Real-World Best Practice
- Grid for page layout
- Flexbox inside components
This is how production apps are built.
🧪 Mini Practice Task
- Build a navbar with Flexbox
- Build a card grid with Grid
- Resize screen and observe behavior
✅ Mini Task Solution
🧭 1. Navbar using Flexbox
HTML
<nav class="navbar">CSS
<div class="logo">MySite</div>
<ul class="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 24px;
background-color: #222;
color: #fff;
}
.menu {
display: flex;
gap: 20px;
list-style: none;
}
What happens:- Logo stays on left
- Menu stays on right
- Items align vertically
- Layout stays clean on resize
🗂️ 2. Card Grid using CSS Grid
HTML
<div class="grid">CSS
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
<div class="card">Card 4</div>
</div>
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
padding: 20px;
}
.card {
padding: 40px;
background-color: #f2f2f2;
text-align: center;
border-radius: 8px;
}
What happens:- Cards align in rows and columns
- Equal width columns
- Clean spacing using gap
📱 3. Responsive Behavior on Resize
Add this media query:
@media (max-width: 768px) {
.grid {
grid-template-columns: repeat(1, 1fr);
}
.menu {
gap: 12px;
}
}
Observed behavior:- Grid shifts from 3 columns to 1 column
- Navbar stays aligned
- No overlap
- No broken layout
Tap ❤️ For More