Skip to main content

Command Palette

Search for a command to run...

Mastering CSS Flexbox – Building Modern Layouts

Updated
4 min readView as Markdown

Over the last few days, I've learned about the CSS Box Model and how elements are displayed and positioned on a webpage.

Today, I explored CSS Flexbox, one of the most powerful layout systems in modern web development.

Before learning Flexbox, arranging elements horizontally or vertically often required complicated techniques.

Flexbox simplifies this process by providing an easy and flexible way to align, distribute, and organize items within a container.

Let's dive in.


What is Flexbox?

Flexbox (Flexible Box Layout) is a one-dimensional layout model used to arrange elements in rows or columns.

Instead of positioning each element manually, Flexbox allows the browser to automatically distribute space between items.

To start using Flexbox, we first need a Flex Container.

.container {
    display: flex;
}

Once display: flex is applied, all direct children become Flex Items.


Flex Container vs Flex Items

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

Here:

  • .container → Flex Container

  • All <div> elements inside it → Flex Items


Flex Direction

The flex-direction property controls the direction in which items are arranged.

.container {
    display: flex;
    flex-direction: row;
}

Common Values

  • row (default)

  • row-reverse

  • column

  • column-reverse

Example:

.container {
    flex-direction: column;
}

Items are stacked vertically.


Justify Content

Controls alignment along the Main Axis.

.container {
    justify-content: center;
}

Common values:

  • flex-start

  • center

  • flex-end

  • space-between

  • space-around

  • space-evenly

This property is useful for spacing items evenly across a container.


Align Items

Controls alignment along the Cross Axis.

.container {
    align-items: center;
}

Common values:

  • stretch

  • flex-start

  • center

  • flex-end

  • baseline


Flex Wrap

By default, Flexbox tries to keep all items on one line.

.container {
    flex-wrap: wrap;
}

This allows items to move to the next line when there isn't enough space.

Very useful for responsive layouts.


Gap

Instead of using margins between items, Flexbox provides the gap property.

.container {
    gap: 20px;
}

This creates consistent spacing between flex items.


Flex Grow

Determines how much an item should grow compared to other items.

.item {
    flex-grow: 1;
}

Items with larger flex-grow values occupy more available space.


Flex Shrink

Determines how items shrink when there isn't enough space.

.item {
    flex-shrink: 1;
}

Flex Basis

Specifies the initial size of a flex item.

.item {
    flex-basis: 200px;
}

The flex Shorthand

Instead of writing:

flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;

We can simply write:

flex: 1 1 200px;

This is the preferred approach in real-world projects.


Main Axis vs Cross Axis

Understanding these two axes is the key to mastering Flexbox.

Main Axis  ➜ ➜ ➜

+----------------------------+
|                            |
|   Item   Item   Item       |
|                            |
+----------------------------+

Cross Axis
⬇
⬇
⬇
  • justify-content → Main Axis

  • align-items → Cross Axis

Once this concept clicked, Flexbox became much easier to understand.


Best Practices

✔ Use Flexbox for one-dimensional layouts.

✔ Use gap instead of margins whenever possible.

✔ Enable flex-wrap for responsive layouts.

✔ Keep layouts simple and readable.

✔ Understand the difference between the Main Axis and Cross Axis.


My Biggest Takeaway

Before today, arranging elements neatly on a webpage felt difficult.

Flexbox changed that.

Instead of manually calculating spacing and alignment, I can now let the browser handle it intelligently.

Learning about the Main Axis, Cross Axis, and properties like justify-content, align-items, and gap has given me a much better understanding of modern CSS layouts.

I can already see why Flexbox is used in navigation bars, cards, hero sections, and many responsive website layouts.

3 views

100 Days of Code: My Journey to Becoming a Full Stack Developer

Part 26 of 50

Welcome to my 100 Days of Code journey! In this series, I'll document my daily progress as I learn Full Stack Web Development from the ground up. Every post will cover what I learned, challenges I faced, mistakes I made, and the projects I built. My goal is not just to complete 100 days but to become a better developer through consistency, discipline, and learning in public. Topics I'll cover include: • Git & GitHub • HTML, CSS & JavaScript • React.js • Node.js & Express • MongoDB • APIs • Real-world Projects • AI tools for Developers Whether you're just starting out or revising your fundamentals, I hope this journey helps you learn alongside me. Let's build, learn, and grow together! 🚀

Up next

Mastering CSS Grid – Building Two-Dimensional Layouts

Yesterday, I learned how Flexbox makes arranging elements in a single row or column simple and responsive. Today, I explored CSS Grid, another powerful layout system that allows us to design web pages

More from this blog

T

TheSaurceCode

73 posts

Documenting my journey to becoming a Full Stack Developer through daily blogs, coding challenges, projects, tutorials, and lessons learned. Learn, build, and grow with me