CSS Max-width
A block element with a fixed width looks fine on the screen you designed it for — but on a narrower screen it overflows, forcing a horizontal scrollbar. max-width fixes that: the element can be up to a certain width, and shrinks when there isn't enough room.
width vs max-width
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
div { padding: 10px; margin: 8px 0; color: white; }
.w { width: 500px; background: #e53935; }
.mw { max-width: 500px; background: #43a047; }
</style>
</head>
<body>
<div class="w">width: 500px — drag the frame narrower and I overflow.</div>
<div class="mw">max-width: 500px — I shrink to fit narrow frames.</div>
</body>
</html>Resize the playground (drag its edge) to see the difference. The red box keeps its 500px and pokes out; the green one adapts.
Tip: If both
widthandmax-widthare set,max-widthwins when it is smaller. And ifmin-widthis larger thanmax-width,min-widthwins.
Centering a container
The most common pattern in all of CSS: a page wrapper with a maximum width, centred with margin: 0 auto. Automatic left and right margins split the leftover space equally.
<!DOCTYPE html>
<html>
<head>
<style>
body { margin: 0; background: #e8eaf6; font-family: system-ui, sans-serif; }
.container {
max-width: 360px;
margin: 20px auto;
padding: 16px;
background: white;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,.12);
}
h2 { margin-top: 0; color: #3949ab; }
</style>
</head>
<body>
<div class="container">
<h2>Centered card</h2>
<p>I am at most 360px wide and always centred, no matter how wide the frame is.</p>
</div>
</body>
</html>Readable line lengths
Long lines of text are tiring to read. Typographers recommend roughly 45–75 characters per line. The ch unit (width of the "0" character) makes this easy: max-width: 65ch.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Georgia, serif; font-size: 16px; line-height: 1.6; }
.narrow { max-width: 40ch; border-left: 4px solid #8e24aa; padding-left: 12px; }
</style>
</head>
<body>
<p class="narrow">This paragraph is limited to about forty characters per line, which keeps the eye from travelling too far before jumping back to the start of the next line.</p>
</body>
</html>Responsive images
max-width: 100% on images is a classic rule: an image is never wider than its container, but never upscaled past its natural size. Pair it with height: auto to keep the proportions.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { width: 200px; border: 3px dashed #999; padding: 6px; }
img { max-width: 100%; height: auto; display: block; }
</style>
</head>
<body>
<div class="box">
<img width="400" height="200" alt="sunset"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='200'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0' y1='0' x2='0' y2='1'%3E%3Cstop offset='0' stop-color='%23ff9800'/%3E%3Cstop offset='1' stop-color='%23e91e63'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='400' height='200' fill='url(%23g)'/%3E%3Ccircle cx='200' cy='150' r='60' fill='%23ffeb3b'/%3E%3C/svg%3E">
<p>A 400px image inside a 200px box</p>
</div>
</body>
</html>Related properties
| Property | Meaning |
|---|---|
max-width |
Upper limit on width |
min-width |
Lower limit on width |
max-height |
Upper limit on height (content may overflow) |
min-height |
Lower limit on height (box can still grow) |
max-inline-size |
Logical version of max-width (respects writing direction) |
Test yourself
Why is max-width usually better than width for a page container?
- It loads faster
- It shrinks on small screens instead of overflowing
- It centers automatically
- It removes padding
Which declaration centres a block that has a max-width?
- text-align: center
- margin: 0 auto
- align: center
- float: center
What does img { max-width: 100%; height: auto; } achieve?
- Stretches every image to full width
- Keeps images inside their container while preserving aspect ratio
- Hides large images
- Crops images