CSS3 using box-sizing layouts

Source: Internet
Author: User

CSS3 adds a box model box-sizing with the following three attribute values:

content-box: The default value, which allows elements to maintain the standard box model. The element's width/height (width/height) (occupied space) equals the element border width (border) plus the element padding (padding) plus the element content width/height (contents width/height) that is: element width/ Height = Border+padding+content width/height.

border-box: Allows elements to maintain IE6 and the following version of the box model, the width/height of the element (occupied space) equals the width/height of the element content. The content width/height here contains the border,padding of the elements, the width/height of the contents. That is: Element width/height =width/height-border-padding.

inherit: Inherits the box model pattern of the parent element.

For example, when writing a project, someone wants you to write the content in a space, as follows:

<Divclass= "wrapper">    <DivID= "header">Header</Div>    <Divclass= "sidebar">Side bar</Div>    <Divclass= "Content">Content content content Content contents content content Contents</Div>    <DivID= "Footer">Footer</Div></Div>

        *{margin:0;padding:0;}. Wrapper{width:500px;margin:0 Auto;Color:#fff;text-align:Center; }#header{Height:50px;Background-color:Red; }. Sidebar{float: Left;width:150px;Height:100px;Background-color:#0099cc; }. Content{float: Left;width:350px;Height:100px;Background-color:#999999; }#footer{Background-color:#ff6600;Height:50px;Clear:both; }

Finally written, but if you are asked to change the look, such as the content area plus the inner margin, the border and so on decoration.

If you add padding, border or something directly, the layout is immediately broken:

Because box-sizing default is Content-box, the content area size will not change, plus padding, margin, border, will be stretched outward, thereby destroying the layout structure

At this point, the use of border-box can be solved perfectly.

        . Content{            box-sizing: border-box;             Padding: 22px;             Border: 12px solid Blue;             float: left;             width: 350px;             height: 100px;             background-color: #999999;        }

In this way, the space occupied by the elements will not change, add padding, border, will squeeze inward, keep the outside container is not destroyed

(Note that margin is not included in the element space, and the margin is stretched outward)

Compatibility: ie8+ and other mainstream browsers support box-sizing. The default of IE6 and below is to calculate the size with a similar border-box box model.

(Ps:firefox browser, box-sizing can also set a padding-box that specifies the width/height of the element equal to the width/height and padding of the content,

That is: Element width/height = content width/height+padding. )

CSS3 using box-sizing layouts

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.