Герб Укріїни
Believe in Ukraine and the Armed Forces. Glory to Ukraine!

How to align blocks in a grid by height, make them equal and even

13.05.2023

Method 1 – display: grid

If you want the blocks placed in a grid using the display: grid property to have the same height, you can use the align-items property with the stretch value. For example:

This property stretches the blocks in each row to occupy the entire height of the row. However, if there is content inside the blocks with a fixed height or content that should not be stretched, additional adjustment may be required.
For example, if there are elements inside the blocks with absolute position, the position: relative property should be applied to the blocks to make them the context for the absolutely positioned elements.

If there are elements inside the blocks with a fixed height, the height: 100% property can be applied to make them occupy the entire height of the block.

Sometimes it may also be useful to set a fixed height for the blocks or use grid-auto-rows for the grid.

Method 2 – display: flex

If you use the display: flex property to arrange blocks in a row, to make them the same height, you can use the align-items property with a value of stretch. For example:

 

Method 3 – JS

You can use JavaScript to align block heights. For example, you can write a function that finds the tallest block in a row and sets that height to all blocks in that row. Example:

These are just some of the possible ways to align blocks by height. The choice of method depends on the specific task and requirements for compatibility and performance.

Leave a Reply

Your email address will not be published. Required fields are marked *