Skip to content
Laviyx Learn

Try

CSS course menu

CSS Width, Height, min/max and box-sizing: border-box

Learn to size elements in CSS: width and height, auto sizing, min-width, max-width, min-height and max-height, box-sizing content-box versus border-box, aspect-ratio, and intrinsic sizes such as fit-content.

Beginner6 min readUpdated
On this page

By default, block elements are as wide as their container and as tall as their content. The sizing properties let you change that.

width and height#

CSS
.banner {
  width: 400px;
  height: 120px;
}

Fixed heights are risky: if the content grows (longer text, bigger font, a translated language), it overflows the box. In most layouts, let the height be auto (the default) and control width instead.

max-width and min-width#

max-width lets an element shrink on small screens but stops it growing too wide on large ones. It is one of the most useful properties in responsive design:

CSS
.content {
  max-width: 360px;
  background: #fff3c4;
  padding: 8px;
}
img { max-width: 100%; height: auto; }
HTML
<div class="content">On a wide screen I stop at 360px; on a narrow one I shrink to fit.</div>
<img src="/lesson-assets/landscape-1200.svg" alt="Green hills" width="1200" height="800">
Result

img { max-width: 100%; height: auto; } makes images shrink to fit their container while keeping their shape. It belongs in almost every stylesheet.

PropertyUse
max-widthLimit how wide something gets (text columns, page containers, images)
min-widthStop something getting too narrow (buttons)
min-heightEnsure a minimum height while still allowing growth (hero sections, cards)
max-heightLimit height, usually together with overflow scrolling

box-sizing#

With the default box-sizing: content-box, width is the content width; padding and border are added outside it. With border-box, width is the total width including padding and border:

CSS
.box {
  width: 220px;
  padding: 20px;
  border: 5px solid #2f5fe0;
  margin-bottom: 10px;
  background: #cfe3ff;
}
.content-box { box-sizing: content-box; }
.border-box { box-sizing: border-box; }
HTML
<div class="box content-box">content-box: 270px wide in total</div>
<div class="box border-box">border-box: exactly 220px wide</div>
Result

border-box is much easier to work with: a width: 50% column with padding stays 50%. That is why almost every modern stylesheet starts with:

CSS
*,
*::before,
*::after {
  box-sizing: border-box;
}

aspect-ratio#

aspect-ratio keeps a box's shape as its width changes, which is great for video embeds, thumbnails and cards:

CSS
.video {
  width: 100%;
  max-width: 320px;
  aspect-ratio: 16 / 9;
  background: #222;
  color: white;
  display: grid;
  place-items: center;
}
HTML
<div class="video">16:9 at any width</div>
Result

Intrinsic sizes#

Some keywords size an element by its content:

ValueSize
autoThe default; for blocks, fill the available width
fit-contentAs wide as the content needs, but no wider than the available space
min-contentAs narrow as possible (the longest word)
max-contentAs wide as the content without wrapping
CSS
p { background: #c9f0d3; padding: 4px 8px; }
HTML
<p>Default width: fills the line</p>
<p style="width: fit-content">width: fit-content</p>
Result

Practice#

Practice exercise

With border-box sizing, create two boxes side by side (use display: inline-block), each exactly 50% wide with 16px padding and a 2px border, so they fit on one line.

Show solution
CSS
* { box-sizing: border-box; }
.half {
  display: inline-block;
  width: 50%;
  padding: 16px;
  border: 2px solid #2f5fe0;
  vertical-align: top;
}
.wrap { font-size: 0; }        /* removes the gap between inline-blocks */
.half { font-size: 16px; }
HTML
<div class="wrap"><div class="half">Left</div><div class="half">Right</div></div>
Result

With content-box, each would be 50% + 36px wide and the second would drop to a new line. The font-size: 0 trick removes the whitespace gap between inline-block elements; Flexbox (next section) avoids this problem entirely.

Key takeaway#

Finished this lesson?