CSS box model and box-sizing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.