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.
width and height — values and unitsmin-width, max-width, min-height, max-heightvw, vh, dvh, svhfit-content,
min-content, max-content
aspect-ratio — responsive
proportional sizingwidth & 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:
/* ── 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 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.
/* ── 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.
/* 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 */
}
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 — 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.
/* 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 — 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:
max-width: 1200px;
margin: 0 auto;
height: auto;
display: block;
min-height: 100dvh;
dvh handles mobile browser bars.aspect-ratio: 16/9;
max-height: 80vh;
overflow-y: auto;
aspect-ratio: 1;
border-radius: 50%;
aspect-ratio: 1 keeps it square
when width is set./* ── 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 */
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.
Quick Check — Chapter 15
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.
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: 100vh2. Cascade
height: 100% all the way up from html and body3. Use
min-height: 100dvh for full-screen sections — no parent
cascade needed4. 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 browsersheight: 100dvh; ← overrides if browser supports itsvh (small viewport height) and lvh (large viewport height) are also available for more specific use
cases.
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.
aspect-ratio: 1 do?width: min(90vw, 560px) do?widthandheightaccept px (fixed), % (relative to parent), rem (root-relative), vw/vh (viewport-relative) andauto.- Responsive container pattern:
width: 100%; max-width: 1200px; margin: 0 auto;— fluid on small screens, capped on large. max-width: 100%; height: autoon images prevents overflow and preserves aspect ratio.min-heightprevents content collapse;max-heightwithoverflow: autocreates scrollable containers.- Viewport units:
vw/vhare percentages of the viewport. Usedvh(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/9maintains proportional sizing — cleaner than the old padding-bottom hack. Combine withobject-fit: coverfor images.min(),max(),clamp()enable fluid sizing without media queries:width: min(90vw, 560px)is the cleanest modal sizing pattern.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.