How do I remove the height style from a DIV using

2019-01-22 10:44发布

By default, a DIV's height is determined by its contents.

But, I override that and explicitly set a height with jQuery:

$('div#someDiv').height(someNumberOfPixels);

How can I reverse that? I want to remove the height style and to make it go back to it's automatic/natural height?

9条回答
该账号已被封号
2楼-- · 2019-01-22 11:11

just to add to the answers here, I was using the height as a function with two options either specify the height if it is less than the window height, or set it back to auto

var windowHeight = $(window).height();
$('div#someDiv').height(function(){
    if ($(this).height() < windowHeight)
        return windowHeight;
    return 'auto';
});

I needed to center the content vertically if it was smaller than the window height or else let it scroll naturally so this is what I came up with

查看更多
Emotional °昔
3楼-- · 2019-01-22 11:12
$('div#someDiv').removeAttr("height");
查看更多
放荡不羁爱自由
4楼-- · 2019-01-22 11:13

To reset the height of the div, just try

$("#someDiv").height('auto');

查看更多
登录 后发表回答