Course Content
Project file
0/1
Complete Web Designing Course for Beginners

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

  1. What is Flexbox?
  2. What is a Flex Container?
  3. What are Flex Items?
  4. What does display:flex do?
  5. What is justify-content?
  6. What is align-items?
  7. 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.

Scroll to Top