Html 如何固定 <div> 元素的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4354722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:35:38  来源:igfitidea点击:

How to fix the height of a <div> element?

csshtml

提问by Jake

I've defined my div's height in a stylesheet:

我在样式表中定义了 div 的高度:

.topbar{
  width:100%;
  height:70px;
  background-color:#475;
}

But as soon as text is entered into the div, the divs height changes.

但是一旦文本输入到 div 中,div 的高度就会发生变化。

Any ideas?

有任何想法吗?

回答by kobe

change the div to display block

将 div 更改为显示块

.topbar{
    display:block;
    width:100%;
    height:70px;
    background-color:#475;
    overflow:scroll;
    }

i made a jsfiddle example here please check

我在这里做了一个 jsfiddle 例子请检查

http://jsfiddle.net/TgPRM/

http://jsfiddle.net/TgPRM/

回答by shamazing

If you want to keep the height of the DIV absolute, regardless of the amount of text inside use the following:

如果你想保持 DIV 的绝对高度,不管里面有多少文本,请使用以下内容:

overflow: hidden;

回答by Yuriy Buha

You can also use min-height and max-height. It was very useful for me

您还可以使用 min-height 和 max-height。这对我很有帮助

回答by Vlad.P

You can try max-height: 70px; See if that works.

你可以试试 max-height: 70px; 看看这是否有效。