Change height on div with javascript

拟墨画扇 提交于 2019-12-03 01:19:28

问题


I want to set the height on a div when the div is more than 100px in height.

For example, when the content of the div makes the div's height fill more than 100px, I want it automatically fit to 200 px.


回答1:


One way you can do this is to make sure there is no "height" attribute in the elements CSS (inline styling is fine). Then, when the content is changed call this function:

if ($('#myDiv').height() > 100) {
    // Div is larger than 100px so increase it to 200px
    $('#myDiv').css('height', '200px');
}



回答2:


I think that the min-height CSS property is what you are looking for:

div#myDiv {
    min-height: 100px;
    height: auto; 
    display: block; /* float won't work */
}

This should automatically resize your div to wrap its whole content dynamically.




回答3:


You can just work with min-height and max-height with CSS.

div#myDiv {
min-height: 100px;
height: auto;
max-height: 200px;
}

you could also do something with javascript as said, but with this you can also have the possibility that the height is 150px, or 120px. if you don't want that, You should do it the javascript-way.




回答4:


You can try

if($("divID").height() > 100){
    $("divID").css("height","200px");
}


来源:https://stackoverflow.com/questions/16037449/change-height-on-div-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!