padding

Term side of the Card.
Topic: Sizes & Margins
Unit title: padding
Categories: Property
Explanation side of the Card

Explanation: The property that sets the space between the content of an element and its border. It can be set for all sides at once or for each side with `padding-top`, `padding-right`, `padding-bottom` and `padding-left`, adding inner spacing without changing margins.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
.sidebar {
  width: 300px;
  padding: 20px;
}
Rightcss
.sidebar {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
}

With the default 'content-box', padding is added on top of width, so the box grows to 340px, while 'border-box' keeps it at 300px.

Common mistakes

  • The element grows wider than set because padding is added to width.
  • Vertical padding on an inline span overlaps nearby lines instead of pushing them away.

Key points

  • Elements with width and padding use box-sizing: border-box.
  • Side padding uses logical properties like padding-inline where layouts support right-to-left text.
  • Fixed aspect ratios use aspect-ratio, not the old padding-top percentage trick.

Interview questions

What is the order of values in the padding shorthand?

Top, right, bottom, left, clockwise; two values set vertical then horizontal, and three set top, horizontal, bottom.

Can padding be negative?

No. Negative padding values are invalid; use negative margin instead.

What is the difference between padding and margin?

padding is space inside the border and shows the background; margin is space outside the border and is transparent.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy