Sub topic 10
Constraining size with min and max dimensions
This chapter assumes you know how width and height set the content area, how padding and border add to it, and how box-sizing changes that arithmetic. It also assumes you are comfortable with px, rem, %, and vh units. Here you will learn to set limits on a box instead of fixing its size, so one stylesheet works from a phone to a wide monitor.
Why limits beat fixed sizes
A fixed width: 600px fails on a 400px screen. It forces horizontal scrolling. A fluid width: 100% fails on a 2000px screen, where lines of text become too long to read.
The four constraint properties solve this by setting a floor and a ceiling:
| Property | Meaning | Initial value |
|---|---|---|
| `min-width` | The box may not be narrower than this | `auto` (resolves to `0` for most boxes) |
| `max-width` | The box may not be wider than this | `none` |
| `min-height` | The box may not be shorter than this | `auto` (resolves to `0` for most boxes) |
| `max-height` | The box may not be taller than this | `none` |
The browser first computes the size from width or height, or from the content if those are auto. It then clamps the result into the allowed range.
The resolution order
Browsers apply the constraints in a fixed order:
- Compute the tentative size from
width(or from the content when it isauto). - If the tentative size is larger than
max-width, usemax-width. - If the result is smaller than
min-width, usemin-width.
Step 3 runs last, so the minimum wins over the maximum when they conflict. Height follows the same sequence with min-height and max-height.
Here is a case where the two limits conflict:
.box {
width: 500px;
max-width: 300px;
min-width: 400px;
}The tentative width of 500px is cut to 300px by max-width. That is below min-width, so the final width is 400px. A conflict like this is almost always a mistake, but the rule is predictable.
The classic readable container
The most common use is a centred column that shrinks on small screens and stops growing on large ones:
.article {
width: 90%;
max-width: 40rem;
margin-inline: auto;
}On a 500px viewport the width is 90% of the parent, which is 450px, and max-width has no effect. On a 1500px viewport the 90% would give 1350px, so max-width caps it at 40rem. The auto margins then split the leftover space evenly, which centres the box.
You can often write this with only max-width. A block element with width: auto already fills its container, and max-width stops it growing past the limit:
.article {
max-width: 40rem;
margin-inline: auto;
}This version is shorter and has an advantage. It never sets an explicit width, so padding and borders cannot push the box past its parent, whatever box-sizing is.
Interaction with box-sizing
The constraints apply to the box that box-sizing selects. With content-box, max-width limits the content area. Padding and border are added on top. With border-box, max-width limits the border box, and the content area shrinks to make room.
.card {
max-width: 300px;
padding: 20px;
border: 5px solid #333;
}With content-box, the border box can reach 350px (300 + 40 + 10). Add box-sizing: border-box and the border box tops out at 300px, with a 250px content area. This is another reason to use the universal border-box reset: the numbers in your constraints then describe the box you see on screen.
Margins are never included in these limits. They sit outside the box that min-* and max-* measure.
Images and media
Replaced elements such as <img> and <video> have an intrinsic size taken from the file. A large photo can overflow a narrow column. The standard fix is:
img,
video {
max-width: 100%;
height: auto;
}max-width: 100% stops the image growing wider than its container. It never stretches a small image, because max-width only caps. height: auto keeps the aspect ratio. Without it, a height attribute in the HTML would squash the image when the width is capped.
Height constraints
Setting height is risky for boxes that hold text. If the text wraps onto more lines than expected, it overflows the fixed height. Use min-height instead, so the box grows with its content but never gets shorter than a floor:
.hero {
min-height: 50vh;
padding: 2rem;
}max-height is useful when you want to cap a box and let the rest scroll:
.log {
max-height: 12rem;
overflow-y: auto;
}If the content is shorter than 12rem, the box fits it. If the content is longer, the box stops at 12rem and the overflow property decides what happens to the excess. Without overflow, the default is visible, so the text spills out over whatever follows.
Percentage heights need a known parent height
A percentage width resolves against the parent's width, which is almost always known. A percentage height resolves against the parent's height, which is often determined by its content. When the parent has height: auto, a percentage min-height or max-height on the child behaves as if it were not set. For a min-height, the percentage behaves like 0.
html,
body {
min-height: 100%; /* does not make the body fill the screen */
}
body {
min-height: 100vh; /* works: relative to the viewport */
}Viewport units refer to the window, not to the parent, so they avoid the problem.
Constraints on inline boxes
Inline elements ignore width, height, and all four constraint properties. A <span> with max-width: 100px is not limited. The constraints apply to block-level boxes, inline-block boxes, and replaced elements such as images. If you need to limit an inline element, set display: inline-block or display: block first.
Content-based keywords
Constraint values can be keywords that depend on the content instead of fixed lengths:
min-contentis the narrowest the box can get without overflowing, which usually means the longest unbreakable word.max-contentis the width the content would take if it never wrapped.fit-contentuses the available space, but never goes belowmin-contentor abovemax-content.
.tag {
width: fit-content;
max-width: 100%;
}
.label {
min-width: min-content;
}You can also use clamp(min, preferred, max) to write a floor, a flexible middle, and a ceiling in one declaration:
.panel {
width: clamp(20rem, 50%, 40rem);
}The panel is 50% of its parent, but never narrower than 20rem or wider than 40rem. This is the same as width: 50%; min-width: 20rem; max-width: 40rem, with one difference. In clamp(), the minimum is applied last as well, so the result for a conflicting pair is the same.
Logical equivalents
Each physical property has a direction-aware counterpart. They follow the writing mode, so in a vertical script the "inline" axis is vertical.
| Physical | Logical |
|---|---|
| `min-width` | `min-inline-size` |
| `max-width` | `max-inline-size` |
| `min-height` | `min-block-size` |
| `max-height` | `max-block-size` |
The readable column becomes:
.article {
max-inline-size: 40rem;
margin-inline: auto;
}Common errors
- A
max-widththat does nothing. The element is probablydisplay: inline. Change the display type. - A box that is wider than its constraint. Check
box-sizing. Withcontent-box, padding and border add to the capped width. - A
min-height: 100%that does not fill the screen. The parent has no definite height. Usevhordvh, or give the ancestors heights. - A
min-widththat overridesmax-width. The minimum always wins. Remove the conflicting value. - Text that overflows a
height. Replaceheightwithmin-height, or add anoverflowrule if you need a hard cap.
Exercise
An element has box-sizing: border-box; width: 80%; max-width: 500px; min-width: 520px; padding: 10px; border: 2px solid. Its parent is 1000px wide. What is the element's border-box width, and why?
Show answer
520px. The 80% width gives 800px. max-width caps that at 500px. The result is below min-width, and the minimum is applied last, so the width becomes 520px. With border-box, this is the border box, so padding and border do not add to it.
Exercise
A <span class="badge"> has max-width: 120px, but long badge text still runs wider than 120px. Give one change that makes the limit work, and say what it does.
Show answer
Add display: inline-block (or display: block) to .badge. Inline boxes ignore width, height, and the min and max constraints. An inline-block box keeps its place in the line but honours the constraints, so the badge stops at 120px.