# width

> The `width` CSS property sets the width of an element's content area.

## How do I make an element full width but not wider than a max size?

The property that defines the width of an element. It can be set using specific values, such as pixels, percentages or other units, controlling how much horizontal space the element occupies within its container.

- 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
.container {
  width: 1200px;
  margin-inline: auto;
}
```

Right:

```css
.container {
  width: min(100% - 2rem, 1200px);
  margin-inline: auto;
}
```

`min()` caps the width at '1200px' but shrinks it on small screens, keeping a side gutter.

## Common mistakes

- A fixed `width` in pixels overflows on small screens.
- `width: 100%` plus padding makes the element wider than its parent.

## Key points

- Layout containers use `max-width` or `min()` instead of a fixed pixel `width`.
- Elements with `width: 100%` and padding use `box-sizing: border-box`.
- Full-width sections use '100%', not '100vw', to avoid horizontal scrolling.

## Interview questions

### What is the default width of a block element?

'auto', which fills the containing block's width minus margins, border and padding.

### What is the difference between width: auto and width: 100%?

'auto' fits the available space including margins, while '100%' sets the content box to the parent width and can overflow with padding or margins.

### Does width work on inline elements?

No. `width` is ignored on non-replaced inline elements like `span`; use 'inline-block' or another display value.

## Related cards

- [max-width](https://wonderium.dev/css/max-width.md)
- [min-width](https://wonderium.dev/css/min-width.md)
- [box-sizing](https://wonderium.dev/css/box-sizing.md)
- [min()](https://wonderium.dev/css/min.md)
- [fit-content()](https://wonderium.dev/css/fit-content.md)

---

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