Lesson 4.6 β Flexbox Basics
π Creating Modern Layouts Using CSS Flexbox
π― Learning Objectives
After completing this lesson, students will be able to:
β Understand Flexbox
β Create Flexible Layouts
β Align Elements Horizontally
β Align Elements Vertically
β Use Flex Container
β Use Flex Items
β Create Responsive Layouts
β Build Modern Website Designs
π Introduction
Imagine creating three boxes.
Without Flexbox:
+-------+
| HTML |
+-------+
+-------+
| CSS |
+-------+
+-------+
| JS |
+-------+
Boxes appear one below another.
But modern websites display them like:
+-------+ +-------+ +-------+
| HTML | | CSS | | JS |
+-------+ +-------+ +-------+
Everything is aligned perfectly.
This is done using:
π¦ CSS Flexbox
π€ What is Flexbox?
Flexbox stands for:
Flexible Box Layout
It is a CSS layout system used to arrange elements easily.
π Why Use Flexbox?
Before Flexbox:
β Complex layouts
β Difficult alignment
β Lots of CSS code
With Flexbox:
β Easy alignment
β Responsive layouts
β Less code
β Professional design
π― Real World Uses
Flexbox is used in:
β Navigation Bars
β Profile Cards
β Product Cards
β Login Forms
β Landing Pages
β Dashboards
π Flexbox Concepts
Flexbox has two parts:
1. Flex Container
Parent element
2. Flex Items
Child elements
Example
HTML
<div class="container">
<div>HTML</div>
<div>CSS</div>
<div>JavaScript</div>
</div>
Here:
<div class="container">
is the Flex Container.
These are Flex Items:
<div>HTML</div>
<div>CSS</div>
<div>JavaScript</div>
π― Creating a Flex Container
Use:
display:flex;
CSS
.container{
display:flex;
}
Result
Items appear side by side.
Output
HTML CSS JavaScript
π First Flexbox Example
HTML
<div class="container">
<div class="box">HTML</div>
<div class="box">CSS</div>
<div class="box">JS</div>
</div>
CSS
.container{
display:flex;
}
.box{
width:100px;
height:100px;
border:2px solid blue;
margin:10px;
}
Output
Three boxes appear in one row.
π― Flex Direction
Controls layout direction.
Syntax
flex-direction:value;
Row Direction
Default.
flex-direction:row;
Output
HTML CSS JS
Column Direction
flex-direction:column;
Output
HTML
CSS
JS
Example
.container{
display:flex;
flex-direction:column;
}
π Justify Content
Controls horizontal alignment.
Syntax
justify-content:value;
Center
Output
justify-content:center;
Items move to center.
Example
.container{
display:flex;
justify-content:center;
}
Output
HTML CSS JS
Space Between
justify-content:space-between;
Output
HTML CSS JS
Space Around
justify-content:space-around;
Output
HTML CSS JS
Space Evenly
justify-content:space-evenly;
Output
HTML CSS JS
Equal spacing.
π Align Items
Controls vertical alignment.
Syntax
align-items:value;
Center
align-items:center;
Example
.container{
display:flex;
height:300px;
align-items:center;
}
Result
Items appear vertically centered.
Start
align-items:flex-start;
Top alignment.
End
align-items:flex-end;
Bottom alignment.
π― Visual Example
Without Alignment
+-------------------+
HTML
CSS
JS
+-------------------+
With Center Alignment
+-------------------+
HTML
CSS
JS
+-------------------+
π Combining Justify and Align
Example
.container{
display:flex;
justify-content:center;
align-items:center;
height:300px;
}
Result
Perfect center alignment.
π Flex Container Styling
Example
.container{
display:flex;
background-color:lightgray;
padding:20px;
}
Result
Styled flex layout.
π Complete Example
HTML
CSS
.container{
display:flex;
justify-content:center;
}
.box{
width:120px;
height:120px;
background-color:lightblue;
border:2px solid blue;
margin:10px;
text-align:center;
}
Output
Three centered boxes.
π― Creating Navigation Menu
HTML
<nav class="menu">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Courses</a>
<a href="#">Contact</a>
</nav>
CSS
.menu{
display:flex;
justify-content:center;
gap:20px;
}
Output
Home About Courses Contact
π What is Gap?
Flexbox provides:
gap:value;
Example
gap:20px;
Result
20px spacing between items.
Example
.container{
display:flex;
gap:15px;
}
π― Flexbox vs Traditional Layout
Traditional Layout:
β Difficult alignment
β More code
Flexbox:
β Easy alignment
β Responsive
β Modern
π» Complete Practical Program
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="box">HTML</div>
<div class="box">CSS</div>
<div class="box">JS</div>
</div>
</body>
</html>
CSS
.container{
display:flex;
justify-content:center;
align-items:center;
gap:20px;
height:300px;
}
.box{
width:120px;
height:120px;
background-color:lightyellow;
border:2px solid orange;
text-align:center;
font-size:20px;
padding-top:40px;
}
π― Expected Output
Three attractive boxes:
HTML CSS JS
Centered horizontally and vertically.
π« Real World Examples
Navigation Bar
display:flex;
Product Cards
display:flex;
Team Member Section
display:flex;
Service Boxes
β οΈ Common Beginner Mistakes
Mistake 1
Applying Flexbox to Child
Wrong:
.box{
display:flex;
}
Usually Flexbox should be applied to parent container.
Mistake 2
Forgetting Height
align-items:center;
needs container height.
Mistake 3
Using Too Much Margin
Use:
gap
instead.
π Best Practices
β Apply flex to parent.
β Use gap for spacing.
β Use justify-content for horizontal alignment.
β Use align-items for vertical alignment.
β Use Flexbox for modern layouts.
π Mini Project β Course Cards
Create:
HTML
CSS
JavaScript
Bootstrap
Cards.
Apply:
β display:flex
β gap:20px
β center alignment
β borders
β colors
π Practical Activity
Create a navigation menu:
Home
About
Courses
Contact
using Flexbox.
Create four boxes:
HTML
CSS
JS
React
Display them in one row.
π Assignment
Assignment 4.6
- What is Flexbox?
- What is a Flex Container?
- What are Flex Items?
- What does display:flex do?
- What is justify-content?
- What is align-items?
- Create a navigation menu using Flexbox.
π§ Quiz
Q1. Which property enables Flexbox?
A. flex
B. display:flex
C. box:flex
D. layout:flex
β Answer: B
Q2. Which property controls horizontal alignment?
A. align-items
B. justify-content
C. flex-direction
D. gap
β Answer: B
Q3. Which property controls vertical alignment?
A. align-items
B. justify-content
C. display
D. gap
β Answer: A
Q4. Which property controls spacing between flex items?
A. margin
B. padding
C. gap
D. border
β Answer: C
Q5. Which direction is default in Flexbox?
A. column
B. vertical
C. row
D. center
β Answer: C
π Lesson Summary
β Flexbox is a modern layout system.
β
display:flex creates a flex container.
β Flex items are arranged inside the container.
β
flex-direction controls layout direction.
β
justify-content controls horizontal alignment.
β
align-items controls vertical alignment.
β
gap creates spacing between items.
β Flexbox is widely used in modern websites.