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
.
Should it Ever Be Used? In many cases, applying width: 100% to a block level element is either unnecessary or will bring undesirable results. If you're using padding on the inner element and you use box-sizing: border-box , then you'll be safe.
Width 100% On the other hand, if you specify width:100%, the element's total width will be 100% of its containing block plus any horizontal margin, padding and border (unless you've used box-sizing:border-box, in which case only margins are added to the 100% to change how its total width is calculated).
Answer is No. cause 100 is pixels and 100% is percentage of overall size of page.
Yes, as per the CSS 2.1 Specification, all non-negative values are valid for width, that includes percentage values above 100%. Show activity on this post. Percentage values simply represent a percentage of the length of the element's container.
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%.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With