HTML Academy
Ordering elements with CSS
Flexbox, part 130/32
Back to the list of tasks
  • 1. The display: flex property, flex item
  • 2. The flex-direction property, main axis
  • 3. The flexbox cross axis
  • 4. The justify-content property, alignment along the main axis
  • 5. The justify-content property: flex-start | flex-end
  • 6. The justify-content property: space-between | space-around
  • 7. The align-items property, alignment along the cross axis
  • 8. The align-items property: flex-start | flex-end
  • 9. The align-items: baseline property
  • 10. The align-self property, or selfish alignment
  • 11. The align-self: baseline property
  • 12. Test: Creating a simple palette
  • 13. The flex-wrap property, overflow of flex items
  • 14. The flex-wrap property, reverse overflow
  • 15. The align-content property, alignment of flex container lines
  • 16. The align-content property: stretch and align-items
  • 17. The align-content property: non-stretch and align-items
  • 18. The align-content property, remaining values
  • 19. The order property, the sequence number of the flex item
  • 20. Test: Creating a more complicated palette
  • 21. Perfect centering by assigning margin: auto to the flex container
  • 22. Perfect centering, flex alignment
  • 23. The adaptive horizontal menu, part 1
  • 24. The adaptive horizontal menu, part 2
  • 25. The adaptive horizontal menu, part 3
  • 26. The vertical row of icons
  • 27. The vertical row of icons, part 2
  • 28. The vertical row of icons, part 3
  • 29. Ordering elements with CSS
  • 30. Vertically aligning blocks using a flexbox
  • 31. Test: Complex palettes
  • 32. Summary of “Flexbox, Part 1”
Test: Complex palettes
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Vertically aligning blocks using a flexbox

Another common task is to create a layout with blocks that are of the same height.

It should be borne in mind that the contents of the blocks may differ and their “natural” heights may vary.

The usual block model has a fundamental drawback: the neighboring blocks are totally unaware of each other, so there is no way to control their heights as a group. It turns out that all of the “standard” options for solving this problem do not work:

  • float or inline-block cannot be used to link together the heights of the neighboring blocks

  • You can use tables and CSS tables to create cells of the same height, but there are limitations on how you can arrange elements in lines, and HTML tables should only be used for tabular data.

  • The minimum height is not suitable, since one of the blocks can always be higher than the others.

  • Of course, you can assign a fixed height to all of the blocks, but this is far from being a universal solution.

Flexbox provides an easy and elegant solution to this problem. After all, the flex items are stretched by default to the entire height of their respective containers.

Try to fix the layout in the float using a flexbox.

Comments

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Blocks with Uniform Height</title> <link href="course.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body class="subtle"> <article class="todos"> <h1>Muffin’s To-Do List</h1> <div class="columns"> <section> <h2>Item 1</h2> <p>Wake up and exercise</p> </section> <section> <h2>Item 2</h2> <p>Take an expedition to the kitchen and feast on fish and caviar from the fridge.</p> </section> <section> <h2>Item 3</h2> <p>Take a break from all of the hard work and nap.</p> </section> <section> <h2>Item 4</h2> <p>Make a mess, bite a person in the heels, and knock a flower pot off the shelf.</p> </section> <section> <h2>Item 5</h2> <p>Steal sour cream from the refrigerator.</p> </section> <section> <h2>Item 6</h2> <p>Determine my relationship with the cat next door.</p> </section> </div> </article> </body> </html>
CSS
.columns { } .columns section { float: left; width: 140px; }

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 a flexbox layout to the .columns block,
    2. and overflow the flex elements onto a new line.

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

    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.