Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery set height of a div to the 'dynamic height' of another

I have two divs.

  <div class="col-1"><p>Content</p></div>
  <div class="col-2"><img src="" alt="" /></div>

With their respected content inside each one.

I am trying to set the height of col-2 to be exactly as that of col-1.

I tried this using jQuery:

   $(document).ready(function() {
        var divHeight = $('.col1').height(); 
        $('.col2').css('min-height', divHeight+'px');
    });

The problem is that col-1 does not have a height set on it. It has a dynamic height which grows when its content grows. Therefore the above code does not work for it.

Is there any way I can set the min height of col-2 to be equal to the dynamic height of col 1 using jQuery?

like image 619
Alex Zahir Avatar asked Aug 20 '15 04:08

Alex Zahir


People also ask

How do I change the height of a DIV dynamically?

The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement.

How do I change the width of a DIV dynamically?

Answer: Use the JavaScript width() method You can set the width of a <div> box dynamically using the jQuery width() method.

How do I get dynamic height in CSS?

We use css property height: calc( 100% - div_height ); Here, Calc is a function. It uses mathematical expression by this property we can set the height content div area dynamically.

How do you measure the height of a div?

Method 1: Using the offsetHeight property: The offsetHeight property of an element is a read-only property and used to return the height of an element in pixels. It includes any borders or padding of the element. This property can be used to find the height of the <div> element.


2 Answers

this works fine for me, its just that your class names are wrong, the class-names should be .col-1 and .col-2:

$(document).ready(function() {
    var divHeight = $('.col-1').height(); 
    $('.col-2').css('min-height', divHeight+'px');
});  

Here is the Fiddle

EDIT- Based on comments:
You can do all this without using jquery

  1. Flexbox (not supported in IE 8 & 9) - if you apply the flex to the parent div, it will make all its children heights equal:

     .row{ display: flex;}  
    
  2. table - you can give your div's the table layout

    .row {display: table; }
    
    .row div { display: table-cell;}
    
like image 75
Luthando Ntsekwa Avatar answered Nov 15 '22 18:11

Luthando Ntsekwa


First of all you need to fix the class names that missed '-' in your jQuery code.

If you want to get the .col-1 height you can do it in several ways, that I will discuss later.

Before that in each case you need to write a function that gives .col-1 height and set .col-2 .

$(document).ready(function() {

    function set_heights(){
        var divHeight = $('.col-1').height(); 
        $('.col-2').css('min-height', divHeight+'px');
    }

});

Then you just call the function whenever you need...

Some of different ways are :

  1. to set interval or timer to call above function once in a period of time you need.

    setInterval(function(){  set_heights(); }, 100);
    
  2. use resize event for .col-1 . to call above function when ever .col-1 changed.

    $('.col-1').resize(function(){
        set_heights();
    });
    
  3. use bind

remember!!! for responsive design you need too call above function even when window resized !

Good luck !

like image 29
Mehrdad Pedramfar Avatar answered Nov 15 '22 19:11

Mehrdad Pedramfar