Skip to content
Laviyx Learn

Try

CSS course menu

CSS Margin and Padding: Shorthand, auto Centering and Negative Margins

Learn CSS margin and padding: individual sides, the one-to-four value shorthand, centering blocks with margin auto, negative margins, logical properties such as margin-inline, and the gap property for spacing in Flexbox and Grid.

Beginner5 min readUpdated
On this page

Padding is the space inside an element, between its content and its border. Margin is the space outside it, between the element and its neighbours. Both use the same syntax.

Individual sides#

CSS
.card {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 10px;
  padding-left: 20px;

  margin-bottom: 24px;
}

The shorthand: one to four values#

The margin and padding shorthands take one to four values, going clockwise from the top:

ValueMeaning
padding: 10px;All four sides 10px
padding: 10px 20px;Top and bottom 10px, left and right 20px
padding: 10px 20px 30px;Top 10px, left and right 20px, bottom 30px
padding: 10px 20px 30px 40px;Top, right, bottom, left

A way to remember the four-value order: TRouBLe (Top, Right, Bottom, Left).

CSS
div { background: #cfe3ff; border: 1px solid #2f5fe0; margin-bottom: 10px; }
.one { padding: 10px; }
.two { padding: 10px 40px; }
.four { padding: 4px 8px 24px 60px; }
HTML
<div class="one">padding: 10px</div>
<div class="two">padding: 10px 40px</div>
<div class="four">padding: 4px 8px 24px 60px</div>
Result

Centering a block with margin: auto#

A block element with a set width (or max-width) can be centered horizontally with auto left and right margins. The browser splits the leftover space equally:

CSS
.container {
  max-width: 300px;
  margin: 0 auto;
  padding: 12px;
  background: #fff3c4;
}
HTML
<div class="container">I am centered in the page.</div>
Result

This is the classic way to center a page's main column. margin: auto does not center vertically in normal flow; for that, see Centering Anything.

Negative margins#

Margins can be negative, pulling an element towards or over its neighbours:

CSS
.photo { height: 60px; background: #9cc9a8; }
.badge {
  margin: -20px 0 0 16px;
  display: inline-block;
  padding: 6px 12px;
  background: crimson;
  color: white;
  border-radius: 6px;
}
HTML
<div class="photo"></div>
<div class="badge">Overlapping badge</div>
Result

Padding cannot be negative.

Logical properties#

Instead of physical directions (top, right, bottom, left), logical properties follow the direction of the text. In left-to-right languages, they map like this:

LogicalPhysical (left-to-right text)
margin-block-start / margin-block-endmargin-top / margin-bottom
margin-inline-start / margin-inline-endmargin-left / margin-right
padding-block: 10pxtop and bottom
padding-inline: 20pxleft and right

In right-to-left languages such as Arabic and Urdu, margin-inline-start becomes the right margin automatically. margin-inline: auto is a neat way to center a block.

gap: spacing between items#

In Flexbox and Grid layouts, gap puts space between items without adding it on the outside edges, which is simpler than margins on each item:

CSS
.row { display: flex; gap: 12px; }
.row span { padding: 8px 12px; background: #cfe3ff; }
HTML
<div class="row"><span>One</span><span>Two</span><span>Three</span></div>
Result

Margin or padding?#

  • Use padding for space inside a box: between a button's text and its edge, or a card's content and its border. Padding is clickable and shows the background.
  • Use margin (or gap) for space between boxes.

Practice#

Practice exercise

Build a centered article column at most 520px wide with 24px padding on the left and right and 16px on the top and bottom (one shorthand declaration), and paragraphs with no top margin and 12px bottom margin.

Show solution
CSS
article {
  max-width: 520px;
  margin: 0 auto;
  padding: 16px 24px;
  background: #f6f7f9;
}
article p { margin: 0 0 12px; }
HTML
<article>
  <p>The first paragraph sits right at the top of the padding.</p>
  <p>Each paragraph is followed by 12px of space.</p>
</article>
Result

Key takeaway#

Finished this lesson?