TExcelTAD
Home › CSS › CSS Height & Width

CSS Height & Width

The width and height properties control the size of an element's content area. By default block elements stretch to fill the available width and are only as tall as their content, but you can override both.

Basic values

Value Meaning
auto Default. The browser calculates the size.
200px, 12rem A fixed length.
50% A percentage of the containing block's width (for width) or height (for height).
min-content / max-content / fit-content Size based on the content itself.
initial / inherit Reset to default / copy from parent.
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  div { background: #4f8ef7; color: white; margin: 6px 0; padding: 6px; }
  .auto { }
  .fixed { width: 220px; height: 60px; }
  .half { width: 50%; }
  .fit { width: fit-content; background: #f76b4f; }
</style>
</head>
<body>
  <div class="auto">width: auto (fills the row)</div>
  <div class="fixed">220px × 60px</div>
  <div class="half">width: 50%</div>
  <div class="fit">fit-content</div>
</body>
</html>

Percentage heights need a parent height

A percentage width works almost everywhere, because the parent always has a known width. A percentage height only works if the parent has an explicit height. Otherwise the browser cannot compute it and treats it as auto.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 16px; }
  .parent { width: 140px; border: 3px dashed #888; padding: 4px; }
  .sized { height: 200px; }
  .child { height: 50%; background: #2bb673; color: white; padding: 4px; }
</style>
</head>
<body>
  <div class="parent">
    <div class="child">50% of ??? (no parent height)</div>
  </div>
  <div class="parent sized">
    <div class="child">50% of 200px</div>
  </div>
</body>
</html>

Tip: For full-screen sections, use viewport units such as height: 100vh or min-height: 100dvh instead of percentages.

Min and max constraints

Fixed sizes break easily on different screens. The min-* and max-* properties set limits while still letting the element be flexible.

Property Effect
min-width Never narrower than this.
max-width Never wider than this.
min-height Never shorter — but can grow with content.
max-height Never taller — extra content overflows.

min-height is especially useful: a card with min-height: 120px looks consistent when empty but still grows when there is a lot of text, while height: 120px would make text spill out.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 12px; align-items: flex-start; }
  .card { width: 150px; padding: 8px; background: #fff3c4; border: 2px solid #e0a800; }
  .fixed { height: 80px; }
  .min { min-height: 80px; }
</style>
</head>
<body>
  <div class="card fixed">height: 80px. Too much text here overflows the box and looks broken.</div>
  <div class="card min">min-height: 80px. Too much text here simply makes the box grow taller.</div>
  <div class="card min">Short.</div>
</body>
</html>

max-width for readable content

A classic pattern: let a block be fluid but stop it from getting uncomfortably wide on big screens, then centre it with auto margins.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: Georgia, serif; background: #eef1f5; margin: 0; padding: 12px; }
  article {
    max-width: 320px;
    margin: 0 auto;
    background: white;
    padding: 16px;
    box-shadow: 0 2px 6px rgba(0,0,0,.15);
  }
</style>
</head>
<body>
  <article>
    This article is never wider than 320px, but it shrinks if the frame gets narrower.
    Drag the playground's edge to resize it and watch.
  </article>
</body>
</html>

What width does not include

width and height describe the content box. Padding, border and margin come on top (unless box-sizing: border-box is set). See the Box Model lesson for the full picture.

Also note that width and height have no effect on non-replaced inline elements like <span> or <a>. Change their display to inline-block or block first.

Test yourself

Why might height: 50% have no effect?

  1. Percentages are not allowed for height
  2. The parent has no explicit height
  3. Height only accepts px
  4. The element is a block

Which property lets a box grow with its content but never be shorter than 100px?

  1. height: 100px
  2. max-height: 100px
  3. min-height: 100px
  4. line-height: 100px

Does width: 200px work on a plain <span>?

  1. Yes, always
  2. No, inline elements ignore width
  3. Only with !important
  4. Only in percentages