CSS Height and Width
Control how big elements are on screen. Learn to use pixels, percentages, auto, viewport units (vw, vh), and min/max constraints to build flexible, responsive layouts — with full code examples and live browser output.
- What are height & width?
- The 6 Properties
- width Property
- height Property
- Size Units
- auto Value
- min/max-width & height
- Percentage Width
- Viewport Units (vw, vh)
- Complete Example
- Common Mistakes
- Try It
- FAQ
What are CSS Height and Width?
The CSS width and height properties control how large an element is on screen. By default, block-level elements like <div> and <p> stretch to fill the full width of their container, and their height is determined by the content inside them.
CSS lets you override both of these defaults — setting exact sizes, percentage-based proportions, or responsive limits using min and max values. These properties are used on virtually every real webpage you'll ever build.
The 6 Height & Width Properties
CSS gives you six properties to control element sizing. Two set the size directly; four set constraints that keep sizing responsive and flexible:
width
Sets the horizontal size of an element.
height
Sets the vertical size of an element.
min-width
Element won't shrink narrower than this.
max-width
Element won't grow wider than this.
min-height
Element won't become shorter than this.
max-height
Element won't grow taller than this.
The width Property
width — Set horizontal size
The width property sets the horizontal size of an element. By default, block elements like <div> take 100% of their parent's width. Use this property to override that and set a specific size using any CSS unit.
<!DOCTYPE html> <html lang="en"> <head> <style> .full { width: 100%; background: #dbeafe; padding: 8px; } .half { width: 50%; background: #ede9fe; padding: 8px; } .fixed { width: 200px; background: #dcfce7; padding: 8px; } </style> </head> <body> <div class="full">width: 100%</div> <div class="half">width: 50%</div> <div class="fixed">width: 200px</div> </body> </html>
The height Property
height — Set vertical size
The height property sets the vertical size of an element. By default an element is as tall as its content. Overriding this with a fixed height can cause overflow if the content is taller than the set value — handle this with overflow: auto or overflow: hidden.
<!DOCTYPE html> <html lang="en"> <head> <style> div { height: 100px; background: #ede9fe; padding: 8px; } nav { height: 60px; background: #7c3aed; color: white; padding: 8px; } main { height: auto; background: #f1f5f9; padding: 8px; } </style> </head> <body> <nav>Fixed 60px nav bar</nav> <div>Fixed 100px box</div> <main>auto height — grows with content</main> </body> </html>
CSS Size Units
CSS supports many different units for sizing. The right unit depends on whether you want a fixed, relative, or viewport-responsive size:
The auto Value
width: auto / height: auto
The auto value tells the browser to calculate the size automatically based on the element's content and context. For height, auto means the element grows as tall as its content needs — which is the default. For images, combining width: 100% with height: auto keeps the correct aspect ratio on any screen size.
You can also use margin: 0 auto to horizontally centre a fixed-width block element — the browser splits the remaining space equally on both sides.
<!DOCTYPE html> <html lang="en"> <head> <style> /* Centre a fixed-width block element */ .container { width: 600px; margin: 0 auto; /* auto = equal left & right margin */ background: #ede9fe; padding: 16px; } /* Responsive image — keeps aspect ratio */ img { width: 100%; height: auto; } </style> </head> <body> <div class="container">This div is centred!</div> </body> </html>
min-width, max-width, min-height, max-height
These four constraint properties let you set boundaries on how small or large an element can become. They're essential for responsive design — especially the max-width pattern used on page wrappers everywhere.
<!DOCTYPE html> <html lang="en"> <head> <style> /* Responsive page container — used by TipsInLearning */ .page-wrapper { width: 100%; /* fills screen on mobile */ max-width: 1200px; /* caps at 1200px on large screens */ margin: 0 auto; /* stays centred */ background: #f1f5f9; padding: 16px; } /* Card with height boundaries */ .card { width: 280px; min-height: 120px; /* always at least this tall */ max-height: 300px; /* won't overflow beyond this */ overflow: auto; /* scroll if content overflows */ background: #ede9fe; padding: 12px; } /* Button that never gets too narrow */ .btn { min-width: 120px; padding: 8px 16px; background: #2563eb; color: white; } </style> </head> <body> <div class="page-wrapper"> <div class="card">Card with min/max height</div> <button class="btn">Click Me</button> </div> </body> </html>
Percentage Width
Percentage widths are relative to the parent element
When you set width: 50%, the element takes 50% of its parent's width — not the screen width. This makes percentage widths perfect for creating flexible, proportional layouts like sidebars, multi-column grids, and fluid content areas.
<!DOCTYPE html> <html lang="en"> <head> <style> .layout { display: flex; gap: 12px; } .sidebar { width: 30%; background: #ede9fe; padding: 12px; } .main { width: 70%; background: #dbeafe; padding: 12px; } </style> </head> <body> <div class="layout"> <aside class="sidebar">30% Sidebar</aside> <main class="main">70% Main Content</main> </div> </body> </html>
Viewport Units — vw and vh
vw / vh — Relative to screen size, not parent
1vw = 1% of the viewport (browser window) width. 1vh = 1% of the viewport height. These units are relative to the screen, not the parent element — which makes them powerful for full-screen hero sections and truly responsive font sizes that scale with the window.
<!DOCTYPE html> <html lang="en"> <head> <style> /* Full-screen hero section */ .hero { width: 100vw; /* always full screen width */ height: 100vh; /* always full screen height */ background: #001233; color: white; display: flex; align-items: center; justify-content: center; } /* Responsive heading that scales with window */ h1 { font-size: 5vw; } </style> </head> <body> <div class="hero"> <h1>Full Screen Hero</h1> </div> </body> </html>
Complete Example
This example combines all six sizing properties in a realistic page layout — the same pattern used here on TipsInLearning:
<!DOCTYPE html> <html lang="en"> <head> <style> /* Responsive page wrapper */ .page { width: 100%; max-width: 1200px; margin: 0 auto; } /* Full-screen hero */ .hero { width: 100%; height: 60vh; background: #001233; color: white; } /* Fixed-height navbar */ .nav { width: 100%; height: 64px; background: #0e0521; } /* Cards with min/max height */ .card { width: 300px; min-height: 150px; max-height: 300px; overflow: auto; } /* Responsive image */ img { width: 100%; height: auto; } /* Sidebar layout */ .layout { display: flex; } .sidebar { width: 25%; min-width: 200px; } .main { width: 75%; } </style> </head> <body> <div class="page"> <nav class="nav"></nav> <div class="hero">Hero Section</div> <div class="layout"> <aside class="sidebar">Sidebar</aside> <main class="main">Main Content</main> </div> </div> </body> </html>
Common Mistakes
If you set height: 200px on a div and later add more text, the text overflows outside the box. Use height: auto (the default) or min-height: 200px instead — the element will grow with its content while still starting at a minimum size.
If you set width: 100% on an image but leave height unset or use a fixed value, the image will appear stretched or squashed. Always pair width: 100% with height: auto to preserve the aspect ratio.
Writing width: 1200px means the element is exactly 1200px wide on every screen — including 320px phones, where it will cause horizontal scrolling. Write width: 100%; max-width: 1200px instead so it fills smaller screens and caps on large ones.
height: 50% only works if the parent element has a defined height. If the parent height is auto (which it is by default), percentage height on the child has no reference value and will behave like auto. Use vh units or flexbox instead for full-height sections.
Questions About CSS Height & Width
The most common questions beginners ask about sizing elements in CSS: