7. Flexbox

7. Flexbox

Welcome to Flexbox!

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 tell them to do exactly what we want.

🎯 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

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:
  • HBO max uses Flexbox on the top Navbar of the landing page and in the sections that display titles as you scroll.
  • Adidas uses Flexbox for the product tiles in the Popular Right Now section of its landing page.

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 articles are all children/items because they are directly nested inside of that section. An article is an element that contains content specific to itself. The article element could be blog post, a news article, and any content of a webpage that could standalone.: Source.
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 articles. 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. In other words, the articles are direct children of section, just as h2 is a direct child of article The idea of direct child is really important to understand as we work with Flexbox.
notion image
(Graphics from CSS Tricks)

Creating a Flex Container

Without Flexbox, 10 colorful articles might look like this:
notion image
But with Flexbox, we can start having some control over them:
notion image
💭
Think About It:
Looking at the CSS in both examples below, what is the difference between the two CSS files?
In the CSS file of the flexbox-applied 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
✏️
Try-It | Flexbox Froggy
Work your way through Levels 1 - 4 of Flexbox Froggy.

Flexbox Try-It Codealong!

✏️
Try-It
  1. Start by forking a copy of Flexbox Try-It Codealong.
  1. Follow the directions in the Sandbox!
💡
Flexbox PSA!
  • Flexbox is an incredibly powerful tool!
  • It’s impossible to memorize every property or scenario of Flexbox, so it’s okay not to!
notion image
📝
Practice #1 | 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 borderproperty to help you understand what space each element is taking up.
  1. Open this CodeSandbox Link
  1. 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 screenshot below, but first, take some time to jot down some notes about how you will approach this.
notion image
Hint 1
To move the red group of boxes, the green group of boxes, the blue group of boxes, and the purple group of boxes, use the following code in index.html to group the boxes by color:
<section class="red-g">
// The existing code for the red boxes
</section>
 
<section class="green-g">
// The existing code for the green boxes
</section>
 
<section class="blue-g">
// The existing code for the blue boxes
</section>
 
<section class="purple-g">
// The existing code for the purple boxes
</section>
 
Together,
<section class="boxes"> <section class="red-g"> // Grouping the red boxes <article class="red">1</article> <article class="red">2</article> <article class="red">3</article> </section> <section class="green-g"> // Grouping the green boxes <article class="green">1</article> <article class="green">2</article> <article class="green">3</article> </section> <section class="blue-g"> // Grouping the blue boxes <article class="blue">1</article> <article class="blue">2</article> <article class="blue">3</article> </section> <section class="purple-g"> // Grouping the purple boxes <article class="purple">1</article> <article class="purple">2</article> <article class="purple">3</article> </section> </section>

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!
📝
Practice #2 | Add Flexbox To Your About Me Site
Congratulations on learning Flexbox! Let’s now revisit your About Me Site and add some Flexbox to it.
  1. Update Your HTML
    1. Start by identifying what it is that you’d like to add Flexbox to. Your Icons & Inspirations page is a great place to start.
    2. Update the HTML with a container.
    3. Create your flex items by adding several child elements.
  1. Apply Flexbox in CSS
    1. In your CSS file, target your class and add flex properties
page icon
For a summary of this lesson, check out the 8. Flexbox One-Pager!