Dev Depot: MatchHeight, Make the Height of all Selected Elements Equal

It seems like a simple enough task, but it is one that has long frustrated web designers: creating a matrix of same sized elements, each (perhaps) containing a different amount of content, so that this array appears “even” and symmetrical.

With a goal of making a more robust equal heights plugin for jQuery that would easily make the height of all selected elements exactly even, developer Liam Brummitt set out to create matchHeight (www.brm.io/jquery-match-height/)

The matchHeight plugin’s features include being row aware, in order to handle floating elements, plus full responsiveness, so that it automatically updates on window resizing.

“I needed a more robust version of the common equal heights plugin (that everyone and their grandma has made before),” Brummitt explains. “So matchHeight improves on these by adding features and handling the edge cases where the others tend to fail in practice.”

The matchHeight plugin’s features include being row aware, in order to handle floating elements, plus full responsiveness, so that it automatically updates on window resizing. Additionally, matchHeight handles mixed padding, margin and border values (even if every element has different values).

Box-sizing and the handling of images and other media, which are updated automatically after the element loads, along with an API for handling data attributes, rounds out the plugin’s initial feature set.

Although the current version is an early v0.5.0., matchHeight has been fully tested in IE8+, Chrome, Firefox and Android it and it works well, but as with all new technologies, should be tested in your own developmental environment.

“Why not use CSS?” Brummitt asks rhetorically, explaining that “Making robust, responsive equal height columns for arbitrary content is difficult or impossible to do with CSS alone (at least without hacks or trickery, in a backwards compatible way).”

JavaScript based solutions are not without their problems however, so developers are advised to ensure that their layout remains usable even if JavaScript is disabled on the viewer’s browser.

Setting up matchHeight is simple, and first requires placing a standard jQuery call, along with the matchHeight script, either jquery.matchHeight.js or jquery.matchHeight-min.js, which are included in the page’s HTML file, like so:

<script src=”jquery.matchHeight.js”></script>.

Using matchHeight then requires a call in the DOM ready event: $(elements).matchHeight(byRow); — where the byRow option is a boolean switch that enables or disables automatic row detection, with the default setting being “true.”

A working example of the script is provided to help explain its functionality and implementation.

In its practical application, matchHeight allows users to employ the data attribute datamatchheight=”group-name” (or data-mh shorthand), where the group-name is an arbitrary string that denotes those elements that should be considered as a group.

The publisher notes that all elements that use the same group name will be set to the same height when the page is loaded, regardless of their position in the DOM, without any extra code required and with byRow optionally enabled when using the data API.

The matchHeight script offers several advanced options, internal properties and functions, such as $.fn.matchHeight._groups which calls an array containing all element groups that have had matchHeight applied.

The matchHeight plugin is available under The MIT License, with the source code over at GitHub.

Related:  

Copyright © 2024 Adnet Media. All Rights Reserved. XBIZ is a trademark of Adnet Media.
Reproduction in whole or in part in any form or medium without express written permission is prohibited.

More Articles

opinion

Goodbye to Noncompete Agreements in the US?

A noncompetition agreement, also known as a noncompete clause or covenant not to compete, is a contract between an employer and an employee, or between two companies.

Corey D. Silverstein ·
opinion

The Search for Perfection in Your Payments Page

There has been a lot of talk about changes to cross sales and checkout pages. You have likely noticed that acquirers are now actively pushing back on allowing merchants to offer a negative option, upsell or any cross sales on payment pages.

Cathy Beardsley ·
opinion

Unpacking the Payment Card Industry's Latest Data Security Standard

The Payment Card Industry Data Security Standard (PCI DSS) is a set of requirements and guidelines that apply to all businesses that accept credit card payments, and is designed to ensure the security of those transactions.

Jonathan Corona ·
opinion

Compliance With State Age Verification Laws

During the past year, website operators have faced a slew of new state age verification laws entailing a variety of inconsistent compliance obligations.

Lawrence Walters ·
opinion

Merchants in Spotlight With Visa's VIRP

By now, most merchants know about the Visa Integrity Risk Program (VIRP) rolled out in spring 2023. The program is designed to ensure that acquirers and their designated agents — payment facilitators, independent sales organizations and wallets — maintain proper controls and oversight to prevent illegal transactions from entering the Visa payment system.

Cathy Beardsley ·
opinion

How to Know When Hosting Upgrades Are Really Needed

I was reminded about an annoyingly common experience that often frustrates website owners: upgrades. Sometimes, an upgrade of physical system resources like CPU, RAM or storage really is required to solve a problem or improve performance… but how do you know you’re not just being upsold?

Brad Mitchell ·
profile

WIA Profile: Natasha Inamorata

Natasha Inamorata was just a kid when she first picked up a disposable camera. She quickly became enamored with it and continued to shoot with whatever equipment she could afford. In her teens, she saved enough money to purchase a digital Canon ELPH, began taking portraits of her friends, shot an entire wedding on a point-and-shoot camera and edited the photos with Picnik.

Women in Adult ·
trends

Collab Nation: Top Creators Share Best Practices for Fruitful Co-Shoots

One of the fastest ways for creators to gain new subscribers and buyers, not to mention monetize their existing fan base, is to collaborate with other creators. The extra star power can multiply potential earnings, broaden brand reach and boost a creator’s reputation in the community.

Alejandro Freixes ·
opinion

Bridging Generational Divides in Payment Preferences

While Baby Boomers and Gen Xers tend to be most comfortable with the traditional payment methods to which they are accustomed, like cash and credit cards, the younger cohorts — Millennials and Gen Z — have veered sharply toward digital-first payment solutions.

Jonathan Corona ·
opinion

Legal and Business Safety for Creators at Trade Shows

As I write this, I am preparing to attend XBIZ Miami, which reminds me of attending my first trade show 20 years ago. Since then, I have met thousands of people from all over the world who were doing business — or seeking to do business — in the adult industry.

Corey D. Silverstein ·
Show More