CSS Width, Height & Overflow
Width and Height
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 |
width and height
width and height set the element's content area dimensions:
Example
<style>
.box {
width: 300px;
height: 200px;
background: #f0f0f0;
}
</style>
<div class="box">Box element showing dimension range</div>
Common Units
Example
<style>
.pixel {
width: 300px;
background: #e3f2fd;
}
.percent {
width: 50%;
background: #fce4ec;
}
.vw-vh {
width: 50vw;
height: 100vh;
background: #e8f5e9;
}
.auto-width {
width: auto;
background: #fff3e0;
}
</style>
<div class="pixel">Pixel unit: fixed width 300px</div>
<div class="percent">Percent unit: width 50%</div>
<div class="vw-vh">Viewport unit: width 50vw</div>
<div class="auto-width">Auto width: determined by content</div>
min-width / max-width
Limit element's minimum/maximum width, commonly used in responsive design:
Example
<style>
.container {
width: 80%;
max-width: 1200px;
min-width: 320px;
margin: 0 auto;
background: #e8eaf6;
padding: 10px;
}
</style>
<div class="container">Container with responsive width constraints</div>
min-height / max-height
Example
<style>
.card {
min-height: 200px;
max-height: 400px;
background: #fff3e0;
border: 1px solid #ccc;
}
</style>
<div class="card">Card component with min/max height. Content exceeding max height will overflow and require overflow property to handle.</div>
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
<style>
.layout {
width: calc(100% - 300px);
background: #e3f2fd;
padding: 16px;
}
.sidebar {
width: 300px;
float: left;
background: #fce4ec;
padding: 16px;
box-sizing: border-box;
}
</style>
<div class="layout">Main content: width = 100% - 300px</div>
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
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().
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.
overflow Values
Example
<style>
.visible {
overflow: visible;
background: #fce4ec;
height: 50px;
}
.hidden {
overflow: hidden;
background: #e8f5e9;
height: 50px;
}
.scroll {
overflow: scroll;
background: #fff3e0;
height: 50px;
}
.auto {
overflow: auto;
background: #e3f2fd;
height: 50px;
}
</style>
<div class="visible">Visible area (overflow shows outside box)</div>
<div class="hidden">Content clipped when exceeding (overflow hidden)</div>
<div class="scroll">Scrollbar always visible even if content fits</div>
<div class="auto">Scrollbar appears only when content overflows</div>
Controlling Each Direction
Example
<style>
.box {
overflow-x: hidden;
overflow-y: auto;
width: 200px;
height: 80px;
background: #f5f5f5;
border: 1px solid #ddd;
}
</style>
<div class="box">Box element with horizontal overflow hidden and vertical auto-scroll for viewing all content</div>
Common Use Cases
1. Text Ellipsis
Combined with text-overflow for single-line text truncation:
Example
<style>
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 200px;
border: 1px solid #ccc;
padding: 8px;
background: #f9f9f9;
}
</style>
<div class="ellipsis">This is a long text that will be truncated with ellipsis when exceeding container width</div>
2. Image Cropping
Fixed container size with hidden overflow for images:
Example
<style>
.avatar {
width: 100px;
height: 100px;
overflow: hidden;
border-radius: 50%;
background: #e0e0e0;
}
.avatar img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
<div class="avatar">Avatar container (circular crop)</div>
3. Scroll Container
Create a fixed-height scrollable area:
Example
<style>
.scroll-box {
height: 300px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 10px;
background: #fafafa;
}
</style>
<div class="scroll-box">
<p>Line 1: Scroll container can display large content</p>
<p>Line 2: Scrollbar appears when content exceeds height</p>
<p>Line 3: Scroll to view hidden content</p>
<p>Line 4: Implemented with overflow-y: auto</p>
<p>Line 5: More content means more scrolling</p>
<p>Line 6: Suitable for comment sections, announcements, etc.</p>
</div>
4. Clear Floats
Setting overflow: hidden or overflow: auto on parent clears child float effects:
Example
<style>
.clearfix {
overflow: hidden;
background: #e8eaf6;
padding: 10px;
}
</style>
<div class="clearfix">Float clearing container (overflow: hidden triggers BFC)</div>
object-fit (Image/Video Adaptation)
object-fit controls how <img> or <video> replacement elements adapt to container size:
Example
<style>
.image-container {
width: 300px;
height: 200px;
background: #f5f5f5;
border: 2px dashed #aaa;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 10px;
}
.image-container img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
<div class="image-container">Image container (replace with actual image)</div>
- 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
CSS Dimension Functions
calc()
calc() enables dynamic dimension calculations in CSS, mixing different units:
Example
<style>
.calc-demo {
width: calc(100% - 40px);
height: calc(50vh - 60px);
background: #e3f2fd;
border: 1px solid #90caf9;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<div class="calc-demo">Width auto-subtracts 40px, height is half viewport minus 60px</div>
Note: 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.



