{"id":7,"date":"2007-11-27T19:08:29","date_gmt":"2007-11-28T01:08:29","guid":{"rendered":"http:\/\/cssnewbie.com\/2007\/11\/understanding-the-css-box-model\/"},"modified":"2007-11-27T19:08:29","modified_gmt":"2007-11-28T01:08:29","slug":"understanding-the-css-box-model","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/understanding-the-css-box-model\/","title":{"rendered":"Understanding the CSS Box Model"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/farm1.static.flickr.com\/33\/52872780_e1dc7aad1b_m.jpg\" \/><\/p>\n<p>A fundamental understanding of the CSS box model is essential to gaining a basic understanding of CSS in general. The good news is, if you can pack a box in real life, you can understand the CSS box model. And if you can\u2019t pack a box in real life due to some traumatic physical injury, you shouldn\u2019t have much of a problem, either. Also, I\u2019m sorry about bringing up the whole box thing.<\/p>\n<p><strong>The basic premise behind the box model is this:<\/strong> Every single element on a web page, no matter how big or small, is a box. Got that? Anything that starts with a &lt;tag&gt; and ends with a &lt;\/tag&gt; is a box (container, whatever). So a &lt;p&gt; tag is a box, which is maybe inside of a &lt;div&gt; box, inside of a &lt;body&gt; box, inside of a &lt;html&gt; box. And with CSS, what you\u2019re really doing is manipulating the size, shape, color, decoration and so forth of that box and its contents.<\/p>\n<p>So what\u2019s the box model? The box model is an agreed-upon (mostly&#8230; I\u2019ll get to that in a bit) set of standards for how web browsers will display (model) the content (boxes) on a page. And what does that have to do with you? Well, if you want to style the content on your site, it\u2019s a good idea to know how the rules you write will be displayed.<\/p>\n<p>So here are the most basic properties you have to worry about when dealing with the box model: border, background, padding, width and margin. We\u2019ll go through these properties one by one below. And to help it make sense, we\u2019ll relate it to how you\u2019d pack a box in real life.<\/p>\n<ul>\n<li><strong>Border: <\/strong>This is the box. When you\u2019re starting out, it helps to think of the border as the outline of a real box. It can have different thicknesses (widths) and colors, but its basically just a rigid structure you put things into. Of course, you can also have a border of zero, but it still helps to think of the border as the outermost part of your box, even if it isn\u2019t there.<\/li>\n<li><strong>Background: <\/strong>If you apply a background to something, it stretches from one side of the border to the other, covering the entire inside of your box. Also, if you apply both a background color and a background image, the image will sit on top of the color.<\/li>\n<li><strong>Padding:<\/strong> Think of padding as the bubble wrap or packing peanuts you\u2019d add to a box before shipping it somewhere. Padding lies between your border and your content. This is the sanest property of the lot.<\/li>\n<li><strong>Width:<\/strong> This is the width of your content inside your box. This can be somewhat counter-intuitive at first, because <strong>it is not the width of your border.<\/strong> It\u2019s the width of the stuff <em>inside <\/em>your border, and <em>inside <\/em>your padding. So if you set a width of 400 pixels, with 20 pixels of padding on both sides, and a 5-pixel border, your actual \u201cbox\u201d would be 400+20+20+5+ 5 (otherwise known as 450) pixels wide.<\/li>\n<li><strong>Margin: <\/strong>Margins exist outside of our hypothetical box, around your border. Think of it as space outside your box that you\u2019re deliberately leaving empty, as if the stuff inside the box were volatile, dangerous, stinky, or otherwise something you\u2019d want to give a wide berth. So if you took our box from the width example and gave it a 10 pixel margin on every side, we\u2019d now have a 450 pixel wide \u201cbox\u201d taking up 470 pixels of space.<\/li>\n<\/ul>\n<p>And that\u2019s really all there is to the box model! Well, at least it would be, except for one teensy-weensy little browser that does things a little bit differently. It\u2019s called Internet Explorer. Perhaps you\u2019ve heard of it.<\/p>\n<p>Here\u2019s how it differs: Instead of counting the width as the width of your content, Internet Explorer counts it as the width of everything related to your box. I\u2019ll explain. Let\u2019s say you set the following rules in your style sheet:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">box {\nwidth: 400px;\npadding: 20px;\nborder: 5px solid black;\nmargin: 10px;\n}<\/pre>\n<p>Look familiar? That\u2019s our box from the example above. And as explained, browsers following the box model will give your content 400 pixels of wiggle room, surrounded by a total of 70 pixels of other stuff around it. But Internet Explorer behaves differently. Basically, instead of adding like we did, IE will <em>subtract<\/em>. So instead of ending up with a 400+40+10+20=470 pixel wide space, IE gives you a 400 pixel wide space with 400-40-10=350 pixels of space inside for your content, with 20 pixels of margin around it.<\/p>\n<p>What\u2019s the good news? Well, if you write your XHTML properly and include an appropriate DOCTYPE at the beginning of your page, everything from IE 6 and newer actually behaves like they should. So really, this is only a problem for IE 5.5 and below, assuming you\u2019re using good DOCTYPEs. For more in-depth detail on IE and the Box Model, I\u2019d suggest <a href=\"http:\/\/www.456bereastreet.com\/archive\/200612\/internet_explorer_and_the_css_box_model\/\">reading this fantastic article on 456 Berea Street<\/a>.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>A fundamental understanding of the CSS box model is essential to gaining a basic understanding of CSS in general. The good news is, if you can pack a box in real life, you can understand the CSS box model. And if you can\u2019t pack a box in real life due to some traumatic physical injury, you shouldn\u2019t have much of a problem, either. Also, I\u2019m sorry about bringing up the whole box thing. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/understanding-the-css-box-model\/\" title=\"Click to read 'Understanding the CSS Box Model'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[169],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}