width:100% vs. width:inherit

HtmlCss

Html Problem Overview


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?

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?

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

Similarly with height.

Html Solutions


Solution 1 - Html

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

width:inherit inherits width that defined by parent.

HTML:

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

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%.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsawaView Question on Stackoverflow
Solution 1 - HtmlChuck NorrisView Answer on Stackoverflow