Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jquery-match-height

liabru64.8kMIT0.7.2TypeScript support: definitely-typed

a responsive equal heights plugin for jQuery

matchHeight, equal, match, height, equalize, columns

readme

jquery.matchHeight.js

matchHeight makes the height of all selected elements exactly equal.
It handles many edge cases that cause similar plugins to fail.

brm.io/jquery-match-height

Demo - Features - Gallery - Install - Usage - Options - Data API
Advanced Usage - Tests - Known limitations - Changelog - License

Demo

See the jquery.matchHeight.js demo.

jquery.matchHeight.js screenshot

Features

  • match the heights for groups of elements automatically
  • use the maximum height or define a specific target element
  • anywhere on the page and anywhere in the DOM
  • responsive (updates on window resize)
  • row aware (handles floating elements and wrapping)
  • accounts for box-sizing and mixed padding, margin, border values
  • handles images and other media (updates after loading)
  • supports hidden or none-visible elements (e.g. those inside tab controls)
  • throttled to balance performance and smoothness
  • easily removed when needed
  • maintain scroll position
  • data attributes API
  • callback events
  • unit tests
  • module loader support
  • tested in IE8+, Chrome, Firefox, Safari, Android, iOS

See how others are using jquery.matchHeight.js

Install

jQuery is required, so include it first. Download jquery.matchHeight.js and include the script in your HTML file:

<script src="jquery.matchHeight.js" type="text/javascript"></script>

You can also install using the package managers Bower and NPM.

bower install matchheight
npm install jquery-match-height

Usage

$(function() {
    $('.item').matchHeight(options);
});

Where options is an optional parameter.
See below for a description of the available options and defaults.

The above example will set all selected elements with the class item to the height of the tallest.
If the items are on multiple rows, the items of each row will be set to the tallest of that row (see byRow option).

Call this on the DOM ready event (the plugin will automatically update on window load).
See the included test.html for many working examples.

Also see the Data API below for a simple, alternative inline usage.

Options

The default options are:

{
    byRow: true,
    property: 'height',
    target: null,
    remove: false
}

Where:

  • byRow is true or false to enable row detection
  • property is the CSS property name to set (e.g. 'height' or 'min-height')
  • target is an optional element to use instead of the element with maximum height
  • remove is true or false to remove previous bindings instead of applying new ones

Data API

Use the data attribute data-mh="group-name" where group-name is an arbitrary string to identify which elements should be considered as a group.

<div data-mh="my-group">My text</div>
<div data-mh="my-group">Some other text</div>
<div data-mh="my-other-group">Even more text</div>
<div data-mh="my-other-group">The last bit of text</div>

All elements with 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.

Note that byRow will be enabled when using the data API, if you don't want this (or require other options) then use the alternative method above.

Advanced Usage

There are some additional functions and properties you should know about:

Manually trigger an update

$.fn.matchHeight._update()

If you need to manually trigger an update of all currently set groups, for example if you've modified some content.

Row detection

You can toggle row detection by setting the byRow option, which defaults to true.
It's also possible to use the row detection function at any time:

$.fn.matchHeight._rows($('.item'));

Which will return an array of element selections for each row, see this thread for more information and an example.

Remove bindings

$('.item').matchHeight({ remove: true });

This will remove all bindings for the selected elements, from all groups.

Custom target element

$(function() {
    $('.item').matchHeight({
        target: $('.sidebar')
    });
});

Will set all selected elements to the height of the first item with class sidebar.

Custom property

$('.item').matchHeight({ property: 'min-height' });

This will set the min-height property instead of the height property.

Callback events

Since matchHeight automatically handles updating the layout after certain window events, you can supply functions as global callbacks if you need to be notified:

$.fn.matchHeight._beforeUpdate = function(event, groups) {
    // do something before any updates are applied
}

$.fn.matchHeight._afterUpdate = function(event, groups) {
    // do something after all updates are applied
}

Where event a jQuery event object (e.g. load, resize, orientationchange) and groups is a reference to $.fn.matchHeight._groups (see below).

Manually apply match height

$.fn.matchHeight._apply(elements, options)

Use the apply function directly if you wish to avoid the automatic update functionality.

Throttling resize updates

$.fn.matchHeight._throttle = 80;

By default, the _update method is throttled to execute at a maximum rate of once every 80ms. Decreasing the above _throttle property will update your layout quicker, appearing smoother during resize, at the expense of performance. If you experience lagging or freezing during resize, you should increase the _throttle property.

Maintain scroll position

$.fn.matchHeight._maintainScroll = true;

Under certain conditions where the size of the page is dynamically changing, such as during resize or when adding new elements, browser bugs cause the page scroll position to change unexpectedly.

If you are observing this behaviour, use the above line to automatically attempt to force scroll position to be maintained (approximately). This is a global setting and by default it is false.

Accessing current group bindings

$.fn.matchHeight._groups

The array that contains all element groups that have had matchHeight applied. Used internally for automatically updating on resize events, but you may modify this array if you need to manually access any groups (e.g. if you're deleting elements).

Tests

Open test/page/test.html in your browser to run unit tests via the jasmine test runner.

If you wish to contribute functionality to this project, you are encouraged to add new tests following the same conventions.

Run gulp test to run unit tests on multiple browsers and multiple resolutions, automatically through selenium.

Run gulp test:cloud to test on even more browsers via a cloud service (you will need to create a file called test/conf/private.conf.js with your cloud credentials that looks like this:

exports.config = {
    user: 'username',
    key: 'key'
};

Cloud browser testing for this project is provided by BrowserStack (which is free for open source).

Known limitations

CSS transitions and animations are not supported

You should ensure that there are no transitions or other animations that will delay the height changes of the elements you are matching, including any transition: all rules. Otherwise the plugin will produce unexpected results, as animations can't be accounted for.

Delayed webfonts may cause incorrect height

Some browsers do not wait for webfonts to load before firing the window load event, so if the font loads too slowly the plugin may produce unexpected results.

If this is a problem, you should call _update once your font has loaded by using something like the webfontloader script.

Content changes require a manual update

If you change the content inside an element that has had matchHeight applied, then you must manually call $.fn.matchHeight._update() afterwards. This will update of all currently set equal heights groups.

Also note that previous matchHeight bindings do not apply to new elements, even if they match the selector used. In this case you must remove the old bindings and add new ones, see this comment.

Changelog

To see what's new or changed in the latest version, see the changelog

License

jquery.matchHeight.js is licensed under The MIT License (MIT)
Copyright (c) 2014 Liam Brummitt

This license is also supplied with the release and source code.
As stated in the license, absolutely no warranty is provided.

Why not use CSS?

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).

Note you should probably ensure your layout is still usable if JavaScript is disabled.

changelog

0.7.2 (2017-02-19)

0.7.1 (2017-02-19)

  • change $.bind to $.on where supported, closes #120 and #145 (e468ae9), closes #120 #145
  • deleted redundant min file (moved to dist) (f530833)
  • fix release tasks (1462c36)
  • Merge pull request #115 from robeerob/patch-1 (ab31d58)
  • Update README.md with raw downloadable install url (dcc9ad6)

0.7.0 (2016-01-04)

release summary

  • added build tasks
  • added selenium unit tests
  • added lint tests
  • added matchHeight.version property
  • added to npm

  • fixed unitless properties

  • fixed inline styles being removed
  • fixed display: flex issue
  • fixed display: inline-flex issue
  • fixed row detection when items contain floating elements
  • fixed compatibility for module loaders

commit log

  • add custom version argument to gulp build (ad8aac5)
  • add delay to jasmine boot (30824fb)
  • add lint to all test tasks (6b16f67)
  • add test for _parse on string values with units (4a64208)
  • add to npm (0055660)
  • added a section on tests to readme (e0be682)
  • added changelog task (5263ab1)
  • added cloud selenium, local emulated ie testing, lint task, build task, release task, improved tests (06bd876)
  • added gulpfile, jasmine test specs, browser test runner, selenium test runner (ca926de)
  • added libscore (03a4317)
  • added matchHeight.version property (431e4d0)
  • added release tasks (49cc72f)
  • added test for property option (7bdada7)
  • added test for remove (445799d)
  • added tests for custom toBeWithinTolerance matcher (a89b1c2)
  • bump jquery package version (cc9c416)
  • change tests to use jquery type checking functions (6cf52f0)
  • faster selenium testing (a6b2da3)
  • fix bower instructions in readme (91e50ad)
  • fix for display: inline-flex, closes #68 (e769b9f), closes #68
  • fix for unitless properties by forcing px, closes #64 (d8cc365), closes #64
  • fix issue maintaining inline styles, closes #95 (878ff96), closes #95
  • fix issue with 'display:flex', closes #77 (dc53a49), closes #77
  • fix issues with build script (1195421)
  • fix linter issues (0165e74)
  • Fix package manager registries URLs (036df1b)
  • fixed local test config for non-windows (d67ca25)
  • fixed missing dependencies (c608b80)
  • handle error when test server is already running (9e6487d)
  • ignore linebreak style on lint (1510b58)
  • Improve row detection when cells contain floating contents (8844acb)
  • improved readme (1cf2c27)
  • improved tasks (61a9ed4)
  • improved tests (b1cadb5)
  • Make plugin compatible with module loaders (b5988c1)
  • Merge branch 'feature/tests' into develop (a7d35dc)
  • Merge branch 'floatingcontents' of https://github.com/jorrit/jquery-match-height into jorrit-floatin (89b74a7)
  • Merge branch 'jorrit-floatingcontents' (dc9716b)
  • Merge pull request #81 from afelicioni/patch-1 (c5566da)
  • Merge pull request #82 from JulienMelissas/patch-1 (63d8ca4)
  • remove ie testing meta tags (44ed2fe)
  • replace browserstack tunnel with ngrok (2c67ca0)
  • run webdriver spec for all breakpoints (3440598)
  • update master build (df2e0c2)
  • update master build (f4b4b98)
  • updated min file (99648ca)
  • use a spy for callback tests (a72a2cf)
  • use gutil.log (00a91bc)
  • use local test images (02398d9)
  • Use unminified version in Bower's "main" argument (eedca73)

0.6.0 (2015-03-31)

release summary

  • added options parameter
  • added property option
  • added target option
  • added callback events
  • added maintain scroll
  • added inline-block support
  • added hidden elements support
  • improved performance and throttling
  • improved demo

  • fixed declaration order issue when using requirejs

  • fixed issues for people using build concatenation
  • fixed data api issue with missing data-mh
  • fixed IE8 border calculation
  • fixed Safari row detection
  • fixed inline style preservation

commit log

  • Fix usage of data-mh attribute (816850d)
  • Improve support when concatenated or minified (09c4b1a)
  • Merge branch 'kwoodfriend-patch-1' (dde46f9)
  • Merge branch 'nyordanov-master' (dc77dbe)
  • Merge branch 'patch-1' of https://github.com/kwoodfriend/jquery-match-height into kwoodfriend-patch- (e009c4c)
  • Merge branch 'stefanozoffoli-patch-1' (c0104c4)
  • Preserve inline styles when using byRow (72ba5cf)
  • added display property tests (5dafa0c)
  • added gitignore (d76b02c)
  • added local jquery (9239f4e)
  • added maintainScroll functionality, closes #18 (ee83317), closes #18
  • added support for hidden elements, closes #12 (9a8944b), closes #12
  • added support for options, added property option for min-height (94c9d28)
  • added update callback events (0b31e21)
  • avoid call to .is when no target specified (db9996d)
  • changed master build description (6dcc13d)
  • early out on options parser (b4326d3)
  • fix for single item rows, closes #48 (64b9a54), closes #48
  • fix handling of hidden elements by row, closes #28 (71a5151), closes #28
  • fix row detection on safari (windows) (b52448a)
  • fix to preserve inline styles (e9de702)
  • fix typo in target option, closes #63 (290dfcf), closes #63
  • fixed IE8 border reset issue, closes #10 (246820d), closes #10
  • fixed support for inline-block (b3df801)
  • fixed throttling issue (fdc8f7a)
  • implemented target option (a01fb70)
  • improved readme (9ba9529)
  • preserve inline styles on hidden parents, closes #46 (4917d6c), closes #46
  • refactored plugin definition (467d928)
  • release 0.6.0 (aef80df)
  • removed redundant css setter (6c7e6ad)
  • reorganised source, closes #27 (cae21cd), closes #27
  • skip apply to rows with only one item (f72ab91)
  • updated min file (56214a1)
  • updated min file (9aa96f1)
  • updated min file (b6f612a)
  • updated min file (128c363)
  • updated readme (667e516)
  • updated readme (a30551f)
  • updated readme with known limitations (57ee64a)
  • updating minified version (ab3963f)

0.5.2 (2014-06-10)

release summary

  • improved demo
  • added matchHeight('remove')
  • added update throttling
  • removed forced display:block after application

commit log

  • added matchHeight('remove') (8f5f13f)
  • added updated throttling (6d9a6a7)
  • prettier demo (f7ea426)
  • release 0.5.2 (4b8f8e4)
  • removed forced display:block after application (a3a058c)
  • updated changelog (ecee5f9)
  • updated readme, changelog, build (ae0a825)

0.5.1 (2014-04-15)

release summary

  • fixed IE8 NaN bug when parsing 'auto' properties
  • fixed IE8 window resize event loop bug
  • fixed compatibility with older jQuery versions
  • added bower package file
  • added jquery package file

commit log

  • Making the library compatible with old jQuery versions < 1.7 (4c3f945)
  • Making the library compatible with old jQuery versions < 1.7 (7d467aa)
  • Merge pull request #3 from dcorb/master (18a6fa1)
  • added CHANGELOG (b1ed72d)
  • added bower package (56c9902)
  • added minified version (44c4554)
  • fixed IE8 NaN bug when parsing 'auto' properties (702eea6)
  • fixed IE8 window resize event loop bug (22b74da)
  • increment version (0cb6082)
  • updated minified build (3873f7d)
  • updated readme (b62297b)

0.5.0 (2014-03-02)

release summary

  • initial release

commit log