Fireship · @fireship
CSS Flexbox in 100 Seconds
Uploaded February 21, 2020 · Length 1:44
Summary
learn css flexbox layout in 100 seconds, including flex direction, justify content and align items.
- css flexbox
- flexbox layout
- css layout
- css tutorial
- css flex direction
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
Flexbox, a flexible way to layout items as either a column or a row.
- 0:04
Start with a container, like a div, and give it a display property of flex.
- 0:08
The flex container now has control over how its immediate children are laid out.
- 0:12
By default, the flex direction is set to row, and a row has a main axis that runs horizontally
- 0:17
and a cross axis that runs vertically.
- 0:20
If we add items to the row with a fixed width, they'll sit in the top left corner.
- 0:24
That's because the main axis starts at the left and the cross axis at the top.
- 0:27
The items will be laid out one after the other from the main axis starting point.
- 0:31
The amount of space that each item takes up on the main axis is known as the main size.
- 0:35
In this case, each item has a fixed width of 50 pixels.
- 0:39
But if we change the item width to 100%, the flexible container will distribute the items
- 0:43
evenly across the main axis.
- 0:45
You can now think of the sizing in terms of a ratio where each item takes up one unit
- 0:49
of space.
- 0:50
You can change that ratio by setting the flex property on an individual item.
- 0:53
If we set flex to 2 on the middle item, it now takes up 50% of the available space, because
- 0:58
there's a total of 4 flex units at play.
- 1:00
Now you'll often want to control the way items are spaced or aligned along the main
- 1:04
axis.
- 1:05
Use justify content to space your items evenly, maximize the space between, or push them to
- 1:09
the flex endpoint.
- 1:11
You can also use these same techniques along the cross axis with the align items property,
- 1:15
and that means we can easily align things both horizontally and vertically.
- 1:18
By both aligning and justifying the items to the center, we can easily convert our row
- 1:22
into a column by setting the flex direction to column. The only thing that changed is
- 1:27
that our main axis now runs vertically and the cross axis horizontally. Everything else
- 1:31
works exactly the same. Just tilt your head sideways. This has been CSS Flexbox in 100
- 1:36
seconds. Make sure to hit the like button if you want to see more short videos like
- 1:39
this, thanks for watching, and I will see you in the comments.


