HTML Academy
Growing items proportionally
Flexbox, part 29/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
The item shrinkage factor, flex-shrink
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Calculating the total size with flex-grow

For this example, Step 1 is to calculate the free space in the flex container:

Free space = Container width - The total of the basic sizes of the items

Step 2. Calculate the size of the minimum share of free space:

The share of free space = Free space / Sum of flex-grow values for all items

Step 3. The basic size of each flex item is increased by the size of the minimum share of free space multiplied by the value flex-grow of this item:

Total size = Basic size + (Share of free space * flex-grow)

For the upper block with raccoons, we want to assign the factors 1 and 2. However, the required block sizes are achieved with the factors 1 and 3. Let’s calculate:

Free space = 300px - (50px * 2) = 200px
Share of free space = 200px / (1 + 3) = 50px
Total size of the green raccoon = 50px + (50px * 1) = 100px
Total size of the brown raccoon = 50px + (50px * 3) = 200px

But if you set the basic size of the flex items to zero, the free space will occupy the entire width of the flex container, and the greed factors will be different.

It is not worth using flex-basis: 0 and flex-grow to precisely control the relative sizes. It is better to define your basic size using percentages.

Precision. It is not just flex-basis that affects the size of the remaining free space, but also padding, borders, and margins. With flex-basis: 0, min-width does not define the basic size, but it can limit the final width and cause space to be redistributed again.

Comments

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Calculating the total size with flex-grow</title> <link href="course.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body class="world world-flat"> <div class="spot spot-1"> <div class="skate no-width racoon-green"></div> <div class="skate no-width racoon-brown"></div> <div class="ruler ruler-green w-100">100px</div> <div class="ruler ruler-brown w-200 l-100">200px</div> </div> <div class="spot spot-2"> <div class="skate no-width racoon-green"></div> <div class="skate no-width racoon-brown"></div> <div class="ruler ruler-green w-200">200px</div> <div class="ruler ruler-brown w-300 l-200">300px</div> </div> </body> </html>
CSS
.spot { display: flex; padding: 10px 0; height: 100px; } .spot-1 { width: 300px; } .spot-1 .skate { flex-basis: 0; min-width: 50px; } .spot-1 .racoon-green { flex-grow: 0; } .spot-1 .racoon-brown { flex-grow: 0; } .spot-2 { width: 500px; } .spot-2 .skate { flex-basis: 0; min-width: 50px; } .spot-2 .racoon-green { flex-grow: 0; } .spot-2 .racoon-brown { flex-grow: 0; }

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. Assign flex-grow values to the raccoons in the .spot-1 upper clearing so that their widths are equal to the width of the bars.
    2. Do the same thing with the raccoons in the .spot-2 lower clearing.

    In this assignment you can only change the values of flex-grow.

    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.