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

Alex Zahir

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?

Luthando Ntsekwa

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;}
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery to set div height

From Dev

How to Set Dynamic Height jQuery

From Dev

Set div height with javascript from another div height

From Dev

Set height of <div> = to height of another <div> through .css

From Dev

Using js to set height of one div to the same height as another div

From Dev

set height of one div to another div in angularjs

From Dev

Take in parent divs (dynamic) height, and set child div to same height

From Dev

Jquery Hide or show div on height of another div

From Dev

Set div's height like height of next sibling div with jQuery

From Dev

Set div height to window height

From Dev

Scrolling dynamic height div with dynamic height footer

From Dev

Not Able to Set Height Of Div Dynamically using jQuery

From Dev

Dynamic height of div

From Dev

Dynamic height of table is not set

From Dev

Positioning a relative div in another relative div with dynamic height

From Dev

How to set content area div height with dynamic header div

From Dev

How to set content area div height with dynamic header div

From Dev

Adding Height to another Div

From Dev

Setting the height of a div based on the height of another div

From Dev

Setting the height of a div based on the height of another div

From Dev

div height depends another div height

From Dev

how to align vertically a dynamic-height div inside another fixed height div

From Dev

Flowing one floated Div around another Div of set height

From Dev

How to set min-height to div which is in another div in Bootstrap?

From Dev

How to set height of a div element to same as another div

From Dev

set div full height and width in click from another div

From Dev

Set height of div to 0

From Dev

Set div with different height?

From Dev

Angular Set Dynamic Height for element of his parent div

Related Related

  1. 1

    jQuery to set div height

  2. 2

    How to Set Dynamic Height jQuery

  3. 3

    Set div height with javascript from another div height

  4. 4

    Set height of <div> = to height of another <div> through .css

  5. 5

    Using js to set height of one div to the same height as another div

  6. 6

    set height of one div to another div in angularjs

  7. 7

    Take in parent divs (dynamic) height, and set child div to same height

  8. 8

    Jquery Hide or show div on height of another div

  9. 9

    Set div's height like height of next sibling div with jQuery

  10. 10

    Set div height to window height

  11. 11

    Scrolling dynamic height div with dynamic height footer

  12. 12

    Not Able to Set Height Of Div Dynamically using jQuery

  13. 13

    Dynamic height of div

  14. 14

    Dynamic height of table is not set

  15. 15

    Positioning a relative div in another relative div with dynamic height

  16. 16

    How to set content area div height with dynamic header div

  17. 17

    How to set content area div height with dynamic header div

  18. 18

    Adding Height to another Div

  19. 19

    Setting the height of a div based on the height of another div

  20. 20

    Setting the height of a div based on the height of another div

  21. 21

    div height depends another div height

  22. 22

    how to align vertically a dynamic-height div inside another fixed height div

  23. 23

    Flowing one floated Div around another Div of set height

  24. 24

    How to set min-height to div which is in another div in Bootstrap?

  25. 25

    How to set height of a div element to same as another div

  26. 26

    set div full height and width in click from another div

  27. 27

    Set height of div to 0

  28. 28

    Set div with different height?

  29. 29

    Angular Set Dynamic Height for element of his parent div

HotTag

Archive