Fireship · @fireship

CSS Flexbox in 100 Seconds

Uploaded February 21, 2020 · Length 1:44

CSS Flexbox in 100 Seconds

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.

  1. 0:00

    Flexbox, a flexible way to layout items as either a column or a row.

  2. 0:04

    Start with a container, like a div, and give it a display property of flex.

  3. 0:08

    The flex container now has control over how its immediate children are laid out.

  4. 0:12

    By default, the flex direction is set to row, and a row has a main axis that runs horizontally

  5. 0:17

    and a cross axis that runs vertically.

  6. 0:20

    If we add items to the row with a fixed width, they'll sit in the top left corner.

  7. 0:24

    That's because the main axis starts at the left and the cross axis at the top.

  8. 0:27

    The items will be laid out one after the other from the main axis starting point.

  9. 0:31

    The amount of space that each item takes up on the main axis is known as the main size.

  10. 0:35

    In this case, each item has a fixed width of 50 pixels.

  11. 0:39

    But if we change the item width to 100%, the flexible container will distribute the items

  12. 0:43

    evenly across the main axis.

  13. 0:45

    You can now think of the sizing in terms of a ratio where each item takes up one unit

  14. 0:49

    of space.

  15. 0:50

    You can change that ratio by setting the flex property on an individual item.

  16. 0:53

    If we set flex to 2 on the middle item, it now takes up 50% of the available space, because

  17. 0:58

    there's a total of 4 flex units at play.

  18. 1:00

    Now you'll often want to control the way items are spaced or aligned along the main

  19. 1:04

    axis.

  20. 1:05

    Use justify content to space your items evenly, maximize the space between, or push them to

  21. 1:09

    the flex endpoint.

  22. 1:11

    You can also use these same techniques along the cross axis with the align items property,

  23. 1:15

    and that means we can easily align things both horizontally and vertically.

  24. 1:18

    By both aligning and justifying the items to the center, we can easily convert our row

  25. 1:22

    into a column by setting the flex direction to column. The only thing that changed is

  26. 1:27

    that our main axis now runs vertically and the cross axis horizontally. Everything else

  27. 1:31

    works exactly the same. Just tilt your head sideways. This has been CSS Flexbox in 100

  28. 1:36

    seconds. Make sure to hit the like button if you want to see more short videos like

  29. 1:39

    this, thanks for watching, and I will see you in the comments.