Tutorials CSS Width & Height
Ch 15 / 60
Chapter 15 Beginner 7 min read CSS

CSS Width & Height

Controlling how big elements are is one of the most fundamental skills in CSS layout. From fixed pixels to fluid percentages, viewport units and smart constraints — understanding the full sizing toolkit is what makes layouts responsive by default.

What you'll learn
width and height — values and units
min-width, max-width, min-height, max-height
Viewport units — vw, vh, dvh, svh
Intrinsic sizing — fit-content, min-content, max-content
aspect-ratio — responsive proportional sizing
Responsive sizing patterns — containers, images, cards

width & height — Basics

The width and height properties control the size of an element's content area (or the full element with border-box). They accept a wide range of value types:

px
Fixed — 320px
fixed
%
60% of parent
relative
rem
20rem = 320px
root-rel
vw
100vw = full viewport
viewport
auto
fills available space (block)
browser
CSS — width & height values
/* ── Fixed values ── */
.box     { width: 300px;  height: 200px; }
.icon    { width: 2rem;  height: 2rem; }   /* 32px × 32px */

/* ── Percentage — relative to PARENT width ── */
.half    { width: 50%; }   /* 50% of parent's width */
.full    { width: 100%; }  /* same as parent width */

/* ── Viewport units ── */
.hero    { width: 100vw; height: 100vh; } /* full screen */

/* ── auto ── */
div      { width: auto; }  /* block: fills available space */
img      { width: auto; }  /* replaced: uses natural dimensions */

/* ── Height behaviour ── */
.card    { height: auto; }  /* grows with content — default */
.fixed-h { height: 400px; overflow: auto; } /* scroll if content overflows */

/* ── % height needs an ancestor with defined height ── */
html, body { height: 100%; }  /* required for % height to work */
.full-page  { height: 100%; }
% height gotcha
Percentage height only works when the parent has a defined height. A parent with height: auto (the default) makes height: 50% on a child have no effect. Use 100vh or dvh for full-viewport heights instead of cascading height: 100%.

min-width, max-width, min-height, max-height

Constraint properties let an element be flexible within a range — never too small, never too large. They are the foundation of responsive design.

min-width: 200px
200px minimum
Element won't shrink below 200px even if parent is narrower
max-width: 600px
600px max
Element won't grow wider than 600px even if parent is wider
width: 100% + max-width
capped
Fluid up to max-width — the most common responsive pattern
CSS — constraint properties
/* ── Responsive container (most used pattern) ── */
.container {
  width:     100%;       /* fluid on small screens */
  max-width: 1200px;    /* cap on large screens */
  margin:    0 auto;    /* centre horizontally */
  padding:   0 clamp(16px, 4vw, 40px);
}

/* ── min-width — prevents text collapse ── */
.btn       { min-width: 120px; }   /* never too narrow for the text */
.sidebar   { min-width: 200px; }

/* ── max-width on images ── */
img         { max-width: 100%; height: auto; }  /* never overflow parent */

/* ── min-height — prevents collapsed cards ── */
.card      { min-height: 200px; } /* at least 200px tall even if little content */
.modal     { min-height: 300px; max-height: 80vh; overflow: auto; }

/* ── max-height — scrollable containers ── */
.dropdown  { max-height: 300px; overflow-y: auto; }
.sidebar   { max-height: 100vh; overflow-y: auto; }

Viewport Units — vw, vh, dvh, svh

Viewport units are relative to the browser window size. They are essential for full-screen layouts, fluid typography and sections that need to fill the screen regardless of content.

vw
Viewport Width
1vw = 1% of the browser window width. 100vw = full window width.
vh
Viewport Height
1vh = 1% of the browser window height. 100vh = full window height.
dvh
Dynamic Viewport Height
Updates when mobile browser bars show/hide. Solves the iOS Safari 100vh bug.
svh
Small Viewport Height
The smallest possible viewport height (browser bar fully visible). Safe for all states.
lvh
Large Viewport Height
The largest possible viewport height (browser bar hidden/scrolled away).
vmin
Viewport Min
1% of whichever is smaller — viewport width or height. Great for squares.
vmax
Viewport Max
1% of whichever is larger — viewport width or height.
cqi
Container Inline
1% of the nearest container's inline size. CSS Container Queries (2023).
CSS — Viewport unit patterns
/* Full-screen hero section */
.hero {
  width:  100vw;
  height: 100dvh;  /* dvh: accounts for mobile browser bars */
}

/* Fluid typography */
h1 { font-size: 5vw; }       /* scales with viewport width */
h1 { font-size: clamp(1.8rem, 5vw, 4rem); }  /* clamped fluid */

/* Sticky sidebar that fills height */
.sidebar {
  height:    100vh;
  position: sticky;
  top:       0;
  overflow-y:auto;
}

/* vmin for a responsive square */
.square  { width: 50vmin; height: 50vmin; }

