An intuitive, class-based interface for creating responsive equalized element heights within ZURB Foundation.
Foundation itself includes the Equalizer component, and there are a few more height-equalization projects in the wild. I’ve found that Equalizer handles basic use-cases well, but breaks down for the responsively updating groupings I needed, and none of the independent projects use the class interface I wanted.
I love Foundation’s simple class name syntax for responsive grid column widths (e.g.
,
, etc.) and I wanted to be able to specify responsive element height groupings in a similar, intuitive way.
To use Responsive Equalized Heights, simply include a reference to this project’s script after Foundation’s script.
<script src="path/to/responsive-equalized-heights.js"></script>
Then add responsive height classes to some elements.
Responsive Equalized Heights is built to work with the grid from Foundation 5. It is untested with earlier versions of Foundation.
If you use Bower or npm:
bower install responsive-equalized-heights
npm install responsive-equalized-heights
Responsive height class names follow this format:
.
| Any of the Foundation screen-size media query names: |
| Any arbitrary string that creates a valid class name. One or two lowercase letters works well: Longer group names also work:
|
Responsive element height classes act just like column width classes with regard to screen size:
medium-height-a
to two elements will equalize their height on medium and larger screens.large-height-a
to another element will equalize the height of all three elements on large screens and above.small-height-b
to two elements will equalize their height for all screen sizes.There are no container classes, no data attributes, and no parent element requirements. Elements can appear anywhere on the page.
Multiple height classes may be used to specify different height groups based on screen size. Using the classes
would match height group a
on medium screens and height group b
on large screens and above.
Elements that are in the same group at the current screen size will share equal height.
Height classes that are not active, such as the class
on a medium screen, are ignored when determining group height.
In the examples below, I’ve added height classes to .panel
elements inside .column
elements. You can add height classes directly on the column divs, or any other element you wish—this pattern of content inside grid containers works best for me.
Just as with Foundation Equalizer, there are two ways to attach callbacks.
// before_height_change callback
Foundation.responsive_heights.before_height_change = function () {
console.log("before_height_change callback");
};
// after_height_change callback
Foundation.responsive_heights.after_height_change = function () {
console.log("after_height_change callback");
};
// before-height-change.fndtn.responsive-heights event
$(document).on('before-height-change.fndtn.responsive-heights', function(){
console.log("before-height-change.fndtn.responsive-heights event");
});
// after-height-change.fndtn.responsive-heights event
$(document).on('after-height-change.fndtn.responsive-heights', function(){
console.log("after-height-change.fndtn.responsive-heights event");
});
Element heights are not set directly on the elements themselves—rather, for each active screen size, a stylesheet is updated with rules for each active group. A throttled window resize handler refreshes the rules as needed.
The JavaScript module extends the global Foundation object with a responsive_heights
object, which contains two methods (update
and remove
) and a reference to the stylesheets for each screen size.
To manually remove all responsive height style rules, use the remove
method, and to manually restore or refresh responsive height style rules, use the update
method.
In JavaScript:
// remove method
Foundation.responsive_heights.remove();
// update method
Foundation.responsive_heights.update();
Try it:
Two elements, always equal in height.
Using the small screen size height classes will set the height group for all screens. These elements will share equal height on all screen sizes from small to xxlarge.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Two elements, equal in height for medium and larger screens.
If no height class applies to the current screen size, elements heights are not affected. These elements stack and shrink to fit their content on small screens, but arrange side-by-side with equalized height on medium and larger screens.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Two elements, equal in height for medium screens only.
The group name auto
is reserved for all screen sizes, and will revert to auto-height layout. Use the format
This example uses the class
to responsively revert to auto-height layout for large screens and above.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
Three elements, two are always equal. On large screens and above, all three are equal.
To facilitate more complex responsive layouts where elements are stacked sometimes and side-by-side at other times, height groups can be configured based on screen size.
The class
is ignored on small and medium screens, but is active on large, xlarge, and xxlarge screens.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Lorem ipsum dolor.
Six elements arranged in responsive columns. Height groups are set to responsively match horizontally arranged elements.
Complex equalized layouts can be created. These six elements rearrange themselves for a different configuration at small, medium, and large screen sizes.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
Lorem ipsum dolor.
Group names can be any string that creates a valid class name.
Remember that auto
is a reserved group name.
Lorem ipsum dolor.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore.