Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 139 ยท The Box Model

Width and Height

Learn how CSS width and height work, what auto really means, how min- and max- limits protect your layout, and when to use min-content, max-content and fit-content.

width and height look like the simplest properties in CSS: give a box a number and it becomes that size. In practice, the best layouts set them rarely. Most of the time the browser works out a size from the container and the content, and you only add limits: "never wider than this", "at least this tall".

This lesson covers fixed sizes, the auto default, percentages, min-* and max-* limits, and the intrinsic keywords min-content, max-content and fit-content that size a box from what is inside it. All the examples assume the border-box reset from the previous lesson.

Fixed sizes

Any length works for width and height:

CSS
.avatar {
  width: 64px;
  height: 64px;
}

.sidebar {
  width: 18rem;
}

Fixed sizes are right for things that really are a set size: icons, avatars, a logo. For containers holding text, a fixed width is usually a mistake. Text can grow when a user zooms or changes the default font size, and translations can be much longer than your English copy. A fixed-height box that holds text is even riskier, because the text will spill out the bottom when it does not fit. You will learn what happens then in Overflow.

auto: the default

Both properties start as auto, and auto means different things in each direction.

width: auto on a block box means "fill the container". The box stretches across the available width, minus its own margins. This is why a <div> or <p> spans the page without you doing anything.

height: auto means "as tall as the content". Add another paragraph and the box grows.

So the browser gives you the useful behavior for free: boxes are as wide as their container and as tall as their content. Notice that width: auto and width: 100% are not the same:

width: auto vs width: 100%

Both items have 20 pixels of horizontal margin. The auto one shrinks to fit inside the container with its margins. The 100% one is the full container width and then gets its margins, so it pokes out on the right. When you want a block to fill its container, leave the width as auto.

Percentages

A percentage width is a share of the containing block's width, which for most elements is the content area of the parent.

Percentage heights are trickier. height: 50% only works when the parent has a definite height. If the parent's height is auto (it depends on the content), the percentage cannot be resolved and behaves like auto. This is the classic "my height: 100% does nothing" problem:

CSS
/* Does nothing if .page has no set height */
.hero { height: 100%; }

For "fill the screen" layouts, viewport units such as 100dvh are usually what you want instead; they are covered in Viewport Units. Flexbox and Grid also stretch children to fill height without any percentages.

Minimums and maximums

min-width, max-width, min-height and max-height set limits. The element can still size itself naturally, but never past these bounds. They win over width and height, and if min-* and max-* disagree, the minimum wins.

The most useful one is max-width. It lets a box be fluid on small screens and stop growing on large ones:

CSS
.article {
  max-width: 40rem;  /* never wider than this */
  margin-inline: auto; /* center it when there is room */
}

min-height is the safe way to make a box "at least this tall": it keeps the size you asked for, but grows if the content needs more. Prefer it over height for anything with text.

Limits keep layouts flexible

Make the frame wide and the cards stop at 260 pixels. Make it narrow and they stop shrinking at 140 pixels. The second card ignores its 80-pixel minimum height because its text needs more room.

A pattern you will use constantly: width: 100%; max-width: 600px; or simply max-width: 600px on a block. It means "as wide as you can, up to 600 pixels". Images use max-width: 100% so they never overflow their container.

Intrinsic sizes: sizing from the content

So far the size came from the container (auto, percentages) or from you (lengths). Three keywords size a box from its content instead:

  • min-content: the narrowest the box can be without overflowing. For text, that is the width of the longest word.
  • max-content: the width the content would take if it never wrapped. For text, that is the whole thing on one line.
  • fit-content: max-content, but never wider than the available space. In other words, shrink-wrap the content, and wrap only if you must.
min-content, max-content and fit-content

The printed widths show the difference. The max-content box overflows the 320-pixel container; the second fit-content box stops at the container width and wraps.

fit-content is perfect for things like a label, a badge or a heading underline that should hug its text but still be a block on its own line. It also works for height, and inside min-width/max-width.

There is also a function form, fit-content(300px), which means "shrink-wrap, but never wider than 300px". It is mainly used for Grid track sizes, which you will meet in Grid Tracks; browser support for it on width is uneven. For width, use width: fit-content; max-width: 300px; instead.

Sizes in flex and grid layouts

Inside Flexbox and Grid, width and height become suggestions. A flex item with width: 200px may shrink below 200 pixels when space is tight, and grid items stretch to fill their cell. You will learn how flex-basis, flex-shrink and track sizes take over in Flex Item Properties.

One behavior worth knowing early: flex and grid items have an automatic min-width of min-content. That stops them shrinking below their longest word, which sometimes causes overflow with long URLs or code. Setting min-width: 0 on the item lets it shrink.

Logical sizes

width and height are physical: horizontal and vertical. CSS also has logical versions that follow the writing direction: inline-size (the width in horizontal text) and block-size (the height), plus min-inline-size, max-block-size and so on. In English pages they behave exactly like width and height; in vertical writing modes they swap. You will meet them in Writing Modes and Logical Properties.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A block has width: auto and margin: 0 20px inside a 400px container. How wide is its border box?

    auto fills the container after margins are subtracted: 400 - 20 - 20 = 360 pixels.
  2. 2. Why might height: 100% have no effect?

    A percentage height needs a parent with a definite height. If the parent depends on its content, the percentage behaves like auto.
  3. 3. An element has width: 500px and max-width: 300px. How wide is it?

    max-width wins over width, so the element stops at 300 pixels.
  4. 4. Which keyword shrink-wraps the content but wraps if the container is too narrow?

    fit-content uses the content's natural width but never exceeds the available space. max-content never wraps, and min-content wraps at every opportunity.
  5. 5. For a box that holds text and should be at least 200px tall, what is the safest choice?

    min-height gives you 200 pixels but lets the box grow if the text needs more room. A fixed height makes the text overflow.

Summary

  • width and height accept lengths, percentages, auto and intrinsic keywords.
  • width: auto fills the container (minus margins); height: auto fits the content. Prefer auto over 100%.
  • Percentage heights need a parent with a definite height.
  • min-* and max-* set limits; they beat width/height, and the minimum wins a conflict.
  • Use max-width for fluid boxes and min-height instead of height for anything with text.
  • min-content, max-content and fit-content size a box from its content.

Next: Margins, including auto centering, negative margins and the rules of margin collapsing.