Children of unrelated parents may have similar height levels

Is there a way to make two unrelated divs have the same height using jQuery? Most solutions I found are for children of the same parent, but in this case, I need to equalize the heights of Parent B's child to Parent A's child. The code snippet below attempts to achieve this:

https://i.sstatic.net/zIzIT.png

I tried using a CSS grid but it did not work for this specific scenario.

Update: Both containers (Parent A and Parent B) are separate and independent from each other:

.a, .b {border:2px solid; padding: 25px; float:left; width: 200px;}
.a div, .b div {border:2px solid red;}
<div class="a">Parent A
<div>Child of A <br> some additional text</div>
</div>

<div class="b">Parent B
<div>Child of B</div>
</div>

The JavaScript code snippet provided aims to adjust the height of columns within their respective containers to ensure equal heights:

$(document).ready(function(){
    $('.containers').each(function(){  
      var min_highestBox = 0;
      $('.columns', this).each(function(){
        if($(this).height() > min_highestBox) {
          min_highestBox = $(this).height(); 
        }
      });               
      $('.columns',this).height(min_highestBox);
    }); 
});
.containers {border:1px solid; width:100%; display:inline-block;}
.columns {border:1px solid red; padding: 20px; min-width:20%; float:left;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="containers">
    <div class="columns">This is<br />the highest<br />column</div>
    <div class="columns">One line</div>
    <div class="columns">Four<br />lines<br />the highest<br />column</div>
</div>
<div class="containers">
    <div class="columns">One line</div>
    <div class="columns">Two<br>lines</div>
    <div class="columns">One line</div>
</div>

Answer №1

If it's not possible to place them inside a wrapper, you can utilize sorting from lowest to highest and assign the highest value to all affected nodes along with using a MutationObserver to easily monitor changes on these nodes and trigger the height-adjustment routine again.

For ensuring equal parent heights:

$(document).ready(() => {
  const selector = ".containers";
  function resize() {
    const nodes = $(selector)
          .css("height", "auto")
          .sort((a, b) => $(a).height() - $(b).height()),
      heighest = nodes.last().height();
    if (nodes.first().height() != heighest)
      $(selector).css(`height`, `${heighest}px`);
  }

  resize();

  const observer = new MutationObserver(resize);
  $(selector).each((idx, el) => observer.observe(el, { childList: true, subtree: true }));


  // Examples:
  setTimeout(() => {
    $(".columns").first().append("<br>Lorem<br>Ipsum<br>Dolor<br>sit<br>Amet");
  }, 1500);
  setTimeout(() => {
    $(".columns").last().append("<br>Lorem<br>Ipsum<br>Dolor<br>sit<br>Amet");
  }, 3000);
  setTimeout(() => {
    $(".columns").last().append("<br>Lorem<br>Ipsum<br>Dolor<br>sit<br>Amet");
  }, 5000);
})
.containers {
  border: 1px solid;
  width: 100%;
  display: inline-block;
}

.columns {
  border: 1px solid red;
  padding: 20px;
  min-width: 20%;
  float: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="containers">
  <div class="columns">This is<br>the highest<br>column</div>
  <div class="columns">One line</div>
  <div class="columns">Four<br>lines<br>the highest<br>column</div>
</div>
<div class="containers">
  <div class="columns">One line</div>
  <div class="columns">Two<br>lines</div>
  <div class="columns">One line</div>
</div>

For ensuring equal child heights:

$(document).ready(() => {
  const selector = ".columns";
  function resize() {
    const nodes = $(selector)
          .css("height", "auto")
          .sort((a, b) => $(a).height() - $(b).height()),
      heighest = nodes.last().height();
    if (nodes.first().height() != heighest)
      $(selector).css(`height`, `${heighest}px`);
  }

  resize();

  const observer = new MutationObserver(resize);
  $(selector).each((idx, el) => observer.observe(el, { childList: true, subtree: true }));


  // Examples:
  setTimeout(() => {
    $(".columns").first().append("<br>Lorem<br>Ipsum<br>Dolor<br>sit<br>Amet");
  }, 1500);
  setTimeout(() => {
    $(".columns").last().append("<br>Lorem<br>Ipsum<br>Dolor<br>sit<br>Amet");
  }, 3000);
  setTimeout(() => {
    $(".columns").last().append("<br>Lorem<br>Ipsum<br>Dolor<br>sit<br>Amet");
  }, 5000);
})
.containers {
  border: 1px solid;
  width: 100%;
  display: inline-block;
}

.columns {
  border: 1px solid red;
  padding: 20px;
  min-width: 20%;
  float: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="containers">
  <div class="columns">This is<br>the highest<br>column</div>
  <div class="columns">One line</div>
  <div class="columns">Four<br>lines<br>the highest<br>column</div>
</div>
<div class="containers">
  <div class="columns">One line</div>
  <div class="columns">Two<br>lines</div>
  <div class="columns">One line</div>
</div>

Answer №2

If you opt for an additional wrapper, achieving this with CSS alone is possible

.wrapper {
  display: grid;
  gap: 5px;
  grid-auto-rows: 1fr; /* equal containers */
}
.containers {
  border: 1px solid;
  display: flex; /* equal children */
}

.columns {
  border: 1px solid red;
  padding: 20px;
  min-width: 20%;
}
<div class="wrapper">
  <div class="containers">
    <div class="columns">This is<br />the highest<br />column</div>
    <div class="columns">One line</div>
    <div class="columns">Four<br />lines<br />the highest<br />column</div>
  </div>
  <div class="containers">
    <div class="columns">One line</div>
    <div class="columns">Two<br>lines</div>
    <div class="columns">One line</div>
  </div>
</div>

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

The initial ajax request returned a status of 0, indicating an error, while the subsequent ajax call received a status of 4, with a successful response code

I encountered a major issue. Initially: Ajax call is being made by a cordova app on an Android device to a Windows Server (IIS, PHP) protected by basic authentication and SSL. After our recent update to the GlobalSign nv-sa certificate (a few days ago), ...

Issues with displaying search form div using jQuery AJAX

Currently, I am in the process of developing a search form that includes a dropdown menu. The functionality involves selecting an item from the dropdown menu triggering an AJAX call to another PHP file. This call returns tags which resize both the dropdown ...

Encountering Issues with Implementing Bootstrap Select for Multiple Selections

I have implemented BootStrap-Select for multi-selection functionality. When selecting multiple options from a list of 10, I noticed that if I select a 4th option, another selected option gets randomly unselected. To address this issue, I am using the follo ...

Updating the style of different input elements using Angular's dynamic CSS properties

I am seeking guidance on the proper method for achieving a specific functionality. I have a set of buttons, and I would like the opacity of a button to increase when it is pressed. Here is the approach I have taken so far, but I have doubts about its eff ...

Issue with jQuery scrolling functionality

Can someone explain why the site is loading in this specific part of the page? My guess is that it has something to do with jQuery. You can view the live website here: Instead of loading in the home section, the site is loading in the portfolio section. I ...

directive in Angular ordering

After exploring this link, my understanding deepened: http://plnkr.co/edit/k5fHMU?p=preview Upon analyzing the code snippet provided in the link above, I noticed that the Angular app consists of both a controller and a directive. However, there seems to ...

Steps for triggering an event based on a specific return value

Is there a way to utilize the response value for triggering a specific action? For example, can I do something like this: $( "#return" ).on( "text=XXX", function() { Below is my ajax post request: $.ajax({ type: "POST", url: "post.php", ...

What is the best way to showcase the final div at the top with CSS?

I'm working with 4 separate divs, with the last one acting as a footer. Is there a way to position this last div at the top using CSS? ...

IBM Watson Conversation and Angular Integration

After recently diving into Angular, I am eager to incorporate a Watson conversation bot into my angular module. Unfortunately, I'm facing an issue with including a library in Angular. To retrieve the Watson answer, I am utilizing botkit-middleware-wat ...

How to retrieve data from a form object sent via cloud function using App Script

Currently, I am in the process of developing a web application that can extract files from a web form and store them in a designated Google Drive folder. After the user submits the form, my goal is to trigger a cloud function using google.script.run while ...

JavaScript appending checkboxes to a list not functioning as expected

I have not been using jQuery, JavaScript, and HTML to create a list of products. The task I am working on now is the ability to select multiple items from this list. Currently, the HTML list is dynamically populated with <li> elements using JavaScri ...

Is the Nopcommerce theme being upgraded from version 2.3 to 3.0?

I am new to nopCommerce and facing a challenge. I need to upgrade a nopCommerce 2.2 site to version 3.0, but only the theme. I already have two sites designed, and I want to apply the 3.0 theme to the 2.2 site. If it were just a simple HTML page, copying ...

What could be causing my dropdown menu to not appear when clicked?

I am trying to implement the functionality shown in the image. When the Settings button is clicked, a window should open allowing the user to navigate to their desired option. However, I am facing an issue where nothing happens when the button is clicked. ...

The jQuery AJAX doesn't specify the complete server path in the URL

Hey friends, I could really use some help with this situation. It's driving me mad because I've successfully done it before, but now it just won't work. Here's the issue - I have an HTML button that triggers a JavaScript function when ...

Utilizing Boostrap to create a background image positioned on the far left of the screen within a .container

I am currently working with bootstrap 4 and have a layout that includes a container class: body #content.container .row .col-6 | Great content .col-6 | I really like it .row .col-12 And so forth .row ...

What is the most effective way to retrieve the children and ID of an item in the jQuery Nestable plugin following a drag-and-drop action,

I'm currently implementing the jQuery Nestable plugin to build a menu editor for a website. After users click on menu items and rearrange their positions, I need to retrieve the item's ID and its Children.   Challenge: I'm struggling with ...

Alter the background of a div in a webpage using PHP and jQuery to display a

I have a collection of 10 background images for each season (Spring, Summer, etc.) stored in folders labeled 1, 2, 3, and 4. I am looking to randomly change the div background image for each season. For example, changing the random image from folder 1 for ...

Getting the values of several labels using a class name - a comprehensive guide

Is there a way to retrieve the values of labels with the "timeAuction" class? I'm currently working on a JavaScript function that will target each label with the class name timeAuction and adjust its value. The number of labels with this class can va ...

What steps can be taken to modify this jQuery code so that any changes made are also saved to localStorage

I have successfully used jQuery to change the background color of all my divs with the same class (userNumber1) when I check its checkbox. However, I am now looking for a way to save these changes to localStorage, so that they persist each time the page is ...

When attempting to execute a function within another function in JavaScript, a ReferenceError is triggered

I recently developed a straightforward app that utilizes the Google Drawing Library (https://developers.google.com/maps/documentation/javascript/examples/drawing-tools) to allow users to draw circles on a map. The first circle represents the source locatio ...