Set DIV height dynamically based on viewport height

elbatron picture elbatron · Jul 3, 2011 · Viewed 38.7k times · Source

I'm trying to set a div's height to 30% of the viewport height and I would very much like to scale it when the height of the viewport changes.

I tried setting min-height: 30%; height:30% but it is not working.

I took a look at JQuery's height(); but I just don't know how to get started.

Thanks.

Answer

Liam Bailey picture Liam Bailey · Jul 3, 2011
function thirty_pc() {
    var height = $(window).height();
    var thirtypc = (30 * height) / 100;
    thirtypc = parseInt(thirtypc) + 'px';
    $("div").css('height',thirtypc);
}

$(document).ready(function() {
    thirty_pc();
    $(window).bind('resize', thirty_pc);
});