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
width:100% vs. width:inherit
提问by sawa
My understanding is that width: 100%
lets the element's width be the same as its parent's, whereas width: inherit
does 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: inherit
works, 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:inherit
inherits 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 child
width 25%, but if I redefine it with width:100%
it will define width of child
50%.
这使得child
宽度为 25%,但如果我重新定义它,width:100%
它将定义child
50% 的宽度。
?
?