{"id":743,"date":"2008-06-23T19:07:56","date_gmt":"2008-06-23T19:07:56","guid":{"rendered":"http:\/\/www.mrc-productivity.com\/techblog\/?p=743"},"modified":"2023-05-12T13:24:13","modified_gmt":"2023-05-12T19:24:13","slug":"the-css-box-model","status":"publish","type":"ht_kb","link":"https:\/\/www.mrc-productivity.com\/techblog\/?ht_kb=the-css-box-model","title":{"rendered":"The CSS Box Model"},"content":{"rendered":"<p>In order to understand positioning with CSS, you must first understand the box model, but before explaining the box model, here are a few helpful definitions that I will be referring to throughout this post:<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">&nbsp;&#8212; <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">The Viewport<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">The viewport is the area on a browser that displays the webpage.<span style=\"\">&nbsp; <\/span>Imagine that your browser is a window, with the outer edges being frames and the area that displays the webpage (the viewport) as the glass.<span style=\"\">&nbsp; <\/span>The viewport is separate from the page itself, so even if the page is scrolled, the viewport is always the area that you can see. <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">The Initial Containing Block<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">The entire space assigned to your document.<span style=\"\">&nbsp; <\/span>The initial containing block is set by the web designer as the size of the web page.<span style=\"\">&nbsp; <\/span>This includes parts of the page that users have to scroll to see. <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Containing Blocks<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">A containing block has another element inside of it.<span style=\"\">&nbsp; <\/span>In the example below, the div named \u201ccontainer\u201d is the containing block for the div named \u201celement\u201d.<span style=\"\"> <br \/><\/span><\/span><\/p>\n<p class=\"MsoNormal\">\n<div align=left>\n<div style=\"margin-bottom:2px;text-align:left;width:630px\">Code:<\/div>\n<pre style=\"margin:0px; padding:6px; border:1px inset; border-bottom:1px solid #E8E8E8; border-right:1px solid #E8E8E8; width:630px; height:114px; overflow:auto;text-align:left;\">\r\n\r\n &lt;div class=\u201dcontainer\u201d&gt;\r\n                &lt;div class=\u201delement\u201d&gt;\r\n                &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n <\/pre>\n<\/div>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Normal Flow<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">The normal flow of the document is the order in which the elements are displayed.<span style=\"\">&nbsp; <\/span>It is the way a document would appear with no positioning or floated elements.<span style=\"\">&nbsp; <\/span>The content will start at the top of the page and flow downward. <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Block-level elements<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Elements that are formatted visually as blocks.<span style=\"\">&nbsp; <\/span>Block elements automatically create a line break, unless given the rule, \u201cdisplay:inline;\u201d, which will cause them to be treated as an inline element.<span style=\"\">&nbsp; <\/span>Examples of block-level elements are: <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\"><\/p>\n<div align=left>\n<div style=\"margin-bottom:2px;text-align:left;width:630px\">Code:<\/div>\n<pre style=\"margin:0px; padding:6px; border:1px inset; border-bottom:1px solid #E8E8E8; border-right:1px solid #E8E8E8; width:630px; height:66px; overflow:auto;text-align:left;\">\r\n\r\n&lt;div&gt;, &lt;h1&gt;, &lt;p&gt;\r\n <\/pre>\n<\/div>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Inline-level elements<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Inline elements do not form blocks of content and are displayed visually in a line.<span style=\"\">&nbsp; <\/span>They do not create line breaks.<span style=\"\">&nbsp; <\/span>Examples of inline level elements are:<\/span><\/p>\n<p class=\"MsoNormal\" style=\"border: medium none ; padding: 0in;\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\"><\/p>\n<div align=left>\n<div style=\"margin-bottom:2px;text-align:left;width:630px\">Code:<\/div>\n<pre style=\"margin:0px; padding:6px; border:1px inset; border-bottom:1px solid #E8E8E8; border-right:1px solid #E8E8E8; width:630px; height:66px; overflow:auto;text-align:left;\">\r\n\r\n&lt;b&gt;, &lt;em&gt;, &lt;span&gt;\r\n<\/pre>\n<\/div>\n<p><\/span><\/p>\n<div style=\"border-style: none none solid; border-color: -moz-use-text-color -moz-use-text-color windowtext; border-width: medium medium 1pt; padding: 0in 0in 1pt;\">    <\/div>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">&nbsp;The Box Model<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Take a look at the image below.<span style=\"\">&nbsp; <\/span>I have taken it directly from the W3 site, in order to illustrate the box model, and why it\u2019s important to positioning with style sheets.<\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">&nbsp;<img decoding=\"async\" src=\"https:\/\/www.mrc-productivity.com\/images\/forumimages\/boxmodel.jpg\"><\/span><\/b><br \/><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\"><!--[if gte vml 1]>                                                  <![endif]--><!--[if !vml]--><!--[endif]--><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">For display purposes, every block-level element in a document is considered to be a rectangular box.<span style=\"\">&nbsp; <\/span>As you can see from the diagram above, each box is comprised of 4 parts:<\/span><\/p>\n<ul style=\"margin-top: 0in;\" type=\"disc\">\n<li class=\"MsoNormal\" style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Content \u2013 This could      include text or an image.<\/span><\/li>\n<li class=\"MsoNormal\" style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Padding \u2013 Invisible      area that defines the distance from the border of the box to the content.<\/span><\/li>\n<li class=\"MsoNormal\" style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Border \u2013 The area      surrounding the content and padding.<\/span><\/li>\n<li class=\"MsoNormal\" style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Margin \u2013 Invisible      area that defines the space between the outside border of the box and      other elements on the page.<\/span><\/li>\n<\/ul>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">It is equally important to understand how the width of the box is calculated, and why defining only \u201cwidth\u201d isn\u2019t enough.<span style=\"\">&nbsp; <\/span>Here is the equation to determine the width of a box:<\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">width<\/span><\/b><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\"> = margin-left + border(left) + padding-left + content width + padding-right + border(right) + margin-right <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">For example, look at the code below and try to determine how wide the box will be:<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">&nbsp;<\/p>\n<div align=left>\n<div style=\"margin-bottom:2px;text-align:left;width:630px\">Code:<\/div>\n<pre style=\"margin:0px; padding:6px; border:1px inset; border-bottom:1px solid #E8E8E8; border-right:1px solid #E8E8E8; width:630px; height:178px; overflow:auto;text-align:left;\">\r\n\r\n.divtest           {\r\n                                position: relative;\r\n                                width: 100px;\r\n                                height: 75px;\r\n                                padding: 10px;\r\n                                margin: 5px;\r\n                                border: solid 1px black;\r\n                         }\r\n <\/pre>\n<\/div>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">According to the box model equation, this box would be &#8216;132px&#8217; wide, as illustrated by the image below. <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Width = margin-left(5) + border-left(1) + padding-left(10) + content width(100) + padding-right(10) + border-right(1)+maring-right(5) = 132<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">&nbsp;<img decoding=\"async\" src=\"https:\/\/www.mrc-productivity.com\/images\/forumimages\/cssboxmodel.jpg\"><br \/><!--[if gte vml 1]>   <![endif]--><!--[if !vml]--><!--[endif]--><\/span><b style=\"\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\"><br \/>Height<\/span><\/b><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">Height is calculated differently than width.<span style=\"\">&nbsp; <\/span>In the normal flow of a document, vertical margins are collapsed.<span style=\"\">&nbsp; <\/span>That means when a box is placed on top of another, the distance between the two is equal to the largest margin, as illustrated by the image below:<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">&nbsp;<img decoding=\"async\" src=\"https:\/\/www.mrc-productivity.com\/images\/forumimages\/boxmodelheight.jpg\"> <br \/><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 11pt; font-family: &quot;Arial Narrow&quot;;\">If these two boxes had been placed next to each other, the margins would be added together to create a total of 15px.<span style=\"\">&nbsp; <\/span>However, since they are placed on top of each other, the margins are collapsed and only the largest margin is used.<\/p>\n<p>It is very important that you understand the box model before moving into positioning.<span style=\"\">&nbsp; <\/span>Whenever you place a block-level element on a page, you must be aware of the 4 attributes explained above which determine placement and size.<span style=\"\">&nbsp; <\/span><b>For information on positioning with CSS, please read the next post in the forum entitled, &#8220;Positioning with CSS&#8221;.<\/b><b style=\"\"><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to understand positioning with CSS, you must first understand the box model, but before explaining the box model, here are a few helpful definitions that I will be referring to throughout this post: &nbsp;&#8212; The Viewport The viewport is the area on a browser that displays the webpage.&nbsp;&#8230;<\/p>\n","protected":false},"author":2,"comment_status":"closed","ping_status":"open","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[],"ht-kb-tag":[],"class_list":["post-743","ht_kb","type-ht_kb","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=743"}],"version-history":[{"count":3,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/743\/revisions"}],"predecessor-version":[{"id":833,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/743\/revisions\/833"}],"wp:attachment":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=743"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fht-kb-category&post=743"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fht-kb-tag&post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}