CSS width / height
- type
- property
- applies to
- block elements, flex/grid items, images — not plain inline text
- inherits
- no
- common values
- auto, 100%, 480px, 60rem; min-width / max-width to set limits
Where it goes
On the box being sized. Remember box-sizing: without border-box, padding and border are added on top of the width you set.
Example
.card {
width: 100%;
max-width: 480px;
min-height: 120px;
}Common mistakes
- Setting a fixed height on a box that holds text. The moment the text wraps one more line, it spills out the bottom. Leave height auto, or use min-height.
- Setting width on an inline tag like <a> or <span> and seeing nothing. Inline tags ignore width and height. Change display first.
- Using width: 100% plus padding, then wondering why it spills over. Under the default box-sizing the padding is added to the 100%. Set border-box.