In this tutorial I will use MatchHeight.js - a jQuery plugin - to dynamically equal the height of HTML elements, providing consistent spacing and improving readability.
Why do we need this?
Let’s say we have a list of movie titles, descriptions and dates shown in a row of columns on a page. Typically, stacking each of these elements would result in the content appearing directly underneath each other, like the image below:
However, that can cause usability issues for the reader.
Sure, reading vertically through each title, description and date is not an issue, but, the reality is that most people will not read the content throughly, they in fact first read horizontally - scanning quickly from left to right, looking for sub headings and critical information following an F-Shaped Pattern, only after finding something of interest do they read vertically.
Improving readability
By simply equalling the height of the titles, and the descriptions we are able to align each of the elements onto a horizontal grid - this additional whitespace improves consistency and now our audience can easily scan through the content.
jQuery is required so make sure that is included first.
Build your page layout, in our case we will use three columns, in a row. Each column contains a .title, .description and .label element. We will use flexbox and calc to create the column layout and make it responsive.
Add some element styling:
Trigger the MatchHeight.js plugin with a script that runs on page load:
How does it work?
This script will look for the all elements with the .match class, find the tallest, and resize all others to equal it. You will see that I added this to class to our .title element.
Now that the MatchHeight plugin has been triggered on the page we can make it resize our .description elements also - we do this using the data-mh="group-name" attribute. This attribute will group all elements with the same ‘group-name’, check for the tallest, and resize the rest to match.
Note that there is no need to apply the .match class on any other elements - just use the data attribute, like I have done in this example:
Now we have a page with 3 columns, each with an equal-height title, and an equal-height description.