Consistent Row Heights for Dynamic Width Elements

I've relied on Chris Coyier's Equal Height Blocks in Rows jQuery script for various projects, and it has consistently delivered great results.

However, this time I am facing a new challenge as I work on a responsive website with a fluid width main container. Unlike my previous projects with fixed resolutions, the fluid nature of the layout is causing an issue that I can't seem to resolve.

In his tutorial, Chris mentions adapting the script for fluid widths:

What About Fluid Width? One of the main advantages of using floated divs over tables is the ability to rearrange based on available horizontal space, especially useful for fluid layouts. The code can be modified to handle this by measuring block heights initially and retaining them with jQuery's data() method, then adjusting based on the original sizes rather than the resized dimensions during window resizes.

While the tutorial specifically addresses fluid width containers, my challenge lies with child elements within these fluid containers. These same elements with fluid widths are what need to be equalized, but their varying sizes depending on the container dimensions seem to disrupt the script's functionality.

I suspect that the changing 'originalHeight' property of the elements due to container resizing is causing the script to malfunction.

It's possible that my assumptions are incorrect, but regardless, the script does not seem to be effective with fluid width elements. Any assistance would be greatly appreciated!

For reference, here is a jsfiddle showcasing the issue. Simply adjust the window width to see that the container heights do not update accordingly.

To trigger the function on window resize, I'm using the following code:

$(window).resize(function() {
    columnConform();
});

Thank you in advance for your help.

Answer №1

When you need to equalize the heights of columns within a row, consider using this simplified jQuery code:

$.fn.extend({
    equalHeights: function(options){
      var maxHeight = Math.max.apply(null, $(this).map(function(){ return $(this).height(); }).get());
      if (typeof maxHeight == 'number') $(this).height(maxHeight);
    }
 });

To execute this, you can simply use:

$(document).ready(function() {
    $(window).resize(function() {
        $('.equalize').equalHeights();
    }).trigger('resize');
})

If you want to apply this per pseudo row, check out this working demo:

<ul class="eqme">
  <li><h1>One</h1></li>
  <li><h2>Two</h2></li>
  ....
</ul>

For handling multiple elements within the same parent element, even across rows based on screen size changes, you can use this more complex script:

$.fn.extend({
equalHeights: function(){
    var topOffset = 0;
    var className = ('equalHeights' + Math.random()).replace('.','');
    $(this).each(function(){
      if ($(this).is(':visible')){
        var thisTop = $(this).offset().top;
        if (thisTop > topOffset) {
            $('.' + className).removeClass(className); 
            topOffset = thisTop;
        }
        $(this).addClass(className);
        $(this).height('auto');
        var maxHeight = Math.max.apply(null, $('.' + className).map(function(){ return $(this).outerHeight(); }).get());
        $('.' + className).height(maxHeight);
      }
    }).removeClass(className); 
}       
});

$(function(){
  $(window).resize(function(){
    $('.eqme li').equalHeights();
  }).trigger('resize');
});

A few notes to consider: Using outerHeight() may provide better results in some cases. Also, adding a clearTimeout and setTimeout within the window.resize function can prevent slowdowns during resizing.

Answer №2

After carefully considering your inquiry, the following code snippet is proposed as a solution:

$(document).ready(function() {

    $(window).on('resize', function() {
        $('.equalize').css('height', determineHighest($('p')));
    }).trigger('resize');

});

function determineHighest(element) {
    var highestHeight = 0;
    element.each(function() {
        var heightVal = $(this).height();
        if(heightVal > highestHeight) {
          highestHeight = heightVal;
        }
    });
    return highestHeight;   
}

Fiddle

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

How can I store a value or text to track view counts or a counter efficiently?

Recently, I've been trying to implement a view counter on my website, similar to what you see on YouTube. Currently, the number of views stands at 23. I managed to find some code that allows me to increment the view count every time I click on an imag ...

Auto-fill input field with URL parameter values using JavaScript or jQuery

I am in possession of a URL http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Denmark Upon accessing the above link, a user will be directed to a new page containing a form. Within this form, there is an i ...

How can I prevent an endless loop in jQuery?

