Sizing Boxes: width, height, Logical Sizes, min/max and Intrinsic Sizes
How CSS sizes boxes: auto widths that fill and auto heights that fit, percentages and why percentage heights often fail, min and max limits and which wins, inline-size and block-size, and the intrinsic keywords min-content, max-content, fit-content and stretch.
kvi5hnuCode Sprout team member8 min readOn this page
Most boxes on a page don't need an explicit size: the browser computes one from the available space and the content. Knowing how that automatic sizing works, and how to limit it, matters more than setting exact numbers. This article covers the sizing properties for block boxes in normal flow; flexbox and grid add their own rules (articles 61 to 72).
Automatic sizes
With the initial value auto:
- Width fills the container. A block box's border box, plus its margins, stretches to the full width of its containing block, usually the parent's content box. That's why a
<div>is as wide as its parent without any CSS. - Height fits the content. The box grows exactly as tall as its content: lines of text, or the boxes of its children.
.card { padding: 1.5rem; } /* full width, as tall as its content */
So in normal flow, width is decided top-down by the container, and height bottom-up by the content. That asymmetry explains most sizing surprises.
width: auto versus width: 100%
They look the same until you add padding, borders or margins:
.a { margin-inline: 1rem; width: auto; } /* fits: auto subtracts the margins */
.b { margin-inline: 1rem; width: 100%; } /* overflows by 2rem */
auto takes whatever space is left after margins, borders and padding. 100% sets the content (or, with border-box, the border box) to the full container width, and then the margins push it past the edge. Leave block widths at auto unless you need something else.

