Flexbox
đŻ Learning Goals
- Explain the difference between a parent and child, and a direct child
- Apply Flexbox to containers to achieve a desired layout
đ Technical Vocabulary
- Child
- Container
- Direct child
- Item
- Parent
đ What is Flexbox?
Flexbox is a part of CSS that provides a more efficient way to layout, align, and distribute space among items in a container. It helps us when we have those silly block elements that can be hard to do just what we want them to do.
Flexbox IRL
Flexbox is used all over the internet. Itâs a little tough to learn, but once you know it, it makes your life a lot easier! Thatâs why it is so popular among front-end developers.
Here are just a few popular pages that use Flexbox:
- Disney Plus uses Flexbox on the top Navbar and the grey bar at the bottom of the landing page
- Lomotif uses Flexbox to control the content on the right side of the landing page (title, subtitle, and two buttons)
- The Ringer uses Flexbox to organize the links in the nav bar at the top of the page. It also uses Flexbox for each âsectionâ of content - usually an image, title, subtitle, author, date, and tag
Parents and Children
Before we start working with Flexbox, we need to make sure we are referring to elements and their relationship to other elements correctly. We need to be careful about the CSS rules we apply to a parent element vs. those to a child element. A parent element wraps other elements, and a child is nested inside the parent. We will also refer to parents as containers, and children as items.
Letâs look an some HTML to make sure we are all on the same page:
<section class="container">
<article class="item-in-container"></article>
<article class="item-in-container"></article>
<article class="item-in-container"></article>
</section>
In the code above, the section
is the parent/container, and the 3 article
s are all children/items because they are directly nested inside of that section
. Letâs looks at one more example:
<section class="container">
<article class="item-in-container">
<h2>Title of Article 1</h2>
</article>
<article class="item-in-container">
<h2>Title of Article 2</h2>
</article>
<article class="item-in-container">
<h2>Title of Article 3</h2>
</article>
</section>
In the code above, we now have these h2
elements nested inside of the article
s. Itâs important to know that h2
is not a child of the section
. It is technically a grandchild of the section
, and a child of article
. The idea of direct child is really important to understand as we work with Flexbox.
(Graphics from CSS Tricks)
Creating a Flex Container
Without Flexbox, 10 colorful article
s might look like this:
But with Flexbox, we can start, having some control over them:
đŁ Discuss
Looking at the CSS in both examples above, what is the difference between the two CSS files?
In the CSS file of the turn-talk-flexbox-applied Glitch project, what element(s) is the declaration display: flex;
applied to? Is that a parent or child?
Takeaways:
- To use Flexbox, we need a container element with one or more children inside of it
- The declaration
display: flex;
should be on the parentâs rule - The parent wonât be affected, but the way the children elements sit inside the parent may change
Flex helps even things out
Adding display: flex
to the CSS rule on the parent makes the parent element a flex container and opens up a world of possibilities.
We can apply a width to each item. If all items can fit in the container at that width, thatâs the width they will be. If the width wouldnât leave enough room for all the items, they will become as wide as they can and remain evenly spaced.
Remix Glitch project below. Change the width of the
article
with thebox
class to several different values and see what happens.
Justify Content
In professional apps, we typically see white space (margin or padding) used, and the content is centered on the screen. We can use Flexbox to center content. By adding justify-content: center;
, the items in the container are now centered instead of being crunched up on the left side of the container.
Remix the Glitch project below. On the containerâs CSS rule, change the code to:
justify-content: space-between;
. What happens? Then, change it to:justify-content: space-around;
. What is the difference between space-around and space-between?
The justify-content
property allows us to control how our content sits in relation to the main axis (for now, this means horizontally).
Learn about even more values that we can provide to justify-content
with CSS Tricks.
Try It: Flexbox Froggy
Work your way through Levels 1 - 4 of Flexbox Froggy.
Align Items
Just like we can control how our content sits in relation to the main axis with justify-content
, we have a tool to control how our content sits in relation to the secondary axis.
Remix the Glitch project below. Try changing the value for
align-items
toflex-end
, thenflex-start
, and see what happens!
Try It: Flexbox Froggy
Work your way through levels 5 - 7 of Flexbox Froggy.
Make sure to check in with your partner every few levels to see if they need any help!
Direction
Another CSS property with Flexbox is flex-direction
. This property takes one of four values:
row
(default): left-to-rightrow-reverse
: opposite of row; right-to-leftcolumn
: same asrow
but top to bottomcolumn-reverse
: same ascolumn
but bottom to top
The direction of your flex container defines the main axis.
Here is a visual, created by the amazing Samantha Ming.
Try It: Flex Direction
Remix this Glitch project.
On line 10 of the CSS file there is a "____" as the value for the flex-direction
property. One by one, change that out for each of the four properties listed in the visual above. What happens to the 10 items?
Back in Flexbox Froggy, work through levels 8 - 13.
Wrap
Now, sometimes we donât want all our items on the same row or column. We can use the property flex-wrap
. It allows items to wrap onto the next line. The three values it takes are:
no-wrap
(default): one line, direction is defined byflex-direction
wrap
: multi-lines, the direction is defined byflex-direction
wrap-reverse
: multi-lines, opposite to the direction defined byflex-direction
Remix the Glitch project below. Try changing the value for
flex-wrap
tono-wrap
, thenwrap-reverse
to observe the affect that property has!
Flexbox
There is more to learn about Flexbox, but we can do a lot with what we know. It will take a while to get used to; remember to use your Dev Tools and use that border
property to help you understand what space each element is taking up.
Practice: Flexbox
Remix this Glitch project.
Your job will be to update the CSS (you'll probably need to add some classes on some HTML elements, too!) so the outcome looks like the screen shot below, but first, take some time to jot down some notes and talk with a partner about how you will approach this.

More Practice
- Scrimba offers a free course that is under an hour long. This might be a great watch if youâre struggling to understand Flexbox!
- Finish levels 14 - 24 of Flexbox Froggy
- Canât get enough of Flexbox Froggy? Try out Flexbox Defense