MOCKSTACKS
EN
Questions And Answers

More Tutorials









CSS Box Sizing

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 for an element. There are three different values for the property available:

  • content-box: The common box model - width and height only includes the content, not the padding or border
  • padding-box: Width and height includes the content and the padding, but not the border
  • border-box: Width and height includes the content, the padding as well as the border

change the box-sizing property to padding-box or borderbox. border-box is most commonly used.

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 coding box-sizing:border-box; is not directly applied to *, so you can easily overwrite this property on individual elements.




Conclusion

In this page (written and validated by ) you learned about CSS box sizing . What's Next? If you are interested in completing CSS tutorial, your next topic will be learning about: CSS margin.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.