Look at the code snippet below: function myFunction(z){ if(z == 1){ $(".cloud").each(function(index, element) { if(!$(this).attr('id')){ $(this).css("left", -20+'%'); $(this).next('a').css ...

How can JSON-loaded scripts be executed using jQuery?

I received a file with the following content: {'html' : '<div id="bla">something</div>', 'script' : ' $().ready(function() { /* some code */});' } I want to use jQuery to load this file and execute the ...

Inaccurate rendering of border widths on IOS platform

After setting the border width of these input boxes to 1px, it appears that on iOS devices, the top border seems larger than intended (regardless of the browser being used). Interestingly, on any other type of device, the border looks perfectly fine. & ...

Delete the CSS class if the element with the class ".dropdown" does not currently have the class

In a larger view port, this menu is displayed horizontally. When you click on the dropdown, the menu expands and the list is shown in line. A span is added to show a list strip under the parent level. Everything seems to be working fine except for one thin ...

How can HTML text be highlighted even when it's behind a transparent div?

In my React application, I am facing an issue with a transparent div that serves as a dropzone for draggable elements. This div covers text and also contains children elements that may have their own text content. <Dropzone> <SomeChild> . ...

Updating the Facebook Comment Plugin with Jquery once it has finished loading

My goal is to populate the TextArea of a Facebook Comment Plugin so that my text is displayed inside it once it loads. The HTML code for the comment section includes: <div class="fb-comments facebook" data-href="https://www.facebook.com/RighToDignity" ...

Is it possible to load a Twitter widget from dynamically loaded HTML using AJAX?

I've been attempting to implement a standard Twitter search widget directly from the Twitter website: <script src="http://widgets.twimg.com/j/2/widget.js"></script> <script> new TWTR.Widget({ version: 2, type: 'search' ...

Use regular expressions and JavaScript to enclose a series of English letters within a wrapper

I am looking to enclose a series or cluster of consecutive English characters within a <span> tag. In simpler terms, I aim to alter the appearance of English language in my writing. Therefore, I need to identify English characters and surround them ...

How can I add page transitions to my simple HTML website?

Working on an internal website project for my office using vue.js has been a rewarding experience. I appreciate how easily it allows me to manage multiple pages with dynamic content and seamless transitions. Unfortunately, my IT department is hesitating to ...

Tips for avoiding content appearing beneath overlapping elements

I am facing an issue with a hero image that has an overlapping box. I want to add content below the box but it ends up appearing behind the box. How can I ensure that the content shows below the box while maintaining responsiveness? .shell { display: ...

Vue.js fails to display multiple uploaded images

I have been working on implementing a multiple image upload feature using vue.js. So far, everything seems to be functioning correctly. However, I am facing an issue where the HTML does not display thumbnails of the images I have selected. Additionally, I ...

I'm working with Angular 12, Bootstrap 5, and ngPrime, and I'm looking to overlap a p-dialog with another element in my project

Is there a way in Angular 12 with Bootstrap 5 using ngPrime to overlap a p-dialog over any other element without using z-index and CSS, solely relying on PrimeNG? I have tried using z-index with: .my-class{ z-index: 2147483647 !important; } However, ...

Retrieve the child element that is being clicked

Alright, I'm facing a little issue here (it seems simple, but I just can't seem to crack it)... Let me paint the picture with a snippet of HTML code below: <!-- New Website #1 --> <!DOCTYPE html> <html style='min-height:0px; ...

Is there a way to automatically click the 'next' arrow on my image slider?

I'm currently working on a website that uses the Semplice theme in Wordpress. The built-in slider doesn't have an autoplay feature, so I attempted to use jQuery to automatically click the 'next' arrow every 5 seconds. However, I ran int ...

The functionality of the jQuery program is compromised when used on the Google Chrome browser

I just recently started diving into the world of Jquery, attempting to learn it from scratch. However, I have encountered an issue while trying to make my Jquery program run smoothly: Let's take a look at the CSS first: p { opacity: 0; } Now on ...

Identify when a div reaches the end of the screen using CSS

I've developed a custom dropdown feature using Vue 2 and TypeScript to meet our specific requirements (without relying on jQuery). The dropdown functions correctly, opening the options list downwards when the main box is clicked. One enhancement I a ...

mandatory data fields for an HTML form

I'm having some trouble creating an HTML form with mandatory input fields. The code I have so far is shown below: <div class="col-sm-6"> <label for="city" class="form-control">City ...

Provide two values and complete the third one

I have a form with three input fields. I want to fill out two of the fields and have the third field automatically filled in. Here is how it should work: - I fill out the first and second fields, and the third field calculates itself - I fill out ...