Sizing elements

Topic 3 of 4 in The box model6 min readBeginnerFree course

Overview

Knowing how to set a size is easy: width: 300px. Knowing when to set one — and when to let the browser decide — is what separates fragile layouts from robust ones. This lesson covers the sizing tools CSS gives you and the defaults you should usually keep.

Default sizes

In normal flow:

  • Width: a block element is as wide as its container (width: auto). An inline element is as wide as its content.
  • Height: a block is as tall as its content (height: auto).

These defaults are good. A paragraph fills its column and grows taller when the text is longer or the screen narrower. Most of the time, you should leave width and height alone and constrain them instead.

Limits: min and max

Fixed sizes break: a 600px-wide box overflows a 375px phone; a 200px-tall card cuts off long text. Minimum and maximum sizes let boxes stay flexible within limits:

CSS
.page {
  max-width: 70rem;        /* fill the screen, but never wider than 70rem */
  margin-inline: auto;     /* centre it when it hits the limit */
}

.hero {
  min-height: 60vh;        /* at least 60% of the viewport, taller if the content needs it */
}

.sidebar {
  width: 20rem;
  max-width: 100%;         /* but never wider than its container */
}

When limits conflict, the order of priority is: min-width beats max-width, which beats width.

Readable line lengths

Lines of text become hard to read beyond roughly 60–75 characters. The ch unit — the width of the “0” character in the current font — makes this easy:

CSS
.prose { max-inline-size: 65ch; }

Percentage heights

width: 50% works because the parent’s width is known before its children are laid out. Heights flow the other way: the parent’s height usually depends on its children, so height: 50% has nothing to be a percentage of, and it’s treated as auto.

Percentage heights work when the parent has a definite height — a fixed height, a viewport unit, or a size given by a flex or grid container. For full-screen sections, use viewport units instead:

CSS
.hero { min-height: 100dvh; }   /* see the CSS units lesson for dvh */

aspect-ratio

aspect-ratio keeps a box’s shape as its width changes:

CSS
.video { width: 100%; aspect-ratio: 16 / 9; }
.avatar { width: 3rem; aspect-ratio: 1; border-radius: 50%; }
.thumbnail { aspect-ratio: 4 / 3; object-fit: cover; }

Give it one dimension and it calculates the other. If the content is taller than the ratio allows, the box grows to fit the content (unless you also set overflow or a min-height: 0). This replaced the old “padding-top: 56.25%” hack for responsive videos.

Setting width and height attributes on <img> elements gives the browser their aspect ratio before the image loads, preventing the page from jumping — more in Responsive images and media.

Intrinsic sizes

So far, sizes have been extrinsic — set from outside (by you, or the container). Content also has intrinsic sizes — the sizes it would naturally like to be:

Four boxes containing the text “Responsive layouts”. min-content: as narrow as the longest word, so it wraps to two lines. max-content: all on one line, even if it overflows. fit-content: max-content, but never wider than the space available; in a narrow container it wraps like min-content.

Keyword Size
min-content the smallest it can be without overflowing — for text, the longest word
max-content the size it would be with unlimited space — text on one line
fit-content max-content, but no wider than the available space
CSS
.tag { width: fit-content; }            /* a block that hugs its text */
.figure { width: min-content; }         /* a figure as wide as its image; the caption wraps to match */
.button-group { width: max-content; }   /* never wrap the buttons */

fit-content is the most useful: it makes a block element shrink-wrap its content while still wrapping on small screens. Flexbox and grid use these intrinsic sizes constantly when deciding how big items should be, as you’ll see in later chapters.

Comparison functions

min(), max() and clamp() choose between values at layout time:

CSS
.card { width: min(100%, 30rem); }            /* the smaller: 30rem, or the full width on small screens */
.section { padding: max(1rem, 5vw); }         /* at least 1rem */
h1 { font-size: clamp(1.75rem, 1rem + 3vw, 3rem); }  /* fluid, with a minimum and maximum */

width: min(100%, 30rem) is a one-line replacement for width: 30rem; max-width: 100%. You’ll use clamp() a lot in Fluid sizing with clamp.

Default to flexibility: let widths be auto, let heights grow with content, and add max-*, min-* and aspect-ratio to keep things in bounds. Fixed widths and heights should be the exception.

Exercise

A readable article with a video

Style an article so its text column is never wider than 65 characters and is centred on wide screens, with 1rem of padding so text never touches the screen edge. Inside it, make .video (an empty placeholder div) fill the column width and always keep a 16:9 shape. Finally make a .tag label that is only as wide as its text, even though it’s a block element.

HTML
<article>
  <span class="tag">New lesson</span>
  <h1>Sizing elements</h1>
  <div class="video"></div>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.</p>
</article>

<style>
  * { box-sizing: border-box; }
  .video { background: #1b1d21; }
  .tag { display: block; background: #eaf0fc; padding: 0.25rem 0.5rem; }
  /* your code here */
</style>

Summary

  • Blocks fill their container’s width by default; their height comes from their content.
  • Prefer max-width and min-height over fixed width and height, so boxes can adapt to content and screens.
  • Percentage heights only work when the parent has a definite height.
  • aspect-ratio keeps a box’s shape as its width changes.
  • min-content, max-content and fit-content size a box from its content.