/* iOS Safari 100vh fix — use dvh with vh fallback */
.hero {
  height: 100vh;    /* fallback for older browsers */
  height: 100dvh;   /* overrides if browser supports dvh */
}
The 100vh mobile bug
On iOS Safari, 100vh equals the largest possible viewport height (browser bar hidden) — so a 100vh hero can be taller than the visible screen when the address bar is showing. 100dvh (dynamic viewport height) solves this by updating when the browser chrome shows or hides. Always provide a 100vh fallback for older browser support.

Intrinsic Sizing — fit-content, min-content, max-content

CSS provides keyword values that size elements based on their own content — without fixed numbers. These are especially useful for buttons, tags and grid columns.

max-content
This text defines the full natural width
min-content
Narrowest possible without overflow
fit-content
Shrink-wraps to content, up to available space
fit-content()
fit-content(200px) — capped at 200px
CSS — Intrinsic sizing keywords
/* max-content — element as wide as its largest content */
.btn      { width: max-content; } /* button fits its text exactly */

/* min-content — element as narrow as possible without overflow */
.narrow   { width: min-content; } /* text wraps at every word break */

/* fit-content — shrink-wraps to content, up to available space */
.tag      { width: fit-content; }  /* tag pill wraps its text */
.tooltip  { width: fit-content; }  /* tooltip matches text width */

/* fit-content(max) — shrink-wrap but cap at a maximum */
.label    { width: fit-content(200px); }

/* Useful in CSS Grid columns */
.grid {
  display: grid;
  grid-template-columns: fit-content(200px) 1fr;
  /* first column: max 200px, second: takes remaining space */
}

aspect-ratio — Proportional Sizing

The aspect-ratio property maintains a fixed width-to-height ratio as the element resizes. Set one dimension and the other is calculated automatically. This replaced the old padding-bottom hack cleanly.

1:1
Square
aspect-ratio: 1/1
16:9
Widescreen
aspect-ratio: 16/9
4:3
Classic
aspect-ratio: 4/3
3:4
Portrait
aspect-ratio: 3/4
2:1
Banner
aspect-ratio: 2/1
CSS — aspect-ratio
/* Set width, height calculated from ratio */
.video      {
  width:        100%;
  aspect-ratio: 16 / 9; /* height = width × (9/16) */
}

/* Avatar circle */
.avatar     {
  width:        48px;
  aspect-ratio: 1;        /* shorthand for 1/1 */
  border-radius:50%;
}

/* Card thumbnail */
.thumbnail  {
  width:        100%;
  aspect-ratio: 4 / 3;
  object-fit:   cover; /* fill without distorting */
}

/* Allow content to override the ratio if needed */
.flexible   {
  aspect-ratio: 16 / 9;
  min-height:   200px; /* override if content is taller */
}

width: auto — How It Actually Behaves

auto is the default for width and height, but it behaves differently depending on the element type:

Block element (div) — width: auto fills available space
Block element — stretches to 100% of parent minus margins
Inline-block / float — width: auto shrinks to content
Inline-block shrinks to content
Absolute / fixed — width: auto shrinks to content
Absolute: width = content width
CSS — auto behaviour by context
/* Block — auto fills the available horizontal space */
div            { width: auto; } /* = 100% of parent minus margins */

/* Inline-block — auto shrinks to content */
.tag           { display: inline-block; width: auto; }

/* Absolute positioned — auto shrinks to content */
.tooltip       { position: absolute; width: auto; }

/* Flex item — auto uses flex sizing rules */
.flex-item     { flex: 1; width: auto; } /* grows to fill flex container */

/* height: auto — always grows with content */
.card          { height: auto; } /* default — content-driven */

/* Replaced elements (img, video) — auto preserves aspect ratio */
img            { width: 200px; height: auto; } /* proportional */

Essential Responsive Sizing Patterns

These are the most important and commonly used width/height patterns in real projects:

Page Container
width: 100%;
max-width: 1200px;
margin: 0 auto;
The universal responsive container. Fluid on mobile, capped on large screens, always centred.
Responsive Image
max-width: 100%;
height: auto;
display: block;
Never overflows its container. Preserves natural aspect ratio. Block removes inline spacing gap.
Full-screen Section
width: 100%;
min-height: 100dvh;
At least full viewport tall, grows with content. dvh handles mobile browser bars.
Embed / Video
width: 100%;
aspect-ratio: 16/9;
Always 16:9, scales with parent width. Clean replacement for the old padding-bottom hack.
Scrollable Modal
width: min(90vw, 560px);
max-height: 80vh;
overflow-y: auto;
Fits screen on mobile, capped at 560px on desktop, scrollable if content overflows.
Avatar Circle
width: 48px;
aspect-ratio: 1;
border-radius: 50%;
Perfect circle. aspect-ratio: 1 keeps it square when width is set.
CSS — Patterns code
/* ── min() / max() / clamp() for sizing ── */

/* min() — use the smaller of two values */
.modal    { width: min(90vw, 560px); }
/* On 400px screen: min(360px, 560px) = 360px */
/* On 1200px screen: min(1080px, 560px) = 560px */

/* max() — use the larger of two values */
.sidebar  { width: max(200px, 20%); }
/* Never narrower than 200px */

/* clamp(min, preferred, max) — best for fluid sizing */
.container{ width: clamp(320px, 90%, 1200px); }
/* Never below 320px, never above 1200px */

h1         { font-size: clamp(1.8rem, 4vw, 3.5rem); }

/* ── object-fit for images ── */
.thumbnail {
  width:      100%;
  height:     200px;
  object-fit: cover;    /* fill, crop — no distortion */
}
/* object-fit values: fill | contain | cover | none | scale-down */
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
One printable page covering every CSS3 sizing property, unit, layout system and box model reference.
Download PDF

Try It Yourself

This demo shows all the major sizing patterns working together: a responsive container, clamped typography, a 16:9 video embed, a responsive image and a scrollable card. Resize your browser to see the fluid behaviour.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 15

Questions & Answers
width sets a fixed size. If you set width: 600px, the element is always 600px — even if the screen is 320px wide, causing overflow.

max-width sets an upper limit. The element can be narrower but never wider than the max-width value. It does not set a minimum size.

The most important responsive pattern combines both: width: 100%; max-width: 1200px; margin: 0 auto;

This means: fill the available width (fluid on small screens) but never exceed 1200px (cap on large screens). The margin: 0 auto centres the element when the viewport is wider than 1200px.
Percentage height is calculated relative to the parent element's height. If the parent has height: auto (the default), its height is determined by its children — creating a circular reference. The browser resolves this by treating percentage height as auto, so the rule has no effect.

Solutions:
1. Give the parent an explicit height: height: 400px or height: 100vh
2. Cascade height: 100% all the way up from html and body
3. Use min-height: 100dvh for full-screen sections — no parent cascade needed
4. Use Flexbox or Grid on the parent — then children can use align-self: stretch
vh (viewport height) is calculated as a percentage of the browser's full viewport height. On iOS Safari and some other mobile browsers, the viewport height changes when the browser address bar shows or hides. 100vh is calculated when the address bar is hidden (maximum height), so when the address bar is visible, a 100vh section is taller than the visible screen area — causing the bottom content to be hidden under the address bar.

dvh (dynamic viewport height) updates dynamically as the browser chrome shows and hides. 100dvh always equals the currently visible viewport height — no overflow.

Best practice:
height: 100vh; ← fallback for older browsers
height: 100dvh; ← overrides if browser supports it

svh (small viewport height) and lvh (large viewport height) are also available for more specific use cases.
All three are intrinsic sizing keywords that size an element based on its content:

max-content: element is as wide as its content's natural (unwrapped) width. Text does not wrap. Like the content is on a single infinite line.

min-content: element is as narrow as possible without overflowing — text wraps at every word break. The width equals the longest unbreakable word.

fit-content: like max-content but capped at the available space. Shrink-wraps to the content, then expands to fill available space — never beyond.

fit-content(200px): fit-content but with an explicit maximum cap of 200px.

Practical use: buttons with width: fit-content size themselves to their label text. Grid columns with grid-template-columns: min-content 1fr make the first column as narrow as its widest word.
aspect-ratio: 16/9 tells the browser to maintain a 16:9 width-to-height relationship. Set the width (or height), and the browser calculates the other dimension automatically.

The old padding-bottom hack worked by setting height: 0; padding-bottom: 56.25% and absolutely positioning content inside. This worked because percentage padding is relative to parent width — but it was hackish, required extra wrapper elements and made absolute positioning on content mandatory.

aspect-ratio advantages:
— Declarative and readable: aspect-ratio: 16/9 is immediately understandable
— Works naturally without extra wrappers or positioned children
— Can be combined with min-height to allow content to push beyond the ratio if needed
— Supported in all modern browsers since 2021

Always provide a fallback for very old browsers: keep the padding-bottom wrapper for browsers that don't support aspect-ratio.
Multiple Choice Questions
Question 1 of 5
What is the standard responsive container pattern in CSS?
Question 2 of 5
What viewport unit correctly handles iOS Safari's address bar showing/hiding?
Question 3 of 5
Which sizing keyword makes an element shrink-wrap its content but never exceed the available space?
Question 4 of 5
What does aspect-ratio: 1 do?
Question 5 of 5
What does width: min(90vw, 560px) do?
Chapter Summary
  • width and height accept px (fixed), % (relative to parent), rem (root-relative), vw/vh (viewport-relative) and auto.
  • Responsive container pattern: width: 100%; max-width: 1200px; margin: 0 auto; — fluid on small screens, capped on large.
  • max-width: 100%; height: auto on images prevents overflow and preserves aspect ratio.
  • min-height prevents content collapse; max-height with overflow: auto creates scrollable containers.
  • Viewport units: vw/vh are percentages of the viewport. Use dvh (dynamic) for full-screen sections on mobile to fix the iOS Safari 100vh bug.
  • Intrinsic keywords: max-content = natural width, min-content = narrowest without overflow, fit-content = shrink-wrap to content up to available space.
  • aspect-ratio: 16/9 maintains proportional sizing — cleaner than the old padding-bottom hack. Combine with object-fit: cover for images.
  • min(), max(), clamp() enable fluid sizing without media queries: width: min(90vw, 560px) is the cleanest modal sizing pattern.
~7 min read
Beginner level
Chapter 15 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel