Socket
Socket
Sign inDemoInstall

vanilla-match-height

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vanilla-match-height

a vanilla responsive equal heights


Version published
Weekly downloads
148
increased by12.12%
Maintainers
1
Weekly downloads
 
Created
Source

vanilla-match-height.js

Inspireb by: jquery-match-height

matchHeight: makes the height of all selected elements exactly equal

Demo - Features - Install - Usage - Options - Data API
Advanced Usage - Changelog - License

Demo

See the vanilla-match-height.js demo.

jquery.match-height.js screenshot

Modern browsers

In the years since this library was originally developed there have been updates to CSS that can now achieve equal heights in many situations. If you only need to support modern browsers then consider using CSS Flexbox and CSS Grid instead.

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)
  • easily removed when needed
  • data attributes API
  • tested in Edge, Chrome, Firefox

Install

Download vanilla-match-height.js and include the script in your HTML file:

<script src="vanilla-match-height.js" type="text/javascript"></script>

You can also install using the package managers NPM.

npm install vanilla-match-height

Usage

document.body.matchHeight({elements: '.item'});

const containers = document.querySelectorAll(".items-container");
containers.forEach((container) => {			
    container.matchHeight({elements: '.item'});
});

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 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: null,
    attributeName: null
}

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 an optional element/s to excluded
  • attributeName is an optional for use custom attribute

Data API

Use the data attribute data-mh="group-name" or data-match-height="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.

It's possible to use custom data attribute data-same-height="group-name"

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

const containers = document.querySelectorAll(".data-api-items");
containers.forEach((container) => {
    container.matchHeight({attributeName: 'data-same-height'});
});

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
window.dispatchEvent(new Event('resize'));

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:

Custom target element
const containers = document.querySelectorAll(".target-items");
containers.forEach((container) => {			
    container.matchHeight({elements: '.item-0, .item-2, .item-3', target: document.getElementById("target-item-1")});
});

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

Custom property
const containers = document.querySelectorAll(".property-items");
containers.forEach((container) => {			
    container.matchHeight({elements: '.item', property: 'min-height'});
});

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

Where event a event object (e.g. load, resize, orientationchange).

Manually apply match height
document.body.matchHeight({elements: '.item'})._apply();

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

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.

Changelog

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

License

vanilla-match-height.js is licensed under The MIT License (MIT)
Copyright (c) 2023 Simone Miterangelis

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

Keywords

FAQs

Package last updated on 18 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc