I\'m having a problem creating a flexbox responsive grid and was hoping that someone can point me to the right direction.
I want all the .block
div\'s t
This is not possible with flexbox or CSS, in general.
An initial setting of a flex container is align-items: stretch
. This causes flex items to expand the full length of the cross axis. This is what is known as "flex equal height columns".
Here are a few notes to keep in mind:
Your question:
I want all the
h2
headings of the row to be the same height. Is this possible in some way?
Not with CSS. Because the h2
's exist in different containers, they aren't siblings (they're more like cousins), so equal heights don't apply.
Equal height columns in flexbox apply only to one flex line. Items on other lines, created by wrapping, establish their own equal height line. This means that equal height columns do not work in a multi-line flex container.
The align-self
property can be used on individual flex items to override align-items
, which can break the equal height feature.
By specifying a height on a flex item (e.g. height: 300px
), both align-items
and align-self
are overridden for that item, and the equal height setting is ignored.
This post focuses on a container with flex-direction: row
. If the container is flex-direction: column
, then equal height becomes equal width. Here's a detailed review: Make flex items take content width, not width of parent container
More details:
Duplicate posts:
I worked out a jQuery solution based on your example.
Add the class "eh" (equal heights) to each parent element for which you want to align some of the (sub) children, and a data-eh element containing the selectors for the child elements.
<div class="row eh" data-eh='["h2",".block-list",".bottom"]'>
and then use this function:
$('.eh').each(function(){
var $this = $(this);
var equalHeightSelectors = $this.data('eh');
$.each(equalHeightSelectors, function( index, value ) {
var min_height = 0;
var $children = $this.find(value);
$children.each(function(){
var $el = $(this);
if($el.height() > min_height){
min_height = $el.height();
}
});
$children.height(min_height);
});
});
https://codepen.io/pwkip/pen/oNvxNYZ