{"id":5969,"date":"2013-04-06T07:35:06","date_gmt":"2013-04-06T11:35:06","guid":{"rendered":"http:\/\/www.ericfeminella.com\/blog\/?p=5969"},"modified":"2013-08-12T09:12:56","modified_gmt":"2013-08-12T13:12:56","slug":"natural-box-model-sizing","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2013\/04\/06\/natural-box-model-sizing\/","title":{"rendered":"Natural Box Model Sizing"},"content":{"rendered":"<p>As Web Developers, the benefits to be afforded by simply taking the time to aquire a fundamental understanding of CSS layouts can not be overstated; for it is these very skills that provide the basis from which designs can be achieved with ease; making the task of designing the Web an enjoyable, and rewarding experience. Conversely, without an understanding of core concepts, one is certain to spend a significant amount of time &#8211; often in frustration &#8211; attempting to achieve a desired layout.<\/p>\n<h2>CSS Layout Fundamentals<\/h2>\n<p>In the context of CSS layouts, such fundamental concepts to be considered (in no particular order) include: <a href=\"http:\/\/webdesign.tutsplus.com\/tutorials\/htmlcss-tutorials\/quick-tip-utilizing-normal-document-flow\/\" target=\"_blank\">document flow<\/a>, <a href=\"http:\/\/alistapart.com\/article\/css-positioning-101\" target=\"_blank\">positioning<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/CSS\/display\" target=\"_blank\">display types<\/a>, <a href=\"http:\/\/reference.sitepoint.com\/css\/top\" target=\"_blank\">off-sets<\/a>, <a href=\"http:\/\/alistapart.com\/article\/css-floats-101\" target=\"_blank\">floats<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/CSS\/overflow\" target=\"_blank\">overflows<\/a>, <a href=\"http:\/\/reference.sitepoint.com\/css\/clear\" target=\"_blank\">clears<\/a>, and the like; and, as the title of this article suggests, the <a href=\"http:\/\/www.w3.org\/TR\/CSS2\/box.html\" target=\"_blank\">Box Model<\/a>.<\/p>\n<h2>The Box Model<\/h2>\n<p>I emphasize the importance of the <strong>Box Model<\/strong> here in particular, as the default sizing of elements with respect to the Box Model is quite the opposite of what one might expect. <\/p>\n<p>For example, consider the following:<\/p>\n<pre lang=\"css\">\n.box {\n    width: 200px;\n    height: 200px;\n    padding: 20px;\n    border: 1px solid #333;\n}\n<\/pre>\n<p>As many would assume, any element with the <code>.box<\/code> class would render with a 1px border and 20px of padding, at exactly 200x200px. However, by default, this is not how elements are sized, but rather, the actual size of a rendered element is calculated to include both <code>borders<\/code> and <code>padding<\/code> in addition to <code>width<\/code> and <code>height<\/code>, the calculation for which essentially being:<\/p>\n<pre>\ntotal width  = left border width + left padding + \n               width + \n               right padding + right border width;\ntotal height = top border width + top padding + \n               height + \n               bottom padding + bottom border width;\n<\/pre>\n<p>This results in nearly all elements (form elements notwithstanding) being measured quite differently then one might have expected. Thus, in the example of the <code>.box<\/code> class mentioned above, rather than elements being rendered at the expected 200x200px, as defined by their respective width and height properties, they would instead be rendered at 242x242px.<br \/>\n<a href=\"http:\/\/code.ericfeminella.com\/articles\/examples\/box-sizing\/content-box.html\" target=\"_blank\" style=\"border:none\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.ericfeminella.com\/blog\/articles\/assets\/images\/content-box.png?w=640&#038;ssl=1\" alt=\".box content-box\" data-recalc-dims=\"1\" \/><br \/>\n<\/a><\/p>\n<p>One could argue that in the majority of cases, this is neither what is expected nor what is desired. Fortunately, CSS3 offers the very useful property <a href=\"http:\/\/www.w3.org\/TR\/css3-ui\/#box-sizing\" target=\"_blank\">box-sizing<\/a>, which can be used to override the default sizing of elements (content-box), and allow for sizing them more naturally simply by setting <code>box-sizing<\/code> to <code>border-box<\/code>: <\/p>\n<pre lang=\"css\">\n.box {\n    \/* set 'border-box' to size as expected *\/\n    box-sizing: border-box;\n    width: 200px;\n    height: 200px;\n    padding: 20px;\n    border: 1px solid #333;\n}\n<\/pre>\n<p>By setting <code>box-sizing: border-box<\/code>, elements with the <code>.box<\/code> class will render at the expected size, 200x200px, as defined by the width and height properties; with the content area being 158px, padding 40px, and border at 2px.<br \/>\n<a href=\"http:\/\/code.ericfeminella.com\/articles\/examples\/box-sizing\/border-box.html\" target=\"_blank\" style=\"border:none\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.ericfeminella.com\/blog\/articles\/assets\/images\/border-box.png?w=640&#038;ssl=1\" alt=\".box border-box\" data-recalc-dims=\"1\" \/><br \/>\n<\/a><\/p>\n<h2>Box-sizing Global Resets<\/h2>\n<p><a href=\"https:\/\/twitter.com\/paul_irish\" target=\"_blank\">Paul Irish<\/a> has a great solution for this very problem whereby a global reset is used to ensure all elements are sized with <code>border-box<\/code>. I highly recommend this approach as it provides a starting point from which all elements will be sized &#8220;naturally&#8221;. The reset is simple, safe, and works perfectly well in supported browser:<\/p>\n<pre lang=\"css\">\n* {\n    \/* set 'border-box' to size as all elements *\/\n    box-sizing: border-box;\n}\n<\/pre>\n<h2>CSS3 background-clip and background-origin<\/h2>\n<p>The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/CSS\/background-clip\" target=\"_blank\">background-clip<\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/CSS\/background-origin\" target=\"_blank\">background-origin<\/a> properties, respectively, can both be used in tandem with <code>box-sizing<\/code>; each accepting the same values as that of <code>box-sizing<\/code>, allowing for related control of how backgrounds are displayed for elements with respect to the Box Model.<\/p>\n<h2>Concluding Thoughts<\/h2>\n<p>It was quite some time ago while trying to understand how the sizing of elements is determined in more detail that I first learned of the <code>box-sizing<\/code> property. In the time since, I have been using <code>box-sizing:border-box<\/code> with great results and have really come to appreciate this property.<\/p>\n<p>And so, if you haven&#8217;t leveraged <code>box-sizing: border-box<\/code> in your designs yet, I strongly recommend giving it a try, as you will likely find it to result in more &#8220;naturally&#8221; sized elements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As Web Developers, the benefits to be afforded by simply taking the time to aquire a fundamental understanding of CSS layouts can not be overstated; for it is these very skills that provide the basis from which designs can be achieved with ease; making the task of designing the Web an enjoyable, and rewarding experience. Conversely, without an understanding of&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2013\/04\/06\/natural-box-model-sizing\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[133,62,59],"tags":[222,223,162,66],"class_list":["post-5969","post","type-post","status-publish","format-standard","hentry","category-browsers","category-css3","category-html5","tag-box-model","tag-box-sizing","tag-css","tag-css3-2"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/5969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=5969"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/5969\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=5969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=5969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=5969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}