HTML Academy
Flex items and the block model
Flexbox, part 22/28
Back to the list of tasks
  • 1. Flex items and the block model
  • 2. Features of the margin property
  • 3. Alignment and margins
  • 4. The flow direction of the main axis and margins
  • 5. The initial main size of a flex item, flex-basis
  • 6. Test: Cubism
  • 7. The item growth factor, flex-grow
  • 8. Growing items proportionally
  • 9. Calculating the total size with flex-grow
  • 10. The item shrinkage factor, flex-shrink
  • 11. Shrinking items proportionally
  • 12. Calculating the total size with flex-shrink
  • 13. flex-shrink and min-width
  • 14. Test: Mastering factors
  • 15. The flex shorthand property
  • 16. Multi-line flex containers and flex-shrink
  • 17. Multi-line flex containers and flex-grow
  • 18. flex-basis: 100% and flex-wrap
  • 19. The heading with a description in flexboxes
  • 20. Heading with description, part 2
  • 21. Flexible menu with wrapping items
  • 22. Flexible menu with wrapping items, part 2
  • 23. Input fields with dynamic width
  • 24. The course card
  • 25. Course card, part 2
  • 26. Course card, part 3
  • 27. So many cards
  • 28. Test: Flexible flows
Alignment and margins
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Features of the margin property

We didn‘t mention the margin property in the previous assignment, because it comes with a number of surprises:

  • Margins do not collapse either horizontally or vertically.

  • Margins do not collapse through the edges of flex containers or flex items.

  • The auto value received the Forbes magazine award in the category “The Most Influential Value for a CSS Property Inside a Flex Container”.

It all has to do with the mechanism that is used to allocate free space. If there is free space inside a flex container, then it is redistributed as follows:

  1. There are items with margins that have been assigned the value auto;

  2. All of the free space along the corresponding axis is allocated to these margins (in other words, a certain margin size is set in pixels).

  3. Along the main axis, free space is shared equally between auto margins. Along the cross axis, auto margins work within each item’s flex line.

  4. It is only once these items are spaced properly that the alignment mechanisms are allowed to act on them.

Therefore, margin: auto; affects the positioning of flex items on both axes, and main-axis auto margins also supersede alignment mechanisms such as justify-content, because alignment occurs when there is free space. Cross-axis auto margins override alignment for the item they belong to. But if all of the free space is absorbed by the automatic margins, then there is nothing to align.

These features can be used to good effect. For example, you can control the arrangement of the items along the main axis using automatic margins. Let’s experiment.

Comments

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Features of the margin property</title> <link href="course.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body class="world"> <div class="spot"> <div class="skate skate-1 racoon-green"></div> <div class="skate skate-2 racoon-brown"></div> <div class="skate skate-3 racoon-orange"></div> <div class="skate skate-4 racoon-gray"></div> </div> </body> </html>
CSS
.spot { display: flex; align-items: center; justify-content: flex-start; } .skate { margin: 0; min-width: 40px; min-height: 100px; } .skate-2 { } .skate-4 { }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

The code has changed, click “Refresh” or turn autorun on.

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Goalscompleted
0
    1. Set margins of 10px on all sides of the .skate skateboards.
    2. Then assign margin-left: auto; to the second skateboard,
    3. and assign margin-right: auto; to the fourth skateboard.
    4. Finally, change justify-content on .spot to flex-end

      and make sure that nothing has changed.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.