Today, when I was studying, I accidentally saw a picture:
I stared in an instant: width and height did not include padding and border!!.
There is a problem in the past knowledge! In my impression, width should include padding and border, but then I see another picture:
This picture is the box sub model under IE , and the first picture is the standard box model ...
So I decided to try and practice.
#box { width: 100px; height: 100px; margin: 20px; padding: 20px; background-color: Blue; Border:solid red 4px; }
Effect
As you can see, in the Chrome browser, width is only the actual content part of the element, not including border and padding, and is consistent with the standard box model. and BackgroundColor covers padding and content
But add a piece of code to the CSS:
Box-sizing:border-box
The effect is the same as the box model under IE:
At this time 100px=content+padding+border
In other words: At this time border and padding are calculated into the width.
In addition, Box-sizing has two values:
Content-box:border and padding do not count into width
Padding-box:padding calculation into width inside
In fact, the WebKit kernel browser, such as Chrome,safari, settings content-box,padding-box, browser rendering is consistent with the standard box model
Only Firefox rendering results are normal, here do not post test code and results, you can test yourself.
CSS box model and box-sizing