Html 宽度:100% 与宽度:继承

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

width:100% vs. width:inherit

htmlcss

提问by sawa

My understanding is that width: 100%lets the element's width be the same as its parent's, whereas width: inheritdoes that only when the parent's width is explicitly specified. Is this understanding correct?

我的理解是width: 100%让元素的宽度与其父元素的宽度相同,而width: inherit只有在明确指定父元素的宽度时才这样做。这种理解是否正确?

If so, it looks to me that when width: inheritworks, then width: 100%would always work, so you could always use the latter. Then, what is the purpose of writing width: inherit? When does it become useful?

如果是这样,在我看来,当width: inherit有效时,那么width: 100%总是有效,所以你总是可以使用后者。那么,写作的目的是width: inherit什么?它什么时候变得有用?

If my understanding is wrong, what is the difference between the two?

如果我的理解是错误的,这两者有什么区别?

Similarly with height.

height.

回答by Chuck Norris

See jsfiddle http://jsfiddle.net/bt5nj/2/and http://jsfiddle.net/bt5nj/3/

见 jsfiddle http://jsfiddle.net/bt5nj/2/http://jsfiddle.net/bt5nj/3/

width:inheritinherits width that defined by parent.

width:inherit继承由parent定义的宽度。

HTML:

HTML:

<div id="parent">
<div id="child"></div>
</div>?

CSS:

CSS:

#parent
{
    width:50%;
    height:30px;
}
#child
{
    width:inherit;
    height:100%;
    background-color:red;
}

This makes childwidth 25%, but if I redefine it with width:100%it will define width of child50%.

这使得child宽度为 25%,但如果我重新定义它,width:100%它将定义child50% 的宽度。

?

?