CSS: CSS Width, Height & Overflow
Last updated: 2026-10-13
1. Width and Height
(1) Dimension Properties Quick Reference
| Property | Common Values | Purpose |
|---|---|---|
width |
300px, 50%, 50vw, auto |
Element content width |
height |
200px, 50%, 50vh, auto |
Element content height |
min-width |
320px, 50% |
Minimum width (prevents too small) |
max-width |
1200px, 80% |
Maximum width (common in responsive) |
min-height |
200px, 100vh |
Minimum height |
max-height |
400px |
Maximum height (overflows if exceeded) |
overflow |
visible, hidden, scroll, auto |
How to handle overflowing content |
overflow-x |
visible, hidden, scroll, auto |
Horizontal overflow control |
overflow-y |
visible, hidden, scroll, auto |
Vertical overflow control |
(2) width and height
width and height set the element's content area dimensions:
▶ Example
▶ Try it Yourself(3) Common Units
▶ Example
▶ Try it Yourself(4) min-width / max-width
Limit element's minimum/maximum width, commonly used in responsive design:
▶ Example
▶ Try it Yourself(5) min-height / max-height
▶ Example
▶ Try it Yourself2. calc() Function
calc() is a CSS function that allows mathematical calculations (add, subtract, multiply, divide) in property values. Its biggest advantage is mixing different units.
+ and - must have spaces on both sides (calc(100% - 20px) ✅, calc(100%-20px) ❌), * and / spaces are optional.
▶ Example
▶ Try it Yourself(1) Common Use Cases
- Sidebar + Main content:
.main { width: calc(100% - 300px); }adapts to remaining space - Equal-spacing cards:
.card { width: calc((100% - 40px) / 3); }three columns with 20px gaps - Responsive font:
font-size: calc(14px + 0.5vw);base size + viewport increment - Precise centering:
left: calc(50% - 100px);known offset alternative to transform
(2) calc() Compatibility
calc() is supported in IE11+, Chrome 26+, Firefox 16+, Safari 7+ and can be safely used in modern projects. Just avoid string concatenation in calc().
3. Overflow Control
auto. Best practice is to use the overflow shorthand.
When element content exceeds set width/height, "overflow" occurs. The overflow property controls how overflow content is handled.
(1) overflow Values
▶ Example
▶ Try it Yourself(2) Controlling Each Direction
▶ Example
▶ Try it Yourself(3) Common Use Cases
1. Text Ellipsis
Combined with text-overflow for single-line text truncation:
▶ Example
▶ Try it Yourself2. Image Cropping
Fixed container size with hidden overflow for images:
▶ Example
▶ Try it Yourself3. Scroll Container
Create a fixed-height scrollable area:
▶ Example
▶ Try it Yourself4. Clear Floats
Setting overflow: hidden or overflow: auto on parent clears child float effects:
▶ Example
▶ Try it Yourself4. object-fit (Image/Video Adaptation)
object-fit controls how <img> or <video> replacement elements adapt to container size:
▶ Example
▶ Try it Yourself- cover: Maintains ratio, fills container, crops excess (most common)
- contain: Maintains ratio, shows complete image, may leave whitespace
- fill: Stretches to fill without maintaining ratio (may distort)
- none: Maintains original size
- scale-down: Takes smaller of none and contain
5. CSS Dimension Functions
(1) calc()
calc() enables dynamic dimension calculations in CSS, mixing different units:
▶ Example
▶ Try it YourselfNote: Operators must have spaces on both sides, otherwise they won't be recognized.
❓ FAQ
📖 Summary
- Controlling element dimensions and overflow behavior is fundamental to layout
width/heightset dimensions,min-width/max-widthconstrain range,overflowhandles overflowing contentobject-fit: coveris a powerful tool for image handling, whilecalc()provides flexible dynamic calculation- Mastering these properties gives you better control over element behavior in all situations
📝 Exercises
- Create a "responsive container" — width 80%, max-width 1200px, min-width 320px, centered horizontally.
- Create a fixed-size image container (300×200) with a larger image, using
object-fit: coverto fill without distortion. - Implement "single-line text ellipsis" and "multi-line text overflow" effects (hint: multi-line uses
-webkit-line-clamp). - Use
calc()to implement a "full screen minus navigation bar height" layout.