CSS Height & Width
CSS Height and Width properties control the dimensions of an element's content box, with values specified in absolute units (px), relative units (%, em, rem), or special keywords (auto, min-content, max-content).
How do width and height work in CSS?
width and height set the size of an element's content box, or of its border box when box-sizing: border-box is used. Block elements are full width by default and as tall as their content. Prefer max-width and min-height over fixed sizes so content can grow and the layout adapts to small screens.
.container { max-width: 1200px; margin-inline: auto; }
.hero { min-height: 60vh; }
img { max-width: 100%; height: auto; }
.avatar { width: 48px; aspect-ratio: 1; }Dimension Units
Absolute
- • Pixels (px)
- • Points (pt)
- • Inches (in)
- • Centimeters (cm)
Relative
- • Percentages (%)
- • Em units (em)
- • Rem units (rem)
- • Character (ch)
Viewport
- • Viewport width (vw)
- • Viewport height (vh)
- • Viewport min (vmin)
- • Viewport max (vmax)
Intrinsic
- • Auto
- • Fit-content
- • Max-content
- • Min-content
Height & Width Demo
Explore different dimension techniques including fixed sizes, percentages, viewport units, constraints, and box-sizing. Hover over elements to see their exact dimensions!
Code Editor
<div class="fixed">width: 200px</div>
<div class="fluid">max-width: 400px (shrinks on small screens)</div>
<div class="fit">width: fit-content</div>
<div class="tall">min-height grows with content. Add more text here and the box expands instead of overflowing.</div>Preview
Box-Sizing Property
How dimensions are calculated
Content-Box (Default)
Width/height applies only to content. Padding and border are added outside.
Border-Box (Recommended)
Width/height includes content, padding, and border. More predictable sizing.
Universal Box-Sizing Reset
Constraints & Responsive Sizing
Min/Max Constraints
Viewport Units
Modern Aspect Ratio
The aspect-ratio property is perfect for responsive media and maintaining proportions.
Your Learning Path
Master CSS dimensions and responsive design
What You've Learned
- ▶Height and width properties and units
- ▶Box-sizing and dimension calculation
- ▶Min/max constraints for responsive design
- ▶Viewport units and modern sizing
- ▶Aspect-ratio property for proportions
What's Next
- ▶CSS Box Model - Complete layout system
- ▶CSS Text - Typography and text styling
- ▶CSS Fonts - Font control and web fonts
- ▶CSS Display - Element display modes
Pro Dimension Tip
Always use border-box sizing for predictable layouts. Combine percentage widths with max-width constraints for truly responsive designs that work on all screen sizes.
CSS Height and Width — step by step
Fixed vs Fluid Sizing
Fixed sizes (px) are predictable but inflexible. Fluid sizes (%, vw, vh) adapt to the viewport — prefer fluid sizing for responsive layouts.
🎯 Quick Check
Which unit always equals the full viewport height regardless of parent height?
CSS Height and Width: summary
Key points
- A percentage height only works if the parent has an explicit height.
- A fixed
heighton a text container causes overflow when the text grows; usemin-height. width: fit-content,min-contentandmax-contentsize an element from its content.- Inline elements such as
spanignorewidthandheight.
Frequently asked questions
›Why is height: 100% not working?
A percentage height is relative to the parent's height. If the parent's height is auto (from its content), the percentage has nothing to resolve against. Give the parent a height, or use min-height: 100vh or a flex/grid layout.
›What is the difference between width and max-width?
width fixes the size. max-width lets the element shrink below that size on small screens but never grow past it, which makes it the better choice for responsive containers.