如何使div固定在高度,但如果内容大于高度则增长?

时间:2022-03-11 21:28:07

Let's say I have a div to which I set height: 500px; in CSS, thus it's fixed in height.

假设我有一个我设置高度的div:500px;在CSS中,因此它的高度是固定的。

The content of my page is generated automatically in JavaScript, thus sometimes it's bigger than 500px and it overflows the div border.

我的页面内容是在JavaScript中自动生成的,因此有时它会超过500px并溢出div边框。

My question is: is it possible to have a fixed size (for example 500px) but when the div's content is greater than 500px, have the div growing bigger to contain that?

我的问题是:是否有可能有一个固定的大小(例如500px),但当div的内容大于500px时,让div变得更大以包含它?

Thanks

3 个解决方案

#1


22  

Try

min-height: 500px;
height:auto;

to set a minimum height.

设置最小高度。

For reference: http://www.w3schools.com/cssref/pr_dim_min-height.asp

供参考:http://www.w3schools.com/cssref/pr_dim_min-height.asp

#2


6  

Yes

min-height:300px;
height:auto;

#3


2  

try this


min-height:500px;
overflow:auto;
margin:0 auto;

#1


22  

Try

min-height: 500px;
height:auto;

to set a minimum height.

设置最小高度。

For reference: http://www.w3schools.com/cssref/pr_dim_min-height.asp

供参考:http://www.w3schools.com/cssref/pr_dim_min-height.asp

#2


6  

Yes

min-height:300px;
height:auto;

#3


2  

try this


min-height:500px;
overflow:auto;
margin:0 auto;