box-sizing

suggest change

The default box model (content-box) can be counter-intuitive, since the width / height for an element will not represent its actual width or height on screen as soon as you start adding padding and border styles to the element.

The following example demonstrates this potential issue with content-box:

textarea {
    width: 100%;
    padding: 3px;
    box-sizing: content-box; /* default value */
}

Since the padding will be added to the width of the textarea, the resulting element is a textarea that is wider than 100%.

Fortunately, CSS allows us to change the box model with the box-sizing property. The two standard values are:

padding-box (width and height include the content and padding, but not the border) was never interoperable and has been removed from the specification. Use border-box instead.

To solve the textarea problem above, set box-sizing to border-box.

textarea {
    width: 100%;
    padding: 3px;
    box-sizing: border-box;
}

To apply a specific box model to every element on the page, use the following snippet:

html {
    box-sizing: border-box;
}

*, *:before, *:after {
    box-sizing: inherit;
}

In this pattern, box-sizing: border-box is not applied directly to *. Descendants inherit it, so an individual element can set a different value.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents