Understanding CSS Flexbox

Understanding CSS Flexbox

Flex display is one of css most important concept and its base but it's pretty tricky to understand, I remember struggling to use it at first, now I use it everywhere. I will be talking about css flexbox and their most important components.

What is css flexbox:

In simple words; a flexbox is a flexible box and layout that makes it easy to create a responsive layout and control items and boxes around a page.

Main / Cross Axis:

You will come across these two terms "main axis" and "cross axis" when learning about the flex container. Every css flex container has the two properties "main axis" & "cross axis".

Main axis

All items in a flex container are always laid out based on the direction of the main axis, in other words, main axis is the flex-direction property of css.

t.png

Main axis flex-direction column.png

Main start & Main end

Each main axis got a main start and a main end. Items are place within the start and the end of the main axis.

t (1).png

Cross axis

The cross axis always runs the opposite way of the main axis, it's in the name, it crosses the main axis. For example; if the main axis is a row, then the cross axis is vertical to the main axis, and vice versa.

Cross start & Cross end

It works the same as the Main start and main end, items are place withing the start and end of the cross axis.

Your paragraph text.png

Main axis flex-direction column (1).png

Justify content

Justify content is what aligns items on the main axis, it helps control the alignement of items on the page, justify content uses 6 values:

justify-content: start

Screenshot 2022-07-25 at 11-05-42 Flex Understand Flexbox.png

justify-content: end

Screenshot 2022-07-25 at 11-06-08 Flex Understand Flexbox.png

justify-content: center

Screenshot 2022-07-25 at 09-34-55 Flex Understand Flexbox.png

justify-content: space-evenly

Screenshot 2022-07-25 at 09-34-12 Flex Understand Flexbox.png

justify-content: space-between

Screenshot 2022-07-25 at 09-39-20 Flex Understand Flexbox.png

justify-content: space-around

Screenshot 2022-07-25 at 11-01-48 Flex Understand Flexbox.png

Align items / self

Align items

Align items, almost similair to Justify content yet different, is what aligns items on the cross axis, it uses 5 flex values:

align-items: flex-start

Screenshot 2022-07-25 at 11-19-45 Flex Understand Flexbox.png

align-items: flex-end

Screenshot 2022-07-25 at 11-20-57 Flex Understand Flexbox.png

align-items: center

Screenshot 2022-07-25 at 11-10-54 Flex Understand Flexbox.png

align-items: stretch

Screenshot 2022-07-25 at 11-21-50 Flex Understand Flexbox.png

align-items: baseline

Screenshot 2022-07-25 at 12-20-17 Flex Understand Flexbox.png

Align-items: baseline: sometimes the baseline and flex-start values behave the same way and that occures when the items are the same size. In the example above I changed the font-size of first and third boxes and it made a huge difference, the baseline value occures when the font size of an item is larger, it aligns them based on the largest element.

Align self

can be used for a single flex item, it overrides the main flex items property, basically it controls an item individually. I've set the second box to a black background and used it to display the align-self property. It uses 5 values:

align-self: flex-start

Screenshot 2022-07-25 at 11-49-41 Flex Understand Flexbox.png

align-self: flex-end

Screenshot 2022-07-25 at 11-52-23 Flex Understand Flexbox.png

align-self: center

Screenshot 2022-07-25 at 11-51-29 Flex Understand Flexbox.png

align-self: stretch

Screenshot 2022-07-25 at 11-51-02 Flex Understand Flexbox.png

align-items: baseline

Screenshot 2022-07-25 at 12-22-22 Flex Understand Flexbox.png

Flex Direction

As I talked about above, the flex direction defines the main axis direction, there two main flex direction with a reversed one on each.

Column

flex-direction: column Aligns items from top to bottom.

Screenshot 2022-07-18 at 23-51-31 Flex Understand Flexbox.png

flex-direction: column-reverse Align items from bottom to top, it basically reverses the order.

Screenshot 2022-07-25 at 12-46-56 Flex Understand Flexbox.png

Row

flex-direction: row Aligns items from left to right.

Screenshot 2022-07-18 at 23-50-28 Flex Understand Flexbox.png

flex-direction: row-reverse Aligns items from right to left.

Screenshot 2022-07-18 at 23-52-19 Flex Understand Flexbox.png

Flex-wrap

Flex-wrap means items inside a flex container will try and fit into one line as the page shrinks. A flex container is set to flex-wrap: no-wrap by default, this can be change to two other properties that help to wrap items. It's pretty handy when working with responsivness.

The codepen below has a flex container with a flex-wrap property, check it out and try and shrink the page and you will notice the difference.

How to center elements using flexbox

First thing that everyone should know is how to center a div in the middle of the page using css flexbox, it's really handy and easy. I use it all the time.

I created a parent div which holds three boxes, I added a width and height + set a background color for each box so it's visible and easy to understand.

  • The first thing to do is to set the parent class to flex, the boxes will automatically be set to a horizontal row, then add the horizontal alignement by typing justify-content:center, the three boxes will center horizontally in the page.
  • To center element vertically, first, you have to set a height to the page, it's prefered to set it to min-height : 100vh, then add the following align-items: center. Without setting a height, the align-items won't work, it needs a height so that it centers the element.

Screenshot 2022-07-25 at 12-07-51 Flex Understand Flexbox.png

That's it!

I only talked about the concepts that are used in almost evey flex container, and the ones that I remember getting stuck at. There few other ones that aren't stated above, make sure you also learn them too, they're as equally as important. I recommend checking out CSS-Tricks they have really simple css explanation and are really clear to follow up. Also, you can always find me on Twitter, so if you need any help or have a suggestion, be sure to come DM me there.