Both mistakes come from fixing a size that the browser would otherwise work out from the container or from the content.
Lengths and percentages
.sidebar { width: 18rem; }
.half { width: 50%; }
- A percentage width is a share of the containing block's width. It always works.
- A percentage height is a share of the containing block's height, but only if that height is definite: set explicitly, or by a layout such as a flex or grid container that fixes it. If the parent's height depends on its content, a percentage height is treated as
auto, because the browser can't size the child from the parent and the parent from the child at the same time.
.wrapper { height: 20rem; }
.wrapper .panel { height: 50%; } /* works: 10rem */
.auto-wrapper .panel { height: 50%; } /* ignored: behaves like auto */
The classic case is trying height: 100% to make a section fill the screen, which then needs height: 100% on every ancestor up to <html>. Use viewport units instead: min-height: 100svh (article 25). For equal-height columns, use flexbox or grid, where boxes stretch to the tallest one by default.
Absolutely positioned elements (article 56) are an exception: their percentage heights refer to the padding box of the positioned ancestor, which is always definite.
min-* and max-*
Limits let a box size itself naturally, but within bounds:
| Property | Initial value | Effect |
|---|---|---|
min-width, min-height | auto (usually 0 in normal flow) | never smaller than this |
max-width, max-height | none | never larger than this |
.page { max-width: 70rem; margin-inline: auto; } /* fills small screens, capped on large ones */
.hero { min-height: 60svh; } /* at least this tall, grows with content */
img { max-width: 100%; height: auto; } /* images never overflow their container */
- Prefer
max-widthoverwidthfor page containers: a fixedwidth: 70removerflows narrow screens, whilemax-widthshrinks. - Prefer
min-heightoverheightfor content boxes: a fixed height cuts off or overflows its content when the text is longer, translated or enlarged by the user, whilemin-heightgrows. img { max-width: 100%; height: auto; }is part of most resets.height: autokeeps the image's proportions when its width is reduced.
Which one wins?
When the limits conflict, min beats max, and both beat the plain size:
.box { width: 100px; min-width: 300px; max-width: 200px; } /* 300px */
The browser first applies width, then clamps it to max-width, and finally to min-width. It's the same order as clamp() (article 26) when its minimum is larger than its maximum.
Logical sizes
width and height are physical: always horizontal and vertical. Their logical equivalents follow the writing direction (article 47):
| Physical | Logical, in horizontal text |
|---|---|
width, min-width, max-width | inline-size, min-inline-size, max-inline-size |
height, min-height, max-height | block-size, min-block-size, max-block-size |
article { max-inline-size: 65ch; } /* limits line length in any writing mode */
In vertical writing modes, inline-size becomes the height. Logical and physical properties set the same value, so the last declaration wins whichever form it uses.
Intrinsic sizes
Intrinsic sizing keywords size a box from its content:
| Keyword | Size |
|---|---|
min-content | the narrowest the content can get: the longest word, or the widest image |
max-content | the content's natural width without any line wrapping |
fit-content | max-content, but never more than the available space, nor less than min-content |
stretch | fill the containing block, minus margins: like auto for blocks, usable anywhere |
body { font: 0.95rem system-ui, sans-serif; margin: 1rem; }
p { background: #f4efe6; border: 2px solid #8a3b12; padding: 0.4rem 0.6rem; margin: 0 0 0.6rem; }
code { font-weight: 700; color: #8a3b12; }
.min { width: min-content; }
.max { width: max-content; }
.fit { width: fit-content; }
.fit-long { width: fit-content; }

The widths on the right are measured: only max-content ignores the container, and fit-content acts like max-content for short text and like auto for long text.
fit-contentis the most useful: it makes a block box shrink-wrap its content, like a button or a badge, while still respecting the container, whichmax-contentdoesn't. Combined withmargin-inline: auto, it centers a box of unknown width.min-contentis useful as a minimum:min-width: min-contentstops a box from getting narrower than its longest word.max-contentcan overflow its container, so use it with care.stretchis in Chrome 138 and Safari 27. Firefox supports it only under the old name-webkit-fill-available. In normal flow it's the same asauto, but it's handy whereautomeans something else, such as for absolutely positioned elements orheight.
These keywords also work in min-* and max-*, and in grid track sizes (article 67).
Avoid fixed heights
Text changes length: users enlarge fonts, translations are longer, content editors write more. A box with a fixed height either cuts off its content (overflow: hidden) or lets it spill out over what follows. Unless the box is a deliberate scroll area (article 43) or a fixed-format element like an avatar, use min-height, padding and the content's own size instead.
What's current
min-content,max-contentandfit-contentwork in all browsers forwidthandheight.stretchis in Chrome and Safari; Firefox has the same behavior as-webkit-fill-available.aspect-ratio(article 38) sizes one dimension from the other.- Animating to and from
autoheights is covered in article 108.
Common questions
Why doesn't height: 50% do anything?
The parent's height depends on its content, so it isn't definite, and the percentage is treated as auto. Give the parent an explicit height, use viewport units, or use flexbox or grid.
Why is my width: 100% box wider than its parent?
Margins (and, with content-box, padding and borders) are added to the 100%. Use width: auto, which subtracts them, or box-sizing: border-box and no horizontal margins.
min-width or max-width for responsive containers?
max-width: the box fills small screens and stops growing on large ones.
What happens when min-width is larger than max-width?
min-width wins.
How do I make a block box only as wide as its text?
width: fit-content. Other ways are display: inline-block, or making it a flex or grid item that doesn't stretch.
Summary
width: autofills the containing block, minus margins, borders and padding.height: autofits the content.width: 100%plus margins or padding overflows. Preferautofor block widths.- Percentage heights only work when the containing block has a definite height.
max-widthcaps a size,min-heightsets a floor.minwins overmax, and both over the plain size.inline-sizeandblock-sizeare the logical versions ofwidthandheight.min-content,max-content,fit-contentandstretchsize boxes from their content or their container.- Avoid fixed heights for content; use
min-height.
Practice questions
Write a page container that's at most 64rem wide, centered, and fills smaller screens with 1rem of space on each side.
Answer
.page { max-width: 64rem; margin-inline: auto; padding-inline: 1rem; box-sizing: border-box; }
Another option, without padding: width: min(100% - 2rem, 64rem); margin-inline: auto;.
A .panel has height: 100% inside a <main> with no height set. How tall is the panel, and why?
Answer
As tall as its content. <main>'s height depends on its content, so it isn't definite, and the percentage is treated as auto.
What width does this box get in a container 15rem wide?
.box {
width: 50rem;
max-width: 100%;
min-width: 20rem;
}
Answer
20rem. The width is first capped at 100% (15rem), and then min-width raises it to 20rem, because min-width wins. It overflows the container.
Write CSS for a centered "Load more" link that's only as wide as its text and padding.
Answer
.load-more { display: block; width: fit-content; margin-inline: auto; padding: 0.5em 1em; }
Previous: The Box Model: Content, Padding, Border, Margin and box-sizing
Next: aspect-ratio, object-fit and object-position for Media and Boxes

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.