CSS max-width / min-height
- type
- properties
- applies to
- block and flex/grid items
- inherits
- no
- common values
- 65ch, 1100px, 100%; 100vh, 3rem, 0
Where it goes
max-width on wrappers and text columns, where 65ch keeps lines readable. On images, where max-width: 100% stops them spilling out. On anything that must shrink on small screens. min-height on heroes, cards and footers that must not collapse.
Example
.wrap {
max-width: 1100px;
margin: 0 auto;
padding: 0 16px;
}
img { max-width: 100%; height: auto; }
.hero { min-height: 100vh; }Common mistakes
- Using width for the content column and getting a sideways scrollbar on phones. A fixed width cannot shrink. max-width can.
- Setting height: 100vh on a hero and then adding content. It spills out and overlaps the next section. min-height grows with the content.
- Forgetting that max-width: 100% on an image measures the parent. With no parent width, a 4000px photo still shows at 4000px. Give the parent a width.