jQuery – Set min-height of div

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 🙂

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

$(".foo").css("min-height", function(){ 
    return $(this).height();
});

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!

var elt = document.getElementById("<%= this.your_element_id.ClientID %>");
elt.style.minHeight = elt.clientHeight + 'px';


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   What is difference between Module Loader and Module Bundler in JavaScript?

Similar Posts