CSS Flexbox
Learning Goals
- Explain what flexbox is and why its an important tool for creating layouts
- Explain the difference between a parent and child element, be able to identify immediate children
- Apply Flexbox to containers in order to achieve a desired layout
Pre-Work
Read through Intro to Layout Pre-Work lesson and all links provided and complete exercises provided in it. Be prepared to demonstrate your understanding of the concepts in that document when you come to this class.
Vocabulary
flexbox
- a layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spacesdisplay
- a css property that sets whether an element is treated as a block or inline element and the layout used for its children, such as grid or flexflex parent
orflex container
- Any HTML element that has been given thedisplay: flex
declarationflex child
orflex item
- Any immediate descendants of a flex parentmain axis
– the primary axis along which flex items are laid out. It can be horizontal or vertical, and is defined by the direction set by theflex-direction
property.
Warm Up
- With a partner, clone down and open up this repo
- Explore the CSS that’s already present. Without googling, what do you think
:nth-child
means? - In your CSS, add the property of “display” with a value of “flex” to the
.wrapper
selector. What happened? Which elements visually changed?
What is Flexbox?
Flexbox is a part of CSS that provides an efficient way to lay out, align and distribute space among items in a container. Before flexbox became popular, it was a real challenge to center elements. We would use something called float
, which could behave unpredictably at times.
Parents and Children
As we start working with flexbox, a very important distinction should be pointed out. 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, a child is nested inside the parent.
Let’s look an some HTML to review the parent <> child relationships. Which element is the parent and which are its children?
<section>
<h1></h1>
<article></article>
<article></article>
<article></article>
</section>
The Answer
In the code above, the section
is the parent element, the <h1>
and the 3 article
s are all children elements because they are directly nested inside of that section
. Proper indentation is really helpful here!
What about in this block of HTML?
<section>
<article>
<h2>Title of Article 1</h2>
</article>
<article>
<h2>Title of Article 2</h2>
</article>
<article>
<h2>Title of Article 3</h2>
</article>
</section>
The Answer
In the code above, we now have these h2
elements nested inside of each article
. It’s important to know that the h2
is not an immediate child of the section
. It is technically a grandchild, and a child of article
. The idea of immediate child is really important to understand as we work with Flexbox.
When we use Flexbox, we will make the parent elements flex containers
and the children elements flex items
.
(Graphics from CSS Tricks)
Try It 1
- Go back to your repo from the warm up
- Experiment adding the
justify-content
property to your.wrapper
. Use the browser’s dev tools to apply the following values (one at a time), and note what changes:center
space-around
space-between
space-evenly
flex-end
Justify Content
justify-content
allows us to define the alignment of items (flex children) along the main axis, and is incredibly useful for centering elements.
flex-start
(default): items are packed toward the start lineflex-end
: items are packed toward to end linecenter
: items are centered along the linespace-between
: items are evenly distributed in the line; first item is on the start line, last item on the end linespace-around
: items are evenly distributed in the line with equal space around themspace-evenly
: items are distributed so that the spacing between any two adjacent alignment subjects, before the first alignment subject, and after the last alignment subject is the same
Here is a great example of how you can use justify-content
to move your items:
See the Pen Flexbox & justify-content by CSS-Tricks (@css-tricks) on CodePen.
The above Codepen is an example from CSS Tricks
Note
Most flexbox-related properties have default values. We don’t see them in our CSS, but can know that they are being applied! These properties can always be changed by us. You’ll see some default values indicated below.
Try It 2
- Go back to your repo from the warm up
- Now add
flex-direction: column;
to your.wrapper
- What happened? How does changing the values for the
justify-content
property affect the boxes?
Flex 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-left)column
: same asrow
but top to bottomcolumn-reverse
: same ascolumn
but bottom to top
The flex-direction
property defines the main axis
Try It 3
- Go back to your repo from the warm up
- Remove the
justify-content
andflex-direction
properties. - Now, add in an
align-items
property and experiment with the following values:stretch
center
baseline
flex-start
flex-end
- Then, add
flex-direction: column;
to your.wrapper
. What changes?
Align Items
Just like we can control how our content sits on the main axis with justify-content
, we have a tool to control how our content sits on the cross-axis.
stretch
(default): stretch to fill the container (still respect min-width/max-width)flex-start
: cross-start margin edge of the items is placed on the cross-start lineflex-end
: cross-end margin edge of the items is placed on the cross-end linecenter
: items are centered in the cross-axisbaseline
: items are aligned such as their baselines align
Note
These properties will get you far enough for now, but they’re just scratching the surface at what flex can do! If you want more, check out this extensive guide from CSS tricks.
Tips and Tricks (write these in your notes!)
When you are about to use flexbox, ask yourself the following questions:
- What elements do I want to move?
- What is the parent element of those elements? (this is where the flex properties should live!)
If flex isn’t working the way you think it should, check the following things:
- Did I remember to include
display: flex;
?
Without that line of code, the other flex properties won’t work! - Am I sure I’m adding the flex properties to the correct element?
Check the parent-child relationship in the HTML file to be sure that you are adding those properties to the direct parent.
You might have to create/add a direct parent in the HTML if there isn’t already one - a great use case for <div> - Is the parent element big enough?
Add a border to the parent element. If it is the same width and/or height as the children, you won’t be able to move those children as expected.
CSS tends to require a lot of trial and error when you’re first learning it. Often, the CSS flow for newbies is something like:
- Developer edits their css file and reloads their webpage to see if it’s what they want.
- Developer realizes it’s not quite right, so they edit their CSS file again, reload again, on and on and on.
We encourage you to make your CSS changes in your dev tools which will instantly show you the result. You can keep editing the CSS right in your dev tools until it’s right, then copy those rules over to your text editor and save yourself all the time on the back and forth.
Using Flexbox on Nested Elements
Try It 4
- In your repo, switch to the
flexbox-nested-elements
branch by following the steps below:- In your terminal, run
git fetch
to fetch all the remote branches - Then run
git checkout flexbox-nested-elements
to move over to that branch.
- In your terminal, run
- Explore the existing code and the UI
- Complete the 3 challenges listed in the CSS file.
Recreating a Comp
We will now continue working on the comp from the CSS Fundamentals lesson. You will have one hour to work on iteration 2
of this activity.