CSS Basics

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: 200px; padding: 20px; border: 10px solid; /* Total width: 260px */

Width/height applies only to content. Padding and border are added outside.

Border-Box (Recommended)

box-sizing: border-box; width: 200px; padding: 20px; border: 10px solid; /* Total width: 200px */

Width/height includes content, padding, and border. More predictable sizing.

Universal Box-Sizing Reset

/* Apply border-box to all elements */ *, *::before, *::after { box-sizing: border-box; }

Constraints & Responsive Sizing

Min/Max Constraints

/* Responsive with limits */ .container { width: 100%; min-width: 320px; /* Mobile minimum */ max-width: 1200px; /* Desktop maximum */ margin: 0 auto; } /* Flexible height */ .card { min-height: 200px; /* Minimum size */ max-height: 80vh; /* Viewport limit */ height: auto; /* Content-based */ }

Viewport Units

/* Viewport-relative sizing */ .hero { width: 100vw; /* Full viewport width */ height: 100vh; /* Full viewport height */ } .sidebar { width: 25vw; /* 25% of viewport width */ min-width: 250px; /* Minimum width */ } /* Dynamic viewport units */ .mobile-hero { height: 100dvh; /* Dynamic viewport height */ width: 100dvw; /* Dynamic viewport width */ } /* Viewport minimums and maximums */ .square { width: 20vmin; /* 20% of smaller dimension */ height: 20vmin; /* Perfect square */ max-width: 20vmax; /* Limited by larger dimension */ }

Modern Aspect Ratio

/* CSS aspect-ratio property */ .video-container { width: 100%; aspect-ratio: 16/9; /* Maintains 16:9 ratio */ } .thumbnail { width: 200px; aspect-ratio: 1; /* Perfect square */ } .card-image { width: 100%; aspect-ratio: 4/3; /* 4:3 ratio */ object-fit: cover; /* For images */ }

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

1 / 4
1

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.

1 of 4
💡max-width Instead of width for Responsive Containers
Using max-width: 1200px with width: 100% gives you a container that fills the screen on small devices and caps at 1200px on large ones — more flexible than setting a fixed width.

🎯 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 height on a text container causes overflow when the text grows; use min-height.
  • width: fit-content, min-content and max-content size an element from its content.
  • Inline elements such as span ignore width and height.

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.