Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

width:100% vs. width:inherit

Tags:

html

css

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.

like image 858
sawa Avatar asked Feb 21 '12 08:02

sawa


People also ask

Should you use 100% width?

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.

What is the meaning of width 100%?

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

Is width 100 and width 100% the same?

Answer is No. cause 100 is pixels and 100% is percentage of overall size of page.

Can we give width more than 100%?

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.


1 Answers

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

like image 189
Chuck Norris Avatar answered Oct 13 '22 02:10

Chuck Norris