CSS: CSS Width, Height & Overflow

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

Properties in this section: width height min-width max-width min-height max-height overflow overflow-x overflow-y text-overflow object-fit

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 Yourself

2. 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.

📌 calc() rules: + 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

  1. Sidebar + Main content: .main { width: calc(100% - 300px); } adapts to remaining space
  2. Equal-spacing cards: .card { width: calc((100% - 40px) / 3); } three columns with 20px gaps
  3. Responsive font: font-size: calc(14px + 0.5vw); base size + viewport increment
  4. 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

💡 overflow-x and overflow-y: When setting both horizontal and vertical overflow with different values, one may be forced to 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 Yourself

2. Image Cropping

Fixed container size with hidden overflow for images:

▶ Example

▶ Try it Yourself

3. Scroll Container

Create a fixed-height scrollable area:

▶ Example

▶ Try it Yourself

4. Clear Floats

Setting overflow: hidden or overflow: auto on parent clears child float effects:

▶ Example

▶ Try it Yourself

4. object-fit (Image/Video Adaptation)

object-fit controls how <img> or <video> replacement elements adapt to container size:

▶ Example

▶ Try it Yourself

5. CSS Dimension Functions

(1) calc()

calc() enables dynamic dimension calculations in CSS, mixing different units:

▶ Example

▶ Try it Yourself

Note: Operators must have spaces on both sides, otherwise they won't be recognized.


Properties in this section: width height min-width max-width min-height max-height overflow overflow-x overflow-y text-overflow object-fit

❓ FAQ

Q Why doesn't height: 100% work?
A Percentage height is relative to the parent's height. If the parent's height is auto (determined by content), the child's height: 100% can't be calculated — the parent hasn't determined its own height yet. Fixes: give the parent an explicit height, use height: 100vh down the chain, or switch to flex so children stretch with align-self: stretch.
Q How do I hide the scrollbar but keep scrolling?
A Combo: .box { overflow: auto; scrollbar-width: none; } .box::-webkit-scrollbar { display: none; }. scrollbar-width is the standard property (Firefox and newer Chrome), while ::-webkit-scrollbar covers Safari and older Chrome. It's a visual trick — keyboard and wheel still scroll, so provide other scrolling hints or users will assume content fits one screen.
Q overflow: hidden clips the overflowing content — what if I need to see it?
A overflow: auto shows a scrollbar only when content actually overflows, overflow: scroll always shows the track, and overflow: clip is stricter than hidden (blocks even programmatic scrolling). overflow: hidden is only for "overflow must not be seen" cases — it's also commonly used to trigger BFC for float issues.
Q What's the difference between max-width: 100% and width: 100%?
A width: 100% forces the element to equal parent width, even if content is small. max-width: 100% means "never exceed parent width" — if the element is naturally smaller, it stays smaller. Using max-width: 100% for responsive images is best practice.
Q What's the real difference between overflow: auto and scroll — which should I use?
A Whether the scrollbar is permanent: auto only appears when content actually overflows; scroll always shows the track. Use auto for normal content areas — no "empty track despite no overflow" awkwardness. Use scroll for fixed panels (like code blocks) when a stable layout without jitter matters.
Q My image overflows its container and gets stretched — how do I crop it proportionally?
A object-fit: cover is the standard answer: the image scales proportionally to fill the container and the excess gets cropped; object-position: center controls the focal point. Compare with overflow: hidden — that just clips without scaling, so the image still gets distorted by width/height. For images, always prefer object-fit.
Q When do I use min-width and max-width, and who wins when combined with width?
A Priority is min-width > max-width > width: min and max form a range and width only applies inside it. Typical setup: card with width: 100%; max-width: 640px; min-width: 280px; — never grows endlessly on large screens, never gets crushed on small ones. Use this combo for inputs and modals too.
Q Text is too long — how do I show an ellipsis (...)?
A Single-line ellipsis needs all three together: overflow: hidden; white-space: nowrap; text-overflow: ellipsis; — miss one and it fails. Multi-line uses -webkit-line-clamp: 3 with display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden;. The -webkit- prefix looks scary but all modern browsers support it.
Q What are the rules for math in calc()?
A Operators need spaces on both sides: width: calc(100% - 200px) — no spaces means invalid. Addition/subtraction require a unit on one side; multiplication/division require the divisor to be a plain number: calc(100% / 3) is valid, calc(100% / 3px) is not. For sidebar-plus-content adaptive layouts, calc saves a wrapper layer.

📖 Summary

📝 Exercises

  1. Create a "responsive container" — width 80%, max-width 1200px, min-width 320px, centered horizontally.
  2. Create a fixed-size image container (300×200) with a larger image, using object-fit: cover to fill without distortion.
  3. Implement "single-line text ellipsis" and "multi-line text overflow" effects (hint: multi-line uses -webkit-line-clamp).
  4. Use calc() to implement a "full screen minus navigation bar height" layout.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