Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Set min-height of div

Tags:

This is probably really easy for most of you. But I'm in need of a small snippet that looks up the current height of a div (the div has a dynamic height based on the content inside it) and then set that value in the css class's min-height value.

Basically what this means is that I want this container to have it's min-height to be the exact same value as it's current height. This is probably a quick one :)

like image 409
Kenny Bones Avatar asked May 01 '10 17:05

Kenny Bones


3 Answers

If I understand you correctly, you could do the following:

$(".foo").css("min-height", function(){ 
    return $(this).height();
});
like image 66
Sampson Avatar answered Oct 23 '22 00:10

Sampson


var elt = document.getElementById("<%= this.your_element_id.ClientID %>");
elt.style.minHeight = elt.clientHeight + 'px';
like image 26
Denis Maslov Avatar answered Oct 23 '22 01:10

Denis Maslov


just a proof of concept!

     // attend for dom ready
    $(function() {
        // hide .foo before we get it's height
        $('.foo').hide();
        // get the height, also the one mentioned below is a good one!
        var foo_height = $('.foo').height();
        // see if the actual height respect our needs! esample 180px
        if ( foo_height > 180 ) {
            // set the height and show it!
            //$('.foo').css('height', foo_height + 'px').show(); OR try
            $('.foo').height( foo_height ).show();
        } else {
            //do something else here
            }
});

this should work as expected!

like image 28
Luca Filosofi Avatar answered Oct 23 '22 01:10

Luca Filosofi