{"id":2060,"date":"2016-11-03T16:24:28","date_gmt":"2016-11-03T16:24:28","guid":{"rendered":"http:\/\/cssreset.com\/?p=2060"},"modified":"2016-11-03T16:24:28","modified_gmt":"2016-11-03T16:24:28","slug":"margins-vs-padding","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/margins-vs-padding\/","title":{"rendered":"Margins Vs. Padding"},"content":{"rendered":"<p>The difference between margin and padding is an important concept for new developers to grasp. While margin and padding both provide space in relation to elements, they do so in different ways. Padding provides space\u00a0<em>within<\/em> the element, meaning that it can add space between an element and the element that is inside of it, or it can be used to expand an element, making it larger vertically, horizontally, or both.<\/p>\n<p>The best way to understand the differences between margins and padding is to see them in action. Below is an image of a div (#main) with another div (.inner) inside of it with NO margins and padding applied to either:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2064 aligncenter\" alt=\"Screen Shot 2016-10-31 at 12.51.21 PM\" src=\"https:\/\/cssdeck.com\/blog\/\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.21-PM-1024x565.png\" width=\"1024\" height=\"565\" srcset=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.21-PM-1024x565.png 1024w, https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.21-PM-300x165.png 300w, https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.21-PM-180x99.png 180w, https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.21-PM.png 1496w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>The #main div has the solid black border, and the .inner div has the purple dashed one. You can see that the elements are practically on top of each other, with no space separating the left and top borders of each div.<\/p>\n<p>One way to create space between the two divs is to add some padding to the #main div. This will add space\u00a0<em>within<\/em> the element and therefore push the .inner div away from the left and top borders. Let&#8217;s try it by adding 30px of padding to the top and left borders of the div. Here&#8217;s what that code would look like:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\r\n\r\n\t\t<div class=\"toolbar\">\r\n\r\n\t\t<div class=\"view-different-container\">\r\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\r\n\t\t\t\t\t<\/div>\r\n\r\n\t\t<div class=\"language-name\">css<\/div>\r\n\r\n\t\t\r\n\t\t<br clear=\"both\">\r\n\r\n\t<\/div>\r\n\t\r\n\t<div class=\"code\">\r\n\t\t<ol class=\"css\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #cc00cc\">#main<\/span><span style=\"color: #00AA00\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">padding<\/span><span style=\"color: #00AA00\">:<\/span> <span style=\"color: #933\">30px<\/span> <span style=\"color: #cc66cc\">0<\/span> <span style=\"color: #cc66cc\">0<\/span> <span style=\"color: #933\">30px<\/span><span style=\"color: #00AA00\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #00AA00\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;img class<span style=\"color: #00AA00\">=<\/span><span style=\"color: #ff0000\">&quot;size-large wp-image-2065 aligncenter&quot;<\/span> alt<span style=\"color: #00AA00\">=<\/span><span style=\"color: #ff0000\">&quot;Screen Shot 2016-10-31 at 12.48.05 PM&quot;<\/span> src<span style=\"color: #00AA00\">=<\/span><span style=\"color: #ff0000\">&quot;https:\/\/cssdeck.com\/blog\/\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.48.05-PM-1024x599.png&quot;<\/span> <span style=\"color: #000000;font-weight: bold\">width<\/span><span style=\"color: #00AA00\">=<\/span><span style=\"color: #ff0000\">&quot;1024&quot;<\/span> <span style=\"color: #000000;font-weight: bold\">height<\/span><span style=\"color: #00AA00\">=<\/span><span style=\"color: #ff0000\">&quot;599&quot;<\/span> \/<span style=\"color: #00AA00\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">You can see now how adding <span style=\"color: #000000;font-weight: bold\">padding<\/span> to the <span style=\"color: #cc00cc\">#main<\/span> div helps to create space between <span style=\"color: #cc00cc\">#main<\/span> and <span style=\"color: #6666ff\">.inner<\/span>. If you don<span style=\"color: #ff0000\">'t want to use the padding property, you can also create this space using margins, but the margins will have to be applied to the .inner div. By assigning margins to this div, you'<\/span>re defining how much space you want between the <span style=\"color: #6666ff\">.inner<\/span> div and the next-nearest element<span style=\"color: #00AA00\">&#040;<\/span>s<span style=\"color: #00AA00\">&#041;<\/span>. Let<span style=\"color: #ff0000\">'s center the .inner div within the #main and give it a small top margin of 10px to leave a little bit of space between the top borders (bonus tip: you can center any block element by giving it left and right margins of auto). Here'<\/span>s the code<span style=\"color: #00AA00\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #00AA00\">&#091;<\/span>css<span style=\"color: #00AA00\">&#093;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">.inner<span style=\"color: #00AA00\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">margin<\/span><span style=\"color: #00AA00\">:<\/span> <span style=\"color: #933\">10px<\/span> <span style=\"color: #993333\">auto<\/span> <span style=\"color: #cc66cc\">0<\/span> <span style=\"color: #993333\">auto<\/span><span style=\"color: #00AA00\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #00AA00\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\r\n\r\n<\/div>\r\n\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-large wp-image-2063\" alt=\"Screen Shot 2016-10-31 at 12.51.45 PM\" src=\"https:\/\/cssdeck.com\/blog\/\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.45-PM-1024x563.png\" width=\"1024\" height=\"563\" srcset=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.45-PM-1024x563.png 1024w, https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.45-PM-300x165.png 300w, https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.45-PM-180x99.png 180w, https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2016\/10\/Screen-Shot-2016-10-31-at-12.51.45-PM.png 1500w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/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>The difference between margin and padding is an important concept for new developers to grasp. While margin and padding both provide space in relation to elements, they do so in different ways. Padding provides space\u00a0within the element, meaning that it [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/margins-vs-padding\/\" title=\"Click to read 'Margins Vs. Padding'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":2063,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6,36,7,42,1],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2060"}],"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=2060"}],"version-history":[{"count":2,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2060\/revisions"}],"predecessor-version":[{"id":2066,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2060\/revisions\/2066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/2063"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}