Width in Tailwind CSS
Width utilities in Tailwind control exactly how wide an element
renders. The system has four distinct groups:
fixed pixel widths using the 4px scale (w-64 = 256px), fractional widths relative to the parent
(w-1/2 = 50%),
content-based widths that adapt to what's inside
(w-fit,
w-min,
w-max), and
maximum-width constraints (max-w-5xl). Understanding when to reach for each group is the foundation
of every layout you'll build in Tailwind.
w-4,
w-8, w-16, w-64
w-1/2,
w-1/3, w-2/3, w-3/4,
w-full
w-fit,
w-min, w-max, w-auto
max-w-sm through
max-w-screen-2xl
w-full (100% of
parent) and w-screen (100vw)
w-full md:w-1/2 lg:w-1/3 with no media queries
What is Width? — Four Sizing Groups
Every HTML element has a natural width — block elements stretch to
fill their container, inline elements shrink to fit their content.
Tailwind's w-*
utilities let you override that default precisely. There are four
groups of width values, each solving a different layout problem:
fixed scale for exact sizes, fractions for proportional columns,
content-sizing for adaptive elements, and max-width for capped
containers.
w-1–w-96: formula is class number × 4 = pixels.
w-16 = 64px,
w-64 = 256px. Use for
icons, avatars, thumbnails, and fixed UI elements.
w-1/2,
w-1/3,
w-2/3,
w-1/4,
w-3/4,
w-full. Relative to the
parent container's width. Use for column layouts inside flex
or grid parents.
w-fit (shrink-wrap),
w-min (minimum content
width), w-max (maximum
content width),
w-auto (browser decides).
Use for badges, chips, and adaptive text containers.
max-w-sm through
max-w-screen-2xl. Caps
the maximum width — the element can be narrower but never
wider. Pair with
mx-auto for centred page
containers.
width: 100% — 100% of the
element's parent container. If the parent is 600px wide,
the element is 600px. w-screen =
width: 100vw — 100% of the browser viewport,
regardless of the parent. Use w-screen for full-width
hero banners and modals; use w-full for elements that
should fill their container.
All Width Utilities — Complete Reference
Every w-* class maps to a
specific CSS width value.
Below are the most important categories, from the 4px fixed scale
to special keyword values.
overflow-x-hidden to
the parent when needed.
w-1/2 = 50%, w-1/3 = 33.333%,
w-2/3 = 66.666%, w-1/4 = 25%,
w-3/4 = 75%. Use inside flex or grid containers
for proportional column layouts.
w-4 = 16px,
w-16 = 64px, w-32 = 128px,
w-64 = 256px. Use for icons, avatars, fixed
sidebars, and UI chrome elements.
w-[300px],
w-[50%], w-[24rem],
w-[calc(100%-2rem)]. Any valid CSS width value
inside square brackets. Use when the scale doesn't hit the
exact size a design specifies.
<!-- w-full: input fills its container -->
<input type="text"
class="w-full px-4 py-2.5 border border-gray-300 rounded-lg outline-none
focus:ring-2 focus:ring-cyan-400"
placeholder="Your name">
<!-- w-16: icon square (64px) -->
<div class="w-16 h-16 rounded-full bg-cyan-600 flex items-center justify-center">
<iconify-icon icon="solar:user-bold" width="28"></iconify-icon>
</div>
<!-- w-fit: badge shrinks to label width -->
<span class="w-fit px-3 py-1 bg-green-100 text-green-800 text-xs
font-medium rounded-full">Beginner</span>
<!-- w-1/2: two-column flex row -->
<div class="flex gap-4">
<div class="w-1/2 p-4 bg-gray-800 rounded-xl">Left column</div>
<div class="w-1/2 p-4 bg-gray-800 rounded-xl">Right column</div>
</div>
<!-- w-[N]: arbitrary exact width -->
<div class="w-[340px] p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">
Sidebar panel — exactly 340px wide
</div>
Fractional & Responsive Widths — Column Layouts
Fractional widths are percentage-based relative to the parent
container. They are the backbone of column layouts inside flex
rows: combine
w-full on mobile with
md:w-1/2 or
lg:w-1/3 to make layouts that
stack vertically on small screens and sit side-by-side on larger
ones — all without writing a single media query.
<!-- Responsive cards: stacked → 2 cols → 3 cols -->
<div class="flex flex-wrap gap-6">
<div class="w-full sm:w-[calc(50%-12px)] lg:w-[calc(33.333%-16px)]
p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">
Card 1
</div>
<!-- Use gap-aware grid instead for cleaner code: -->
</div>
<!-- Cleaner: grid approach (recommended over flex + fractional) -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">Card 1</div>
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">Card 2</div>
<div class="p-6 bg-white dark:bg-gray-800 rounded-2xl shadow">Card 3</div>
</div>
<!-- Classic sidebar + main layout -->
<div class="flex flex-col md:flex-row gap-6">
<aside class="w-full md:w-1/3 p-6 bg-gray-800 rounded-xl">Sidebar</aside>
<main class="w-full md:w-2/3 p-6 bg-gray-800 rounded-xl">Main content</main>
</div>
<!-- Responsive width: same element, different width per breakpoint -->
<div class="w-full sm:w-3/4 md:w-1/2 lg:w-1/3 mx-auto
p-6 bg-white rounded-2xl shadow text-center">
Narrows as screen grows
</div>
Max-width — Constrained & Centred Containers
max-w-* sets the maximum width
an element can reach. The element can be any width up to that cap
— on small screens it will naturally be narrower. The universal
page-container pattern is
max-w-5xl mx-auto px-4: capped
at 64rem, centred with auto margins, and guttered with padding on
small screens.
max-w-2xl hits that range in
normal body text.
img class="max-w-full h-auto".
<!-- Standard page container — the pattern used on every page -->
<div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
All page content lives here — centred, capped, guttered
</div>
<!-- Prose / article — optimal reading width -->
<article class="max-w-2xl mx-auto px-4 py-8 prose dark:prose-invert">
Long-form content...
</article>
<!-- Modal / dialog -->
<div class="fixed inset-0 flex items-center justify-center p-4">
<div class="w-full max-w-lg bg-white dark:bg-gray-800 rounded-2xl
shadow-2xl p-6">
Dialog content
</div>
</div>
<!-- Prevent image overflow in container -->
<img src="banner.jpg" alt="Banner"
class="w-full max-w-full h-auto rounded-xl">
<!-- Login form — narrow focussed width -->
<form class="max-w-sm mx-auto p-8 bg-white dark:bg-gray-800
rounded-2xl shadow space-y-4">
<input class="w-full px-4 py-2.5 border rounded-lg" type="email" placeholder="Email">
<button class="w-full py-2.5 bg-cyan-500 text-white rounded-lg font-semibold">Sign in</button>
</form>
Content-based Widths — w-fit, w-min, w-max, w-screen
These special keywords let the element's own content — rather than a fixed number or percentage — determine its width. They solve the age-old problem of making a block element take only as much width as its content needs.
<!-- w-fit: badge / tag — takes only the space it needs -->
<div class="w-fit px-3 py-1 bg-cyan-900 text-cyan-300
rounded-full text-sm font-medium">
Tailwind CSS
</div>
<!-- w-fit: centre a block element without a fixed width -->
<div class="w-fit mx-auto">
<img src="logo.svg" alt="Logo" class="h-12">
</div>
<!-- w-fit mx-auto = centre any element by its content size -->
<!-- w-min: tooltip that collapses to shortest line -->
<div class="w-min bg-gray-900 text-white text-xs px-2 py-1 rounded">
Copy to clipboard
</div>
<!-- Each word wraps independently to find the minimum width -->
<!-- w-screen: full-viewport hero section -->
<section class="w-screen min-h-screen flex items-center justify-center
bg-gradient-to-br from-cyan-900 to-blue-950">
<div class="max-w-4xl mx-auto px-4 text-center">
<h1 class="text-5xl font-bold text-white">Learn Tailwind CSS</h1>
</div>
</section>
w-max expands to the maximum content width —
it prevents any text wrapping and can overflow its parent.
w-fit also adapts to content, but respects the
available space — text will still wrap if the content is wider
than the container. For badges and buttons,
prefer w-fit. Use w-max only when you explicitly need no line
breaks (single-line chip labels, monospace code tags).
Quick Reference — All Width Utilities
| Class | CSS Property | Notes |
|---|---|---|
w-0 |
width: 0px |
Zero width |
w-px |
width: 1px |
Hairline divider |
w-0.5 |
width: 0.125rem |
2px |
w-1 … w-96 |
N × 0.25rem |
4px scale — most used range ★ |
w-auto |
width: auto |
Browser-determined width ★ |
w-full |
width: 100% |
100% of parent — most used ★★ |
w-screen |
width: 100vw |
Full viewport width ★ |
w-svw |
width: 100svw |
Small viewport width (mobile) |
w-fit |
width: fit-content |
Shrink-wrap to content ★★ |
w-min |
width: min-content |
Minimum text wrapping width ★ |
w-max |
width: max-content |
No wrapping, may overflow ★ |
w-1/2 |
width: 50% |
Half parent ★★ |
w-1/3 |
width: 33.333% |
One third ★★ |
w-2/3 |
width: 66.667% |
Two thirds ★ |
w-1/4 |
width: 25% |
Quarter width ★ |
w-3/4 |
width: 75% |
Three quarters ★ |
w-1/5 … w-11/12 |
Various % | All fractional steps |
w-[N] |
Custom | Arbitrary: w-[340px] ★ |
max-w-sm |
max-width: 24rem |
384px — forms, modals ★ |
max-w-lg |
max-width: 32rem |
512px — dialogs ★ |
max-w-2xl |
max-width: 42rem |
672px — prose ★ |
max-w-4xl |
max-width: 56rem |
896px ★ |
max-w-5xl |
max-width: 64rem |
1024px — page container ★★ |
max-w-7xl |
max-width: 80rem |
1280px — dashboard ★ |
max-w-full |
max-width: 100% |
Prevent overflow ★ |
max-w-[N] |
Custom | Arbitrary: max-w-[760px] |
sm:w-* |
At ≥640px | Responsive prefix ★ |
md:w-* |
At ≥768px | Responsive prefix ★ |
lg:w-* |
At ≥1024px | Responsive prefix ★ |
★ = commonly used • ★★ = used on virtually every project
-
Four width groups — fixed scale (
w-64 = 256px), fractional (w-1/2 = 50%), content-based (w-fit), and max-width constraints (max-w-5xl). Each group solves a different layout scenario. -
w-full vs w-screen —
w-full= 100% of the parent (most common).w-screen= 100vw of the browser viewport, regardless of the parent — use for full-bleed heroes and modals. -
Fixed scale — formula: class number × 4 =
pixels.
w-4 = 16px,w-16 = 64px,w-64 = 256px. Identical formula to padding and margin. Use for icons, avatars, and fixed UI elements. -
Fractional widths —
w-1/2,w-1/3,w-2/3,w-3/4are percentages of the parent container. Combine with responsive prefixes:w-full md:w-1/2 lg:w-1/3for column layouts that stack on mobile. -
w-fit —
fit-content— the most useful content-based width. Makes any block element shrink-wrap its content without becoming inline. Standard for badges, pills, and button variants. - max-w-5xl mx-auto — the universal page container pattern on the web. Caps content at 1024px, centres it with auto margins, and stays responsive at any viewport narrower than that.
-
Arbitrary values —
w-[340px],max-w-[760px],w-[calc(100%-2rem)]. Any valid CSS length works inside square brackets when the standard scale doesn't match your design exactly. -
Next chapter — Height covers
h-*,h-full,h-screen,min-h-*,max-h-*, and the new viewport unitsh-svhandh-dvh.
Quick Check — Questions & Answers
Test your understanding. Click a question to reveal the answer.
w-full sets width: 100% —
100% of the element's parent container. If the
parent is 800px, the element is 800px. This is the correct
choice for almost every full-width element inside a
layout.
w-screen sets width: 100vw —
100% of the browser viewport, regardless of what
the parent's width is. This can cause horizontal overflow
if the parent has padding, because the viewport width
includes that padding zone. Use it for full-bleed hero
banners that intentionally break out of their container,
or for modal overlay backdrops.
w-fit sets width: fit-content —
the element shrinks to wrap around its content while still
obeying the available space (text will wrap if content
would be wider than the parent).
It's the block-level equivalent of making an element
inline — it takes only as much horizontal space as its
content needs, but remains a block element. Use it for
badges, pill labels, chip components, centring an image
with w-fit mx-auto, or any block element that
should not stretch to fill its container.
Fractional widths are percentages of the
parent element's width.
w-1/2 = width: 50%,
w-1/3 = width: 33.333%,
w-3/4 = width: 75%.
They work best inside flex containers:
<div class="flex gap-4"><div
class="w-1/3">Sidebar</div><div
class="w-2/3">Main</div></div>
creates a sidebar and main area that together fill the
parent. Remember to account for gap — the fractions add up
to 100%, but any gap will push the total over, so you may
need w-[calc(50%-12px)] or just use a grid
instead.
w-* sets an exact width — the
element is that width regardless of screen size (within
reason). w-64 = always exactly 256px wide.
max-w-* sets a maximum — the
element can be narrower, but never wider than the cap. On
a small screen a max-w-5xl container will be
as narrow as the viewport; on large screens it stops at
1024px. This is what makes
max-w-5xl mx-auto the universal
page-container pattern: responsive by default, no extra
code needed.
Two approaches — grid (recommended) and flex with responsive widths:
Grid approach:
<div class="grid grid-cols-1 md:grid-cols-2
gap-6">
— one column on mobile, two on md+. No width classes
needed on children.
Flex approach:
<div class="flex flex-col md:flex-row
gap-6"><div class="w-full
md:w-1/2">...</div><div class="w-full
md:w-1/2">...</div></div>
— column on mobile, row on md+, each child at 50%.
Grid is cleaner because children don't need width classes. Use flex + widths when you need asymmetric sizes like a 1/3 sidebar and 2/3 main area.
MCQ Quiz — Test Yourself
Select the best answer. Correct answers are revealed on click.
w-full
and
w-screen?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.