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

profile

'Traffic Captain' Andy Wullmer Braves the High Seas as Spirited Exec

Wullmer networked and hobnobbed, gaining expertise in everything from ecommerce to SEO and traffic, making connections and over time rising through the ranks of several companies to become CEO of the mobile business arm of TrafficPartner.

Alejandro Freixes ·
opinion

To Cloud or Not to Cloud, That Is the Question

Let’s be honest. It just sounds way cooler to say your business is “in the cloud,” right? Buzzwords make everything sound chic and relevant. In fact, someone uninformed might even assume that any hosting that is not in the cloud is inferior. So what’s the truth?

Brad Mitchell ·
opinion

Upcoming Visa Price Changes to Registration, Transaction Fees

Visa is updating its fee structure. Effective April 1, both the card brand’s initial nonrefundable application fee and annual renewal fee will increase from $500 to $950. Visa is also introducing a fee of 10 cents for each settled transaction, and 10 basis points — 0.1% — on the payment volume of certain merchant accounts.

Jonathan Corona ·
opinion

Unpacking the New Digital Services Act

Do you hear the word “regulation” and get nervous? When it comes to the EU’s Digital Services Act (DSA), you shouldn’t worry. If you’re complying with the most up-to-date card brand regulations, you can breathe a sigh of relief.

Cathy Beardsley ·
opinion

The Perils of Relying on ChatGPT for Legal Advice

It surprised me how many people admitted that they had used ChatGPT or similar services either to draft legal documents or to provide legal advice. “Surprised” is probably an understatement of my reaction to learning about this, as “horrified” more accurately describes my emotional response.

Corey D. Silverstein ·
profile

WIA Profile: Holly Randall

If you’re one of the many regular listeners to Holly Randall’s celebrated podcast, you are already familiar with her charming intro spiel: “Hi, I’m Holly Randall and welcome to my podcast, ‘Holly Randall Unfiltered.’ This is the show about sex, the adult industry and the people in it.

Women In Adult ·
trends

What's Hot Now: Leading Content Players on Trending Genres, Monetization Strategies

The juggernaut creator economy hurtles along, fueled by ever-ascendant demand for personality-based authenticity and intimacy — yet any reports of the demise of the traditional paysite are greatly exaggerated.

Alejandro Freixes ·
opinion

An Ethical Approach to Global Tech Staffing

One thing my 24-year career as a technologist working to support the online adult entertainment industry has taught me about is the power of global staffing. Without a doubt, I have achieved significantly more business success as a direct result of hiring abroad.

Brad Mitchell ·
opinion

Finding the Right Payment Partner

Whenever I am talking with businesses that are just getting started, one particular question comes up a lot: “How do I get a merchant account?” It’s a simple question, but it has a complicated answer.

Jonathan Corona ·
opinion

The Taxman Cometh for Every Business

February may be the month of romance, but it is also a time when we need to think about something that inspires very little love: taxes. April is not far away, and the taxman is always waiting. This year, federal and most state income taxes are due Monday, April 15.

Cathy Beardsley ·
Show More