{"id":27704,"date":"2020-10-30T12:00:16","date_gmt":"2020-10-30T16:00:16","guid":{"rendered":"https:\/\/blog.logrocket.com\/?p=27704"},"modified":"2021-07-26T15:07:54","modified_gmt":"2021-07-26T19:07:54","slug":"css-reference-guide-margin","status":"publish","type":"post","link":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/","title":{"rendered":"CSS Reference Guide: <code>margin<\/code>"},"content":{"rendered":"<!DOCTYPE html>\n<html><p>The CSS <code>margin<\/code> shorthand property is used to create space around an element outside of any defined borders. It defines the outermost portion of the box model.<\/p><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"487\" src=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png\" class=\"attachment-full size-full wp-post-image\" alt=\"CSS Reference Guide: Margin Shorthand Property\" srcset=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png 730w, https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn-300x200.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\">\n<h4><em>Jump ahead:<\/em><\/h4>\n<ul>\n<li><em><a href=\"#demo\">Demo<\/a><\/em><\/li>\n<li><em><a href=\"#syntax\">Syntax<\/a><\/em><\/li>\n<li><em><a href=\"#values\">Values<\/a><\/em>\n<ul>\n<li><em><a href=\"#one\">One value<\/a><\/em><\/li>\n<li><em><a href=\"#two\">Two values<\/a><\/em><\/li>\n<li><em><a href=\"#three\">Three values<\/a><\/em><\/li>\n<li><em><a href=\"#four\">Four values<\/a><\/em><\/li>\n<li><em><a href=\"#centering\">Centering elements<\/a><\/em><\/li>\n<li><em><a href=\"#negative\">Negative values<\/a><\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The <code>margin<\/code> property can be specified using lengths, percentages, and keywords such as <code>auto<\/code>. It can also accept negative values.<\/p>\n<hr>\n<h2 id=\"demo\">Demo<\/h2>\n<p class=\"codepen\" style=\"height: 500px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;\" data-height=\"500\" data-theme-id=\"dark\" data-default-tab=\"css,result\" data-user=\"kaperskyguru\" data-slug-hash=\"XWKdVBb\" data-pen-title=\"CSS Margin Example\">See the Pen <a href=\"https:\/\/codepen.io\/kaperskyguru\/pen\/XWKdVBb\"><br>\nCSS Margin Example<\/a> by Solomon Eseme (<a href=\"https:\/\/codepen.io\/kaperskyguru\">@kaperskyguru<\/a>)<br>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p><script async src=\"https:\/\/static.codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<hr>\n<h2 id=\"syntax\">Syntax<\/h2>\n<pre class=\"language\">div {\n  margin: &lt;length&gt; | &lt;percentage&gt; | auto\n}<\/pre>\n<p><code>margin<\/code> is a CSS shorthand property used to define up to four values for <code>margin-top<\/code>, <code>margin-right<\/code>, <code>margin-left<\/code>, and <code>margin-bottom<\/code>.<\/p>\n<pre class=\"language-css\">div {\n  margin: 2px 3px 4px 5px;\n}<\/pre>\n<p>The equivalent <code>margin<\/code> can be defined as below.<\/p>\n<pre class=\"language-css\">div {\n  margin-top: 2px;\n  margin-right: 3px;\n  margin-left: 5px;\n  margin-bottom: 4px;\n}<\/pre>\n<hr>\n<h2 id=\"values\">Values<\/h2>\n<p>The <code>margin<\/code> proprety can accept one to four values.<\/p>\n<h3 id=\"one\">One value<\/h3>\n<p>When one value is supplied to the <code>margin<\/code> shorthand property, it applies the same <code>margin<\/code> value to all four sides.<\/p>\n<pre class=\"language-css\">div {\n  margin: 5px;\n}\n \/\/ SAME AS\ndiv {\n  margin-top: 5px;\n  margin-right: 5px;\n  margin-bottom: 5px;\n  margin-left: 5px;\n}<\/pre>\n<hr>\n<h3 id=\"two\">Two values<\/h3>\n<p>When two values are supplied to the <code>margin<\/code> property, the first value is applied to the top and bottom margins, while the other is applied to the left and right.<\/p>\n<pre class=\"language-css\">div {\n  margin: 5px 3px;\n}\n \/\/ SAME AS\ndiv {\n  margin-top: 5px;\n  margin-right: 3px;\n  margin-left: 3px;\n  margin-bottom: 5px;\n}<\/pre>\n<hr>\n<h3 id=\"three\">Three values<\/h3>\n<p>When three values are supplied, the first value is applied to the top margin; the second value is applied to the left and right margins; and the third value is applied to the bottom margin.<\/p>\n<pre class=\"language-css\">div {\n  margin: 5px 3px 1px;\n}\n \/\/ SAME AS\ndiv {\n  margin-top: 5px;\n  margin-right: 3px;\n  margin-left: 3px;\n  margin-bottom: 1px;\n}<\/pre>\n<hr>\n<h3 id=\"four\">Four values<\/h3>\n<p>When four values are supplied, the values are applied in clockwise order. In other words: the first value is applied to the top margin; the second value is applied to the right margin; the third value is applied to the bottom; and the fourth value is applied to the left.<\/p>\n<pre class=\"language-css\">div {\n  margin: 5px 3px 1px 6px;\n}\n \/\/ SAME AS\ndiv {\n  margin-top: 5px;\n  margin-right: 3px;\n  margin-left: 6px;\n  margin-bottom: 1px;\n}<\/pre>\n<hr>\n<h3 id=\"centering\">Centering elements<\/h3>\n<p>With the use of the <code>auto<\/code> keyword, it is very easy to center elements in the container using <code>margin<\/code>.<\/p>\n<pre class=\"language-css\">div {\n  margin: 2em auto;  \/* top and bottom: 2em margin   *\/\n                    \/* Box is horizontally centered *\/\n}\n\ndiv {\n  margin: auto;    \/* top and bottom: 0 margin     *\/\n                  \/* Box is horizontally centered *\/\n}<\/pre>\n<hr>\n<h3 id=\"negative\">Negative values<\/h3>\n<p>When negative values are supplied to the <code>margin<\/code>, they pull the element in a particular direction rather than push it.<\/p>\n<p>For example, the snippet below will pull the element towards the <code>top<\/code> by 5x, towards the <code>right<\/code> by 3x, towards the <code>left<\/code> by 6px, and towards the <code>bottom<\/code> by 1px.<\/p>\n<pre class=\"language-css\">div {\n  margin: -5px -3px -1px -6px;\n}<\/pre>\n<p>&nbsp;<\/p>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>Our guide to the CSS margin shorthand property, including syntax, values, and a live demo.<\/p>\n","protected":false},"author":156415559,"featured_media":27737,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2109827],"tags":[2109867,2109707,2109826],"class_list":["post-27704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-reference-guides","tag-archive","tag-css","tag-reference-guide"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>CSS Reference Guide: margin - LogRocket Blog<\/title>\n<meta name=\"description\" content=\"The CSS margin shorthand property is used to create space around an element outside of any defined borders.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CSS Reference Guide: margin - LogRocket Blog\" \/>\n<meta property=\"og:description\" content=\"The CSS margin shorthand property is used to create space around an element outside of any defined borders.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/\" \/>\n<meta property=\"og:site_name\" content=\"LogRocket Blog\" \/>\n<meta property=\"article:author\" content=\"https:\/\/facebook.com\/solomon.eseme\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-30T16:00:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-26T19:07:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png\" \/>\n\t<meta property=\"og:image:width\" content=\"730\" \/>\n\t<meta property=\"og:image:height\" content=\"487\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Solomon Eseme\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/kaperskyguru\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Solomon Eseme\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/\",\"url\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/\",\"name\":\"CSS Reference Guide: margin - LogRocket Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.logrocket.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png\",\"datePublished\":\"2020-10-30T16:00:16+00:00\",\"dateModified\":\"2021-07-26T19:07:54+00:00\",\"author\":{\"@id\":\"https:\/\/blog.logrocket.com\/#\/schema\/person\/3fb2da23007344f6c9522f259b6c5d84\"},\"description\":\"The CSS margin shorthand property is used to create space around an element outside of any defined borders.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#primaryimage\",\"url\":\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png\",\"contentUrl\":\"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png\",\"width\":730,\"height\":487,\"caption\":\"CSS Reference Guide: Margin Shorthand Property\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.logrocket.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS Reference Guide: margin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.logrocket.com\/#website\",\"url\":\"https:\/\/blog.logrocket.com\/\",\"name\":\"LogRocket Blog\",\"description\":\"Resources to Help Product Teams Ship Amazing Digital Experiences\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.logrocket.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.logrocket.com\/#\/schema\/person\/3fb2da23007344f6c9522f259b6c5d84\",\"name\":\"Solomon Eseme\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.logrocket.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36252be8af8893209ad20c99cc25c20ef08175574993fb54b7e46132153c97e8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36252be8af8893209ad20c99cc25c20ef08175574993fb54b7e46132153c97e8?s=96&d=mm&r=g\",\"caption\":\"Solomon Eseme\"},\"description\":\"I am a software developer who is geared toward building high-performing and innovative products following best practices and industry standards. I also love writing about it at masteringbackend.com. Follow me on Twitter @kaperskyguru, on Facebook, onLinkedIn, and on About.Me at Solomon Eseme.\\\" Follow me: Twitter, Facebook, LinkedIn, about.me\",\"sameAs\":[\"https:\/\/masteringbackend.com\/\",\"https:\/\/facebook.com\/solomon.eseme\/\",\"https:\/\/www.linkedin.com\/in\/solomoneseme\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/kaperskyguru\/\"],\"url\":\"https:\/\/blog.logrocket.com\/author\/solomoneseme\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CSS Reference Guide: margin - LogRocket Blog","description":"The CSS margin shorthand property is used to create space around an element outside of any defined borders.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"CSS Reference Guide: margin - LogRocket Blog","og_description":"The CSS margin shorthand property is used to create space around an element outside of any defined borders.","og_url":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/","og_site_name":"LogRocket Blog","article_author":"https:\/\/facebook.com\/solomon.eseme\/","article_published_time":"2020-10-30T16:00:16+00:00","article_modified_time":"2021-07-26T19:07:54+00:00","og_image":[{"width":730,"height":487,"url":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png","type":"image\/png"}],"author":"Solomon Eseme","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/kaperskyguru\/","twitter_misc":{"Written by":"Solomon Eseme","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/","url":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/","name":"CSS Reference Guide: margin - LogRocket Blog","isPartOf":{"@id":"https:\/\/blog.logrocket.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#primaryimage"},"image":{"@id":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png","datePublished":"2020-10-30T16:00:16+00:00","dateModified":"2021-07-26T19:07:54+00:00","author":{"@id":"https:\/\/blog.logrocket.com\/#\/schema\/person\/3fb2da23007344f6c9522f259b6c5d84"},"description":"The CSS margin shorthand property is used to create space around an element outside of any defined borders.","breadcrumb":{"@id":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.logrocket.com\/css-reference-guide-margin\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#primaryimage","url":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png","contentUrl":"https:\/\/blog.logrocket.com\/wp-content\/uploads\/2020\/10\/lr-css-reference-guide-margin-nocdn.png","width":730,"height":487,"caption":"CSS Reference Guide: Margin Shorthand Property"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.logrocket.com\/css-reference-guide-margin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.logrocket.com\/"},{"@type":"ListItem","position":2,"name":"CSS Reference Guide: margin"}]},{"@type":"WebSite","@id":"https:\/\/blog.logrocket.com\/#website","url":"https:\/\/blog.logrocket.com\/","name":"LogRocket Blog","description":"Resources to Help Product Teams Ship Amazing Digital Experiences","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.logrocket.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.logrocket.com\/#\/schema\/person\/3fb2da23007344f6c9522f259b6c5d84","name":"Solomon Eseme","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.logrocket.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/36252be8af8893209ad20c99cc25c20ef08175574993fb54b7e46132153c97e8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36252be8af8893209ad20c99cc25c20ef08175574993fb54b7e46132153c97e8?s=96&d=mm&r=g","caption":"Solomon Eseme"},"description":"I am a software developer who is geared toward building high-performing and innovative products following best practices and industry standards. I also love writing about it at masteringbackend.com. Follow me on Twitter @kaperskyguru, on Facebook, onLinkedIn, and on About.Me at Solomon Eseme.\" Follow me: Twitter, Facebook, LinkedIn, about.me","sameAs":["https:\/\/masteringbackend.com\/","https:\/\/facebook.com\/solomon.eseme\/","https:\/\/www.linkedin.com\/in\/solomoneseme\/","https:\/\/x.com\/https:\/\/twitter.com\/kaperskyguru\/"],"url":"https:\/\/blog.logrocket.com\/author\/solomoneseme\/"}]}},"yoast_description":"The CSS margin shorthand property is used to create space around an element outside of any defined borders.","_links":{"self":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts\/27704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/users\/156415559"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/comments?post=27704"}],"version-history":[{"count":8,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts\/27704\/revisions"}],"predecessor-version":[{"id":27889,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/posts\/27704\/revisions\/27889"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/media\/27737"}],"wp:attachment":[{"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/media?parent=27704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/categories?post=27704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.logrocket.com\/wp-json\/wp\/v2\/tags?post=27704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}