{"id":56,"date":"2008-03-17T07:00:03","date_gmt":"2008-03-17T12:00:03","guid":{"rendered":"http:\/\/cssnewbie.com\/margins-and-padding-use\/"},"modified":"2008-03-17T07:00:03","modified_gmt":"2008-03-17T12:00:03","slug":"margins-and-padding-use","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/margins-and-padding-use\/","title":{"rendered":"When to Use Margins and Padding"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/03\/lillypad-400.jpg' alt='' title='Photo by Roger Smith. Used under a Creative Commons license.' \/><\/p>\n<p>There are two ways in CSS to create space around your elements: margins and padding. In most usage scenarios, they are functionally identical \u2013 for example, if you add 20 pixels of either margin or padding to the left side of an element, that element will generally move 20 pixels to the right on the screen. But in truth, they do behave in slightly different ways. So when should you use margins, and when would padding be more appropriate?<\/p>\n<p><strong>Use margins when:<\/strong><\/p>\n<ul>\n<li>You want your spacing to appear outside of the \u201cbox\u201d created by a border property. Margins lie outside of borders, so you\u2019d use them if you want your border to stay in one place. This can be useful if you have, for example, a sidebar with a border that you want to move away from your main content area.<\/li>\n<li>You don\u2019t want your background color or image to invade your personal space. Background colors and images stop at the border, so if you\u2019d rather keep your backgrounds out of the space you\u2019re making, margins are the property you want.<\/li>\n<li>You\u2019d like collapsible space on the top and bottom of your element. Top and bottom margins behave differently than side margins in that if two margins are atop one another, they collapse to the size of the largest margin set. For example, if I set paragraph to have a top margin of 20 pixels and a bottom margin of 15 pixels, I\u2019ll only have 20 pixels of space <em>total<\/em> between paragraphs (the two margins collapse into one another, and the largest wins out).<\/li>\n<\/ul>\n<p>Padding, on the other hand, behaves very differently in these scenarios. Thus, you\u2019d want to <strong>use padding when:<\/strong><\/p>\n<ul>\n<li>You want all of the space you\u2019re creating to be inside your border. Padding is contained within your borders, so it\u2019s useful for pushing your borders away from the content inside of your element.<\/li>\n<li>You need your background color\/image to continue on into the space you\u2019re creating. Your background will continue on behind your padding, so only use it if you want your background to peek out.<\/li>\n<li>You want your top and bottom space to behave more rigidly. For example, if you set the paragraphs in your document to have a top padding of 20 pixels and a bottom padding of 15 pixels, then any time you had two paragraphs in a row, they\u2019d actually have a total of 35 pixels of space between them.<\/li>\n<\/ul>\n<p>So as you can see, there are important differences between margins and padding that you should take into consideration when choosing which to use to move elements around the page. However, in those cases where either margins or padding could be used to the same effect, a lot of the decision comes down to personal preference. <\/p>\n<p>For example, I tend to use margins almost exclusively when I\u2019m trying to create space between two elements. In my mind, margins are used for creating space <em>between<\/em> elements, while padding is used for creating space <em>within<\/em> elements. Therefore, I use margins to move disparate elements apart, and padding to create a little breathing room within the elements themselves.<\/p>\n<p>However, a case could also be made for using padding for effecting space between elements. From time to time, Internet Explorer 6 will have trouble with margins \u2013 there\u2019s something out there known as the <a href=\"http:\/\/www.positioniseverything.net\/explorer\/doubled-margin.html\">doubled-margin float bug<\/a> that can wreak all sorts of havoc with pages laid out with margins when they\u2019re loaded in our favorite \u201cspecial\u201d browser. Using padding almost exclusively does create a way around this problem. I personally tend to just use a CSS hack or two to make IE behave itself when we\u2019re in public, but as  I said, this can be an effective argument for ubiquitous padding.<\/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>\n<p>There are two ways in CSS to create space around your elements: margins and padding. In most usage scenarios, they are functionally identical \u2013 for example, if you add 20 pixels of either margin or padding to the left side [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/margins-and-padding-use\/\" title=\"Click to read 'When to Use Margins and Padding'\">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":[187],"tags":[300,301,118,349],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/56"}],"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=56"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}