# padding

> The `padding` CSS property sets the space between the content of an element and its border.

## How do I add padding without making the element wider in CSS?

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.

- Topic: Sizes & Margins
- Type: property
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 1+, Safari iOS 1+

## Example

Wrong:

```css
.sidebar {
  width: 300px;
  padding: 20px;
}
```

Right:

```css
.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.

## Related cards

- [margin](https://wonderium.dev/css/margin.md)
- [box-sizing](https://wonderium.dev/css/box-sizing.md)
- [Logical Properties](https://wonderium.dev/css/logical-properties.md)
- [aspect-ratio](https://wonderium.dev/css/aspect-ratio.md)
- [scroll-padding](https://wonderium.dev/css/scroll-padding.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/padding
- Last updated: 2026-11-25
