Children of flex items sharing the same height

I'm struggling with creating a flexbox responsive grid and need some guidance.

My goal is to make all the .block divs have equal height, with the .bottom div positioned at the bottom. The current solution achieves this, but when the h2 heading spans multiple lines, I want all headings in the row to be the same height.

Is there a way to accomplish this?

I've created a Codepen demo to illustrate the issue: http://codepen.io/kenvdbroek/pen/eZKdEQ

h1,
h2,
h3 {
  margin: 0;
}
body {
  margin: 0;
  padding: 0;
}
ul.clean-list {
  margin: 0;
  padding: 0;
}
ul.clean-list li {
  list-style: none;
  margin-bottom: 5px;
}
li:last-child {
  margin-bottom: 0;
}
.container {
  padding-top: 50px;
}
.block {
  margin-bottom: 30px;
  border: 1px solid red;
}
.block > .bottom {
  border: 1px solid blue;
}
@media only screen and (min-width: 480px) {
  ...
<div class="container">
  ...
...
</div>

Answer №1

Unfortunately, achieving equal height columns with flexbox or CSS in general is not possible.

When a flex container has the initial setting of align-items: stretch, the flex items will expand to fill the entire length of the cross axis. This concept is commonly referred to as "flex equal height columns."

Here are some key points to consider:

  • Equal height columns only work for the children of a flex container; meaning they must have the same parent in order for the equal height property to take effect.

Your inquiry:

I would like all the h2 headings within a row to be the same height. Is there any way to achieve this?

Unfortunately, this cannot be accomplished using just CSS. Since the h2 elements exist in different containers, they are not siblings but more like cousins, which prevents the equal height property from being applied.

  • Equal height columns in flexbox only apply to one flex line. Items on different lines created by wrapping form their own equal height lines, making equal height columns actually ineffective in a multi-line flex container.

  • The align-self property can be used on individual flex items to override the default behavior set by

    align-items</code, potentially breaking the equal height functionality.</p>
    </li>
    <li><p>If a height value is specifically defined for a flex item (e.g., <code>height: 300px
    ), both align-items and align-self properties are overridden for that particular item, disregarding the equal height setting.

  • This discussion primarily focuses on a container with a flex-direction: row. In cases where the container's direction is flex-direction: column, the concept of equal height transitions to equal width. For additional insights, refer to this thorough analysis: Ensuring flex items accommodate content width, rather than the width of the parent container

For further information:

  • Implementing equal height rows in a flex container
  • Disabling equal height columns in Flexbox

Similar discussions:

  • Establishing uniform header heights among cards or similar elements using flexbox?
  • CSS - Achieving consistent heights for children within separate parent elements?
  • Positioning nested grid items within a higher-level container (utilizing subgrid functionalities)
  • Aligning child components within distinct sections
  • Solely utilizing CSS to establish multiple rows with uniform height within a responsive grid layout
  • Aligning child elements sourced from various parent containers
  • Utilizing flexbox display to harmonize items within a row wrapper

Answer №2

I came up with a jQuery solution inspired by your sample.

To align specific child elements within parent elements, simply assign the class "eh" (equal heights) to each parent element and include a data-eh attribute with the selectors for the child elements.

<div class="row eh" data-eh='["h2",".block-list",".bottom"]'>

Then, utilize 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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Height setting for flex column layout

.flex-body { display: flex; flex-wrap: wrap; } .flex-body div{ width: 50%; align-items: flex-start; } <div class="flex-body"> <div style="background: #0980cc; height:100px;"></div> <div style="background: #09cc69;height: 20 ...

When applying the OWASP ESAPI encodeForHTMLAttribute method, I noticed that symbols are being rendered as their corresponding HTML entity numbers instead of the actual symbols

I recently started exploring OWASP ESAPI for preventing XSS and integrating the JavaScript version into my application. As per Rule #2 in the XSS prevention cheat sheet, it is recommended to "Attribute Escape" before inserting untrusted data into attribut ...

I am selecting specific items from a list to display only 4 on my webpage

I am trying to display items from a list but I only want to show 4 out of the 5 available items. Additionally, whenever a new item is added, I want it to appear first on the list, with the older items following behind while excluding the fifth item. Despi ...

Angular application's HTML Canvas unexpectedly changes to a black color when I begin drawing

Currently, I am developing an Angular application in which users can draw text fields on PDF pages. To achieve this functionality, I have integrated the ng2-pdf-viewer library and created a PDF page component that incorporates an HTML canvas element. Howe ...

Are font classifications determined by the operating system of the viewer or the web browser of the viewer

One thing I've observed is that Google Fonts never uses "fantasy" as a fallback class in the font-family. Instead, they utilize "cursive" for all script and typical fantasy fonts. Could this be a subtle indication that Chrome does not support "fantas ...

A lateral sliding motion

Here is the HTML code I am working with: <div class="menuTabs"> <div class="mtabArrowLeft">Left</div> <input class="menutabBTN" name="" type="button" value="a" /> <input class="menutabBTN" name="" type="b ...

When attempting to specify the path in the angular.json file, Angular encounters difficulty accessing Bootstrap from the node_modules directory

I have been attempting to integrate Bootstrap into my Angular project. Firstly, I used npm install --save bootstrap to add Bootstrap to my project. Following that, I installed jQuery as well. I then specified the path for Bootstrap. Displayed below is an ...

The image in the HTML is currently positioned at the bottom, but I would like it to be

this is what it looks like: https://i.sstatic.net/VLqN7.png I need it to be at the top next to the first card. <!-- top nav--> <nav class="navbar navbar-expand-lg navbar-light" style="background-color:white"> <a class="navbar-brand" ...

The input placeholder text in Safari 5.1.7 is unable to be centered

Check out my code snippet below: HTML: <input type="text" name="Surname" placeholder="Surname" id="surname"> CSS: ::i-block-chroms, input[placeholder]::-webkit-input-placeholder { text-align:center; } ::-webkit-input-placeholder { t ...

Alternative form for non-javascript browsers in the absence of script

I'm currently working on a landing page for an upcoming product, and I've run into a bit of a snag. <form novalidate method="POST" class="subscribe-form"> <noscript> </form> <form method="POST" action="/ ...

Unable to place an absolutely positioned Div within relative parent containers

After applying the id "enterGame" with position: absolute, I noticed that the div disappears. Even when I tried adding position: relative to the body or html tag, the div remained invisible. The relevant code snippet is as follows: html { height: 10 ...

Is there a way to enlarge an iFrame to fill the entire screen with just a button click, without using JavaScript

I am currently attempting to achieve full screen mode for an iFrame upon clicking a button, similar to the functionality on this site. On that website, there is a bar that expands to full screen along with the embedded game, which is what I am aiming for. ...

Retrieve the information from a website and display it on the current webpage using an ajax request

Is there a way to insert parsed HTML content into my webpage using just a link from another page? I'm attempting to use an AJAX call, but I keep receiving an error. Below is the code I've written, and the browser doesn't seem to be the issue ...

How can we briefly make the list item color and size return to default after clicking the Bootstrap navbar-toggler button?

.navbar .collapse .navbar-nav .nav-item .nav-link { font-family: 'yekan'; color: #fff; opacity: .8; font-size: .9rem; padding-left: 15px; padding-right: 15px } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery. ...

Learn the process of utilizing Uglify.js to combine JavaScript files for individual views within Express.js

My website is currently built on express.js and I am looking to optimize my JavaScript files using uglify.js in the build process. An example of a typical view on my site appears as follows: extend layout block head script(src="/js/polyfills/html5s ...

The CSS is acting unexpectedly by applying styles to elements that were not intended to be styled

I am trying to style 2 specific elements within a div that has the class promotion, setting their text color to white. .promotion h1, p { color: #FFF; } <section> <article class="promotion grid-col-span-3 text-center"> <h ...

Enhanced Vertical Space Produced on my Website in Firefox

For some reason, Firefox is adding an extra 250px at the end of my website below the footer. I have not been able to determine why this is occurring. Note: The site functions properly on all other browsers; the issue is specific to Firefox. You can view ...

What causes the bootstrap grid system to deviate from the intended column sizes?

Here is the alignment I am aiming for with the Phone and Extension fields in the form: https://i.sstatic.net/JRmDU.png This is the code snippet I have written for the Phone and Extension row: <div class="row form-group"> ...

Discovering specific keywords within HTML tags and performing replacements using jQuery

I am searching for specific strings within my HTML code, and if I find them, I want to replace them with nothing. For example: HTML: <img src=`javascript:alert("hello ,world!")`> My goal is to locate keywords like javascript, alert, etc, within H ...

Achieving Left and Right Alignment of Navigation Elements using Flex Basis in Chakra UI with Next JS

For my project, I am working on creating a straightforward Navigation bar using Chakra UI and Next JS. The goal is to have an svg logo positioned at the top-left, while the menu and UI buttons are aligned to the top-right. However, I'm facing challeng ...