{"id":16,"date":"2018-01-01T07:00:45","date_gmt":"2018-01-01T14:00:45","guid":{"rendered":"https:\/\/coreassistance.com\/tips\/?p=16"},"modified":"2018-01-12T13:26:31","modified_gmt":"2018-01-12T20:26:31","slug":"intuitive-box-sizing","status":"publish","type":"post","link":"https:\/\/coreassistance.com\/tips\/2018\/01\/01\/intuitive-box-sizing\/","title":{"rendered":"Intuitive Box Sizing"},"content":{"rendered":"<p>CSS is great, but it has some unusual quirks. One of its most annoying oddities has to do with how the width and height of elements are calculated. Take this CSS, for example:<\/p>\n<pre><code class=\"css\">div.example {\r\n    width: 100px;\r\n    height: 100px;\r\n    padding: 10px;\r\n    border: solid 2px;\r\n}<\/code><\/pre>\n<p>You might expect the <code>div.example<\/code> element to be 100 pixels wide and 100 pixels high, as those are the values specified for the <code>width<\/code> and <code>height<\/code> properties, but you would be wrong. The CSS above will actually result in a <code>div.example<\/code> that has a width and height of <em>124 pixels<\/em>.<\/p>\n<p>Where did the number 124 come from? By default in CSS and element&#8217;s dimension are calculated using something called the content box model. That means the area where the content goes, in the middle of an element, are what the <code>width<\/code> and <code>height<\/code> properties apply to. So, in this case, our context box is made to be 100 pixels square. Then the padding and border are added on top of that. This results in a width of 124 pixels: 100 pixels of content width, 10 pixels of padding on either side, and 2 pixels of border on either side. 100 + 10 + 10 + 2 + 2 = 124. The height is calculated the same way.<\/p>\n<p>Quirky, isn&#8217;t it? That&#8217;s not how most people think about this kind of thing. When you write something like <code>width: 100px<\/code> you expect the thing to be 100 pixels wide, right?<\/p>\n<p>Well, I&#8217;ve got good news! You can specify a different box sizing model in CSS that actually does what you would expect. All you need to do is set the <code>box-sizing<\/code> property to <code>border-box<\/code>, like this:<\/p>\n<pre><code class=\"css\">div.example {\r\n    box-sizing: border-box;\r\n    width: 100px;\r\n    height: 100px;\r\n    padding: 10px;\r\n    border: solid 2px;\r\n}<\/code><\/pre>\n<p>Now, instead of the <code>width<\/code> and <code>height<\/code> properties applying to the content box in the middle of the element, they instead apply to the outermost border box surrounding the outer edge of the element. This results in a 100 pixel square box, which is much more intuitive.<\/p>\n<p>I don&#8217;t know about you, but I vastly prefer the <code>border-box<\/code> model, which is why I almost always start every new project with the following CSS at the top of the stylesheet, which applies <code>border-box<\/code> sizing to all elements so I don&#8217;t have to think about it again:<\/p>\n<pre><code class=\"css\">* {\r\n    box-sizing: border-box;\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The default box model in CSS is a bit strange.  Learn how to fix it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-htmlcss"],"_links":{"self":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":10,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/posts\/16\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coreassistance.com\/tips\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}