Skip to main content
Albero Academy

Sizing, Overflow & object-fit

Control dimensions with min/max, handle content that is too big with overflow, and fit images into boxes with object-fit.

Chapter 10CSS13 min readBeginner friendly
  • #CSS
  • #Sizing
  • #Overflow

Controlling size well is what makes layouts robust across screens and content lengths. This chapter covers width/height and — more usefully — min-/max- constraints, what to do when content overflows its box, and fitting images cleanly.

Width & height

dimensions
width: 300px; /* fixed — avoid for responsive layouts */
width: 100%; /* relative to the parent */
height: 100vh; /* full viewport height */
/* Remember box-sizing: border-box (Chapter 2) so width includes padding+border */

min / max sizing

Constraints usually beat fixed sizes — they let content flow while staying within bounds. max-width is the responsive workhorse:

flexible bounds
.container {
width: 100%;
max-width: 1200px; /* never wider than 1200, but shrinks on small screens */
margin: 0 auto; /* centre it */
}
img { max-width: 100%; height: auto; } /* never overflow its container */
button { min-width: 120px; } /* don't get too narrow */

Overflow

when content is too big
overflow: hidden; /* clip the excess */
overflow: auto; /* scrollbars only when needed */
overflow-x: auto; /* horizontal scroll (e.g. a wide table/code block) */
overflow-y: scroll; /* always show a vertical scrollbar */
/* Truncate one line of text with an ellipsis */
.truncate {
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

object-fit

When an image must fill a fixed box without distorting, object-fit controls how it fits — like background-size but for real <img> elements:

fit images cleanly
.avatar {
width: 80px; height: 80px;
object-fit: cover; /* fill & crop, keeping aspect ratio (no squish) */
object-position: top; /* which part to keep when cropping */
border-radius: 50%;
}
/* object-fit: contain — fit the whole image, letterboxed */

Practice

Build a centred page container with max-width + margin: 0 auto. Make a fixed-size square avatar that crops any image with object-fit: cover. Add a wide code block that scrolls horizontally with overflow-x: auto, and a single-line truncated title.

Key takeaways

  1. 01Prefer relative sizes and constraints over fixed pixel widths for responsive layouts.
  2. 02max-width + margin: 0 auto is the centred responsive container; img { max-width: 100% } prevents overflow.
  3. 03overflow: hidden/auto/scroll handles content bigger than its box.
  4. 04Truncate one line with white-space: nowrap + overflow: hidden + text-overflow: ellipsis.
  5. 05object-fit: cover fills a fixed box and crops (no distortion) — ideal for avatars and thumbnails.