css height : 100% vs height : auto 之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15943009/
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 22:24:28  来源:igfitidea点击:

difference between css height : 100% vs height : auto

css

提问by Gowsikan

I was asked a question in an interview that "what is the difference between the css height:100%and height:auto?"

我在一次采访中被问到“cssheight:100%和之间有什么区别height:auto?”

Can any one explain?

谁能解释一下?

回答by Manish Mishra

height: 100%gives the element 100% height of its parent container.

height: 100%为元素提供其父容器的 100% 高度。

height: automeans the element height will depend upon the height of its children.

height: auto意味着元素高度将取决于其子元素的高度。

Consider these examples:

考虑以下示例:

height: 100%

高度:100%

<div style="height: 50px">
    <div id="innerDiv" style="height: 100%">
    </div>
</div>

#innerDivis going to have height: 50px

#innerDiv将会有 height: 50px

height: auto

高度:自动

<div style="height: 50px">
    <div id="innerDiv" style="height: auto">
          <div id="evenInner" style="height: 10px">
          </div>
    </div>
</div>

#innerDivis going to have height: 10px

#innerDiv将会有 height: 10px

回答by Rohit Azad

A height of 100%for is, presumably, the height of your browser's inner window, because that is the height of its parent, the page. An autoheightwill be the minimum heightof necessary to contain.

一个100%的高度的是,据推测,您的身高浏览器的内部窗口,因为这是其母公司的高度,页面。的auto高度将是最小高度必要遏制

回答by Vahid Heydarinezhad

The default is height: autoin browser, but height: X%Defines the height in percentage of the containing block.

默认height: auto在浏览器中,但height: X%以包含块的百分比定义高度。

回答by Chukwuemeka

height:100% works if the parent container has a specified height property else, it won't work

如果父容器具有指定的高度属性,则高度:100% 有效,否则无效