CSS Flexbox

Introduction

CSS Flexbox is a powerful layout system used to design flexible and responsive layouts. It makes it easy to align items, distribute space, and create modern UI designs without using complex positioning or floats.

Flexbox works on a one-dimensional layout (row or column) and is widely used in real-world web development.

What You’ll Learn

  • What is Flexbox
  • Flex container and flex items
  • Main axis and cross axis
  • Flexbox properties
  • Real-world usage

What is Flexbox?

Flexbox (Flexible Box Layout) is a one-dimensional layout system used to:

  • Align items
  • Distribute space
  • Control layout direction
  • Make responsive designs easier

It works on:

  • Row (horizontal)
  • Column (vertical)

Basic Structure of Flexbox

Flexbox always has:

  • Flex Container (parent)
  • Flex Items (children)

Example:

<div class="container">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
</div>
.container {
  display: flex;
}

πŸ‘‰ Once you add display: flex;, the container becomes a flex container and all children become flex items.

πŸ”Ή Important Concept: Main Axis vs Cross Axis

Understanding axes is very important in Flexbox.

If flex-direction: row

  • Main axis β†’ horizontal
  • Cross axis β†’ vertical

If flex-direction: column

  • Main axis β†’ vertical
  • Cross axis β†’ horizontal

πŸ‘‰ All alignment properties depend on these axes.

Important Flexbox Properties

πŸ”Ή flex-direction

Controls direction of items.

.container {
  display: flex;
  flex-direction: row; /* default */
}

Values:

  • row
  • row-reverse
  • column
  • column-reverse

πŸ”Ή justify-content

Aligns items along the main axis.

.container {
  justify-content: center;
}

Values:

  • flex-start
  • flex-end
  • center
  • space-between
  • space-around
  • space-evenly

πŸ‘‰ Used for horizontal alignment (in row direction).

πŸ”Ή align-items

Aligns items along the main axis.

.container {
  align-items: center;
}

Values:

  • stretch
  • flex-start
  • flex-end
  • center
  • baseline

πŸ‘‰ Used for vertical alignment (in row direction).

πŸ”Ή flex-wrap

Controls whether items move to the next line.

.container {
  flex-wrap: wrap;
}

πŸ‘‰ Prevents overflow when items don’t fit in one line.

πŸ”Ή gap

Adds space between items.

.container {
  display: flex;
  gap: 20px;
}

πŸ‘‰ Cleaner and better than using margins.

Practical Example (Very Important)

Centering Content Perfectly

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

πŸ‘‰ This centers content both horizontally and vertically.

Used in:

  • Hero sections
  • Login forms
  • Cards
  • Landing pages

πŸ”Ή Flex Item Properties (Advanced Basics)

πŸ”Έ flex-grow

Defines how much space an item should take.

.box {
  flex-grow: 1;
}

πŸ‘‰ Items expand equally.

πŸ”Έ flex-shrink

.box {
  flex-shrink: 0;
}

πŸ‘‰ Prevents shrinking.

πŸ”Έ flex-basis

.box {
  flex-basis: 100px;
}

πŸ‘‰ Sets initial size before spacing is distributed.

πŸ”Έ flex (Shorthand)

.box {
  flex: 1;
}

πŸ‘‰ Combines grow, shrink, and basis.

Real Use Case for Your Website

You can use Flexbox for:

  • Navigation bar
  • Cards layout
  • Footer columns
  • Blog post grids
  • About page sections

Example for navbar:

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

πŸ‘‰ This is used in almost every website.

πŸ”Ή Complete Example

<!DOCTYPE html>
<html>
<head>
<style>
  .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
  }

  .box {
    width: 100px;
    height: 100px;
    background: blue;
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
</head>
<body>

<div class="container">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
</div>

</body>
</html>

πŸ”Ή Common Mistakes

  • Forgetting display: flex
  • Confusing justify-content and align-items
  • Not using flex-wrap
  • Overusing fixed widths

πŸ”Ή Best Practices

  • Use Flexbox for alignment and layout
  • Keep layouts simple
  • Use gap instead of margins
  • Combine with responsive design

πŸ”Ή Practice Tasks:

Task 1: Create a Flex Container

Create a container with 3 boxes and apply display: flex.

πŸ‘‰ Output should show items in a row.

Task 2: Change Direction

Use flex-direction: column.

πŸ‘‰ Items should appear vertically.

Task 3: Center Items

Center all items horizontally and vertically using:

  • justify-content
  • align-items

Task 4: Add Spacing

Add space between items using gap.

πŸ‘‰ Try different values like 10px, 20px.

Task 5: Use justify-content

Apply different values:

  • center
  • space-between
  • space-around

πŸ‘‰ Observe alignment changes.

Task 6: Use align-items

Try:

  • flex-start
  • center
  • flex-end

πŸ‘‰ See vertical alignment differences.

Task 7: Enable Wrapping

Use flex-wrap: wrap.

πŸ‘‰ Reduce screen width and observe behavior.

Task 8: Create a Navbar

Create a simple navbar using Flexbox.

πŸ‘‰ Use:

  • justify-content: space-between
  • align-items: center

Task 9: Equal Width Boxes

Use flex: 1 on all items.

πŸ‘‰ All boxes should have equal width.

Task 10: Center a Box in Page

Create a full-height container and center a box using Flexbox.

πŸ‘‰ Use:

  • height: 100vh
  • justify-content
  • align-items

Conclusion

CSS Flexbox is an essential layout tool for modern web development. By understanding how flex containers and items work, along with alignment properties, you can build responsive and clean layouts easily.

πŸ‘‰ In the next tutorial, you will learn CSS Flexbox – Advanced, which is used for more complex designs.

Related Tutorials

  • CSS Introduction

    Introduction CSS stands for Cascading Style Sheets. It is used to style and design web pages created with HTML. While HTML is used to structure content, CSS is used to control how that content looks on the screen. With CSS, you can change colors, fonts, layouts, spacing, and overall appearance of a website. In this…

  • CSS Syntax & Selectors

    Introduction CSS syntax defines how styles are written and applied to HTML elements. Selectors are used to target specific elements on a webpage and apply styles to them. Understanding CSS syntax and selectors is essential for writing effective and clean CSS code. In this lesson, you will learn the basic structure of CSS and different…

  • Colors, Units & Backgrounds

    Introduction CSS allows you to control the appearance of web pages using colors, units, and background properties. These are essential for designing visually appealing and responsive websites. In this lesson, you will learn how to apply colors, use different units, and style backgrounds in CSS. What You’ll Learn These are core CSS skills used on…

  • CSS Borders & Box Model

    Introduction CSS borders and the box model are essential concepts for designing layouts and controlling spacing in web development. Every HTML element is treated as a rectangular box, and understanding how this box works helps you create clean and structured designs. In this lesson, you will learn how to style borders and understand the CSS…

  • CSS Display & Position

    Introduction CSS display and position properties control how elements are shown and placed on a web page. These properties are essential for creating layouts, aligning elements, and building modern web designs. In this lesson, you will learn how different display types work and how positioning helps control element placement. What You’ll Learn πŸ”Ή CSS Display…

  • CSS Flexbox – Advanced

    Introduction In the previous lesson, you learned the basics of Flexbox, including alignment, direction, and spacing. In this advanced guide, we will explore deeper Flexbox concepts that are used in real-world layouts. These advanced properties help you build more flexible, responsive, and professional designs. What You’ll Learn Quick Recap (Short Section) Flexbox works with: Main…

Leave a Reply

Your email address will not be published. Required fields are marked *