{"id":25,"date":"2012-05-05T00:00:30","date_gmt":"2012-05-05T00:00:30","guid":{"rendered":"https:\/\/learnwebcode.com\/?p=25"},"modified":"2016-03-29T01:37:05","modified_gmt":"2016-03-29T01:37:05","slug":"css-box-model-margin","status":"publish","type":"post","link":"https:\/\/learnwebcode.com\/css-box-model-margin\/","title":{"rendered":"CSS Lesson 5: Understanding The Box Model Part 2: Margin"},"content":{"rendered":"<p>As you begin designing layouts with CSS you will inevitably need to create spacing between elements; thus, today\u2019s lesson is regarding the \u201cmargin\u201d property.\u00a0 Let\u2019s begin with a quick image to illustrate the margin property in action:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif\" alt=\"CSS Margin Graph\" \/><\/p>\n<p>Margin is space added to the outside of an element (past the element\u2019s edge, or border).\u00a0 In some instances, Padding and Margin can be used to achieve a similar affect, but it\u2019s important to understand the difference.\u00a0 Padding adds space to the <strong>inside<\/strong> of an element, while margin adds space to the <strong>outside<\/strong> of an element.<\/p>\n<p>Let\u2019s code a simple example of margin in action.\u00a0 Begin with the following HTML code, and place it in a <a title=\"How To Create an XHTML Starter File\" href=\"https:\/\/learnwebcode.com\/how-to-create-and-save-your-first-xhtml-file-by-hand\/\">blank XHTML starter file<\/a>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;div id=&quot;box-a&quot;&gt;\r\n&lt;p&gt;Box A&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div id=&quot;box-b&quot;&gt;\r\n&lt;p&gt;Box B&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Next, let\u2019s give these two boxes a width, background-color, a border and padding.\u00a0 Add the following code to your CSS file:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">#box-a {\r\n    width: 300px;\r\n    padding: 15px;\r\n    background-color: orange;\r\n    border: 1px solid black;\r\n}\r\n\r\n#box-b {\r\n    width: 300px;\r\n    padding: 15px;\r\n    background-color: gray;\r\n    border: 1px solid black;\r\n}\r\n<\/pre>\n<p>When viewed in a web browser, our example should look like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/learnwebcode.com\/images\/lessons\/margin-screenshot-1.gif\" alt=\"CSS Margin screenshot\" \/><\/p>\n<p>Now imagine we want to create 10 pixels of spacing between the two boxes.\u00a0 We would achieve this by adding bottom-margin to <strong>box-a<\/strong>:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n#box-a {\r\n    margin-bottom: 10px;\r\n    width: 300px;\r\n    padding: 15px;\r\n    background-color: orange;\r\n    border: 1px solid black;\r\n}\r\n<\/pre>\n<p>When refreshed in our web browser, our page should now look like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/learnwebcode.com\/images\/lessons\/margin-screenshot-2.gif\" alt=\"CSS Margin Screenshot 2\" \/><\/p>\n<p>You may wonder, &#8220;Could we have also added <strong>margin-top<\/strong> to <strong>box-b<\/strong>?&#8221;\u00a0 Let&#8217;s do that now; let&#8217;s double the space between the boxes by adding another 10 pixels of margin to <strong>box-b<\/strong>:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n#box-b {\r\n    margin-top: 10px;\r\n    width: 300px;\r\n    padding: 15px;\r\n    background-color: orange;\r\n    border: 1px solid black;\r\n}\r\n<\/pre>\n<p>When you refresh the page in your web browser, you&#8217;ll notice that the spacing didn&#8217;t double; it stayed exactly the same.  This is because two margins are touching, so one <strong>collapsed<\/strong>.  Essentially, when two margins touch the larger margin is used, and the smaller disappears (or collapses).<\/p>\n<p>To fully illustrate this point, let&#8217;s change the <strong>margin-top<\/strong> of <strong>box-b<\/strong> from 10 pixels to <strong>11<\/strong>.  When we refresh the page in our web browser we&#8217;ll notice that the spacing hasn&#8217;t ballooned to 21 pixels, but instead has increased by <strong>only one pixel<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/learnwebcode.com\/images\/lessons\/margin-screenshot-3.gif\" alt=\"CSS Margin Screenshot 3\" \/><\/p>\n<p>The browser uses the 11 pixel margin of <strong>box-b<\/strong> and collapses the smaller 10 pixel margin of <strong>box-a<\/strong>.<\/p>\n<p>That&#8217;s all for today&#8217;s lesson.\u00a0 To recap, you now know:<\/p>\n<ul>\n<li>The difference between margin and padding<\/li>\n<li>How to use margins to create spacing around elements<\/li>\n<li>How the smaller of two touching margins collapses<\/li>\n<\/ul>\n<p>If you prefer to watch video lessons instead of reading written lessons check out <a href=\"\/web-design-for-beginners-real-world-coding-html-css\/\">my 8 hour video course<\/a> and learn pro-level HTML, CSS, and responsive design.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you begin designing layouts with CSS you will inevitably need to create spacing between elements; thus, today\u2019s lesson is regarding the \u201cmargin\u201d property.\u00a0 Let\u2019s begin with a quick image to illustrate the margin property in action: Margin is space added to the outside of an element (past the element\u2019s edge, or border).\u00a0 In some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-css-lessons"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>CSS Lesson 5: Understanding The Box Model Part 2: Margin - LearnWebCode<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnwebcode.com\/css-box-model-margin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CSS Lesson 5: Understanding The Box Model Part 2: Margin - LearnWebCode\" \/>\n<meta property=\"og:description\" content=\"As you begin designing layouts with CSS you will inevitably need to create spacing between elements; thus, today\u2019s lesson is regarding the \u201cmargin\u201d property.\u00a0 Let\u2019s begin with a quick image to illustrate the margin property in action: Margin is space added to the outside of an element (past the element\u2019s edge, or border).\u00a0 In some [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnwebcode.com\/css-box-model-margin\/\" \/>\n<meta property=\"og:site_name\" content=\"LearnWebCode\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-05T00:00:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-03-29T01:37:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif\" \/>\n<meta name=\"author\" content=\"Brad\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brad\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/\"},\"author\":{\"name\":\"Brad\",\"@id\":\"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6\"},\"headline\":\"CSS Lesson 5: Understanding The Box Model Part 2: Margin\",\"datePublished\":\"2012-05-05T00:00:30+00:00\",\"dateModified\":\"2016-03-29T01:37:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/\"},\"wordCount\":492,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6\"},\"image\":{\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif\",\"articleSection\":[\"CSS Lessons\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/learnwebcode.com\/css-box-model-margin\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/\",\"url\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/\",\"name\":\"CSS Lesson 5: Understanding The Box Model Part 2: Margin - LearnWebCode\",\"isPartOf\":{\"@id\":\"https:\/\/learnwebcode.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif\",\"datePublished\":\"2012-05-05T00:00:30+00:00\",\"dateModified\":\"2016-03-29T01:37:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnwebcode.com\/css-box-model-margin\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage\",\"url\":\"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif\",\"contentUrl\":\"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnwebcode.com\/css-box-model-margin\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learnwebcode.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS Lesson 5: Understanding The Box Model Part 2: Margin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnwebcode.com\/#website\",\"url\":\"https:\/\/learnwebcode.com\/\",\"name\":\"LearnWebCode\",\"description\":\"Become a Web Developer\",\"publisher\":{\"@id\":\"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnwebcode.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6\",\"name\":\"Brad\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg\",\"url\":\"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg\",\"contentUrl\":\"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg\",\"width\":400,\"height\":400,\"caption\":\"Brad\"},\"logo\":{\"@id\":\"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg\"},\"url\":\"https:\/\/learnwebcode.com\/author\/lwcadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CSS Lesson 5: Understanding The Box Model Part 2: Margin - LearnWebCode","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnwebcode.com\/css-box-model-margin\/","og_locale":"en_US","og_type":"article","og_title":"CSS Lesson 5: Understanding The Box Model Part 2: Margin - LearnWebCode","og_description":"As you begin designing layouts with CSS you will inevitably need to create spacing between elements; thus, today\u2019s lesson is regarding the \u201cmargin\u201d property.\u00a0 Let\u2019s begin with a quick image to illustrate the margin property in action: Margin is space added to the outside of an element (past the element\u2019s edge, or border).\u00a0 In some [&hellip;]","og_url":"https:\/\/learnwebcode.com\/css-box-model-margin\/","og_site_name":"LearnWebCode","article_published_time":"2012-05-05T00:00:30+00:00","article_modified_time":"2016-03-29T01:37:05+00:00","og_image":[{"url":"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif","type":"","width":"","height":""}],"author":"Brad","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Brad","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#article","isPartOf":{"@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/"},"author":{"name":"Brad","@id":"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6"},"headline":"CSS Lesson 5: Understanding The Box Model Part 2: Margin","datePublished":"2012-05-05T00:00:30+00:00","dateModified":"2016-03-29T01:37:05+00:00","mainEntityOfPage":{"@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/"},"wordCount":492,"commentCount":5,"publisher":{"@id":"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6"},"image":{"@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage"},"thumbnailUrl":"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif","articleSection":["CSS Lessons"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/learnwebcode.com\/css-box-model-margin\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/","url":"https:\/\/learnwebcode.com\/css-box-model-margin\/","name":"CSS Lesson 5: Understanding The Box Model Part 2: Margin - LearnWebCode","isPartOf":{"@id":"https:\/\/learnwebcode.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage"},"image":{"@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage"},"thumbnailUrl":"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif","datePublished":"2012-05-05T00:00:30+00:00","dateModified":"2016-03-29T01:37:05+00:00","breadcrumb":{"@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnwebcode.com\/css-box-model-margin\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#primaryimage","url":"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif","contentUrl":"https:\/\/learnwebcode.com\/images\/lessons\/css-margin-graph.gif"},{"@type":"BreadcrumbList","@id":"https:\/\/learnwebcode.com\/css-box-model-margin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnwebcode.com\/"},{"@type":"ListItem","position":2,"name":"CSS Lesson 5: Understanding The Box Model Part 2: Margin"}]},{"@type":"WebSite","@id":"https:\/\/learnwebcode.com\/#website","url":"https:\/\/learnwebcode.com\/","name":"LearnWebCode","description":"Become a Web Developer","publisher":{"@id":"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnwebcode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/learnwebcode.com\/#\/schema\/person\/db825ebc5a08e39b01de31771e4c7fe6","name":"Brad","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg","url":"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg","contentUrl":"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg","width":400,"height":400,"caption":"Brad"},"logo":{"@id":"https:\/\/learnwebcode.com\/wp-content\/uploads\/0UWl0Tet_400x400.jpg"},"url":"https:\/\/learnwebcode.com\/author\/lwcadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":9,"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":1012,"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/posts\/25\/revisions\/1012"}],"wp:attachment":[{"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnwebcode.com\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}