CSS Flexbox Step by Step

Frontend ~ 8 minutes

# Welcome to Flexbox

Hello and welcome! Today we will learn CSS Flexbox. Flexbox is a tool that helps us place items in a row or in a column. It makes layouts easy. Before Flexbox, people used tricks like floats, and those were hard to control.

With Flexbox, you can center things, space them evenly, and make them change size when the screen changes. Do not worry if this sounds big. We will go step by step.

What you will learn

  • What a flex container and flex items are
  • How to change direction
  • How to align and space items
  • How to wrap items and make them grow

You need to know a little HTML and CSS. If you know how to write a CSS rule, you are ready. Let us start by making a project folder.

Create a practice folder
Code Copied to Clipboard

# Set up the files

Good! Now, please open the new folder in your editor. Make two files inside it. Name the first one index.html and the second one style.css.

In index.html, we will link the CSS file and add a simple box with three items inside. The outer box will be our flex container. The three items inside will be our flex items.

This idea is the most important part of Flexbox. You turn on Flexbox on the parent. Then the children follow its rules. Please remember this: the parent controls the layout, and the children are controlled.

Let us write the HTML now. It is short and simple.

The HTML file
Code Copied to Clipboard

# Turn on Flexbox

Now, let us open style.css. To start Flexbox, we write one line: display: flex;. We put it on the container. That is all! The three items now sit side by side in a row.

Before this line, the div items were stacked one under another. This is because div is a block element. After we use display: flex, the items become flex items, and they line up in one row.

We will also add some simple colors and spacing, so we can see each item clearly. A border and some padding will help.

Things to notice

  • The container has display: flex
  • The items sit in a row by default
  • The items shrink to fit their content

Please type the following CSS and save the file.

Main axis and cross axis

Flexbox has two directions. The main axis is the direction that items follow. By default it goes from left to right. The cross axis is the other direction, from top to bottom.

This is very important, because the two alignment properties depend on these axes. The property justify-content works on the main axis. The property align-items works on the cross axis.

If you change the direction to a column, the axes swap. Then justify-content works from top to bottom. Keep this in mind, and Flexbox will feel much easier. You can read more on MDN Flexbox basics.

Start the container
Code Copied to Clipboard

# Direction, alignment and spacing

Now we will control the layout. First, the flex-direction property. Its default value is row. If you set it to column, the items stack from top to bottom.

Next, justify-content decides how items sit along the main axis. Common values are flex-start, center, flex-end, space-between and space-around. The value space-between puts the first item at the start, the last item at the end, and shares the free space between them.

Then, align-items decides how items sit on the cross axis. The value center puts them in the middle. To center something perfectly, both in the middle and from the side, we use these two properties together.

Useful container properties

  • flex-direction for row or column
  • justify-content for the main axis
  • align-items for the cross axis
  • gap for space between items

Please add these lines to your .container rule and try different values.

Center and space items
Code Copied to Clipboard

# Open it in the browser

Let us see our work in the browser. You can double click index.html. But a local server is nice, because the page updates the same way a real website does.

If you have Node.js installed, you can use a small helper tool called serve. It starts a server in one command. Please run the command below inside your project folder. Your terminal will show an address. Open it in your browser.

If you do not have Node.js, do not worry. You can simply open the file by double click. Everything in this tutorial works both ways. After each CSS change, save the file and refresh the page. Then you will see how each property changes the layout. Playing with the values is the best way to learn.

Start a quick preview server
Code Copied to Clipboard

# Wrap, grow and shrink

What happens when there are many items and not enough space? By default, Flexbox squeezes them into one line. To allow new lines, we use flex-wrap: wrap. Then extra items go to the next row.

Now let us talk about the items themselves. The flex-grow property tells an item how much extra space it can take. If one item has flex-grow: 1, it fills all the free space. If all items have flex-grow: 1, they share the space equally.

The flex-shrink property is the opposite. It tells an item how much it may shrink when the space is small. The flex-basis property sets the starting size before growing or shrinking.

Item properties to know

  • flex-grow to take extra space
  • flex-shrink to give up space
  • flex-basis for the starting size
  • flex as a short way to write all three

Let us see a card layout that wraps and grows.

Wrapping cards
Code Copied to Clipboard
Mistakes to avoid

Here are some common problems. Please read them with care.

First, remember that display: flex goes on the parent, not on the items. If you put it on the item, nothing happens to its brothers and sisters.

Second, justify-content and align-items depend on the direction. If your layout does not look right, check flex-direction first. In a column, the axes are swapped.

Third, if align-items: center does nothing, your container may have no height. The items cannot be centered in a space that does not exist. Give the container a height or min-height.

Quick checklist

  • Is display: flex on the parent?
  • Did you check the flex-direction?
  • Does the container have enough height?
  • Did you try gap instead of margins?

The browser developer tools can also help. In Chrome or Firefox, right click the page and choose Inspect. A small flex badge lets you see the flex layout.

# Summary and next steps

Congratulations! You have learned the basics of Flexbox. Let us review the main ideas.

You know that Flexbox starts with display: flex on a parent. You know about the main axis and the cross axis. You can change direction with flex-direction, place items with justify-content and align-items, and add space with gap. You also learned how to wrap items and how to use flex-grow.

What to do next

  • Build a simple navigation bar with Flexbox
  • Make a row of cards that wraps on small screens
  • Learn CSS Grid for two-direction layouts
  • Play the fun game Flexbox Froggy

Flexbox is best for layouts in one direction, like a row or a column. For bigger page layouts, CSS Grid is a good partner. Keep practicing, and soon this will feel natural. Well done!