What is padding and margin?

Padding is the space inside an element, between its content and its border. Margin is the space outside the border, between the element and the elements around it. Both are CSS properties, set in pixels (px), rem or percent.

An easy way to remember it: padding makes a box roomier on the inside, and its background color fills the padding. Margin pushes other boxes away, and it is always transparent.

4 · Margin3 · Border2 · Padding1 · Content24px32px
  1. Content: the text, picture or button itself.
  2. Padding: the space between the content and the border. It has the same background color as the element.
  3. Border: the line around the padding. It can also be 0 pixels wide, so you do not see it.
  4. Margin: the transparent space outside the border that keeps other elements at a distance.
Example: padding 24px, border 2px, margin 32px.

The box model: content, padding, border, margin

Every element on a web page, such as a heading, a button or a section, is a rectangular box with these four layers. The browser draws them from the inside out. Padding and margin are the two you use most to give a page its spacing.

Padding and margin in CSS

You set both in your stylesheet. In this example the card gets 24 pixels of space inside its border and keeps 32 pixels of distance to everything around it:

.card {  padding: 24px; /* space inside the border */  border: 2px solid #E42919;  margin: 32px; /* space outside the border */}

One value applies to all four sides. With more values you set the sides apart, always in the order top, right, bottom, left (clockwise):

  • padding: 24px; 24px on all four sides.
  • padding: 12px 24px; 12px top and bottom, 24px left and right.
  • padding: 8px 16px 12px 20px; top 8px, right 16px, bottom 12px, left 20px.
  • margin-top: 40px; only one side: padding-top, padding-right, padding-bottom and padding-left work the same way.

Padding vs margin

PaddingMargin
WhereInside the borderOutside the border
Background colorFills the paddingAlways transparent
ClickableYes, part of the element (a bigger button)No
Negative valuesNot allowedAllowed: the element moves closer or overlaps
CollapsingNeverVertical margins of two blocks merge into the larger one
Use it forSpace between content and its edge, such as text in a buttonSpace between elements, such as between two sections

When do you use padding and when margin?

Use padding when the space belongs to the element itself: text that should not touch the edge of a colored block, or a button that needs a larger area to click or tap. Use margin when you want distance between two separate elements, such as two cards or a heading and the text below it.

Spacing also helps people on a phone: enough padding makes buttons easy to tap, and enough margin keeps a page calm and readable. In the WEMASY website builder you set the padding of a section in its settings, also separately for tablet and phone, without writing any CSS.

Frequently asked questions

What is the difference between padding and margin?

Padding is the space inside an element, between its content and its border, and it takes the element's background color. Margin is the space outside the border, between the element and its neighbors, and it is always transparent.

Does padding make an element bigger?

By default, yes: the width you set is for the content only, and the padding and border are added to it. With box-sizing: border-box the padding and border fit inside the width you set, which is why many websites use border-box for every element.

Why do my margins collapse?

When two blocks sit on top of each other, their vertical margins merge into one, the larger of the two. Padding never collapses, and margins of items inside a flex or grid layout do not collapse either. Use padding or the gap property when you need an exact distance.

Related terms

  • CSS: the language in which you set padding, margin and the rest of a page's look.
  • HTML: the elements that padding and margin are applied to.
  • Whitespace: the empty space in a design that padding and margin create.
  • Responsive design: a layout that adapts to the screen, often with smaller spacing on a phone.

Put it to work
on your own website.

Start free with WEMASY and build your site in a few minutes.