Ch 19 / 45
Chapter 19 Beginner 6 min read Tailwind CSS

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.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
Set fixed pixel widths with the 4px scale: w-4, w-8, w-16, w-64
Use fractional widths relative to parent: w-1/2, w-1/3, w-2/3, w-3/4, w-full
Apply content-based widths: w-fit, w-min, w-max, w-auto
Constrain maximum width with max-w-sm through max-w-screen-2xl
Understand the difference between w-full (100% of parent) and w-screen (100vw)
Stack responsive widths: 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.

Four width groups — each solves a different layout problem 1. Fixed widths — 4px scale w-16 w-32 = 128px w-64 = 256px class number × 4 = px 2. Fractional — % of parent parent container (100%) w-1/2 = 50% w-1/3 = 33% w-3/4 = 75% 3. Content-based w-fit w-min w-max (expands fully) shrinks or grows to fit content 4. max-w-* — cap container width max-w-5xl = max 1024px pairs with mx-auto for centred containers
Fixed Scale
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.
Fractional
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.
Content-based
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-* 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.
w-full vs w-screen w-full = 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.
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

w-full
width: 100% — Fills 100% of the parent's width. The most-used width class on any page. Use for inputs, images, cards, and any element that should stretch to fill its container.
w-screen
width: 100vw — Fills 100% of the browser viewport, ignoring any parent container's width. Use for full-bleed hero sections and overlay backdrops. Can cause horizontal overflow — add overflow-x-hidden to the parent when needed.
w-auto
width: auto — Lets the browser determine the width using its normal layout algorithm. Block elements expand to fill; inline elements shrink to content. The CSS default — useful for resetting a fixed width.
w-1/2…w-11/12
Percentage of parent — 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-1…w-96
Fixed pixel widths (4px scale) — Formula: class number × 4 = pixels. w-4 = 16px, w-16 = 64px, w-32 = 128px, w-64 = 256px. Use for icons, avatars, fixed sidebars, and UI chrome elements.
w-fit
width: fit-content — Shrinks to fit content while respecting the available space. The block equivalent of an inline element — it takes only as much space as its content needs. Use for badges, buttons with dynamic labels, and pills.
w-[N]
Arbitrary value — 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.
HTML — essential width utilities in action
<!-- 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.

Fractional widths — percentage of parent container (parent = 100%) parent container (100% = 672px in this example) w-full = 100% w-3/4 = 75% w-2/3 = 66.7% w-1/2 = 50% w-1/3 = 33% w-1/4=25% w-1/5=20% …
w-full
mobile (stacked)
100% of parent
sm:w-1/2
≥ 640px — 2 cols
50% of parent
md:w-1/3
≥ 768px — 3 cols
33.3% of parent
lg:w-1/4
≥ 1024px — 4 cols
25% of parent
w-2/3
main content
66.7% of parent
w-1/3
sidebar
33.3% of parent
HTML — fractional and responsive width patterns
<!-- 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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-5xl mx-auto — centred container, capped width, responsive gutters browser viewport (any width) margin-left: auto margin-right: auto max-w-5xl mx-auto max 1024px wide • centred • never overflows px-4 px-4 Page content — always centred, always readable prose, cards, sections — all inside the container Common values: max-w-sm=384px max-w-md=448px max-w-lg=512px max-w-xl=576px max-w-2xl=672px max-w-4xl=896px max-w-5xl=1024px max-w-7xl=1280px
max-w-sm
max-width: 24rem (384px) — Use for narrow modals, login forms, newsletter sign-up boxes, and single-column content blocks like a poem or quote.
max-w-lg
max-width: 32rem (512px) — Standard card and dialog width. Wide enough for a form with labels and inputs, narrow enough to stay focused on mobile.
max-w-2xl
max-width: 42rem (672px) — Optimal reading width for prose. Research shows ~65–75 characters per line is easiest to read — max-w-2xl hits that range in normal body text.
max-w-5xl
max-width: 64rem (1024px) — The most common page container width on the web. Provides comfortable two or three-column layouts on desktop while staying readable on narrower screens.
max-w-7xl
max-width: 80rem (1280px) — Wide container for dashboards, data-heavy layouts, and designs that need maximum real-estate on large monitors.
max-w-full
max-width: 100% — Prevents overflow without enforcing a specific breakpoint. Use on images inside flex/grid cells: img class="max-w-full h-auto".
HTML — max-width container patterns
<!-- 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>
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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.

Content-based widths — element adapts to its own content parent container — all four elements are inside the same 672px container w-min shrinks to longest word min-content w-fit (badge) wraps content, no wider than parent fit-content w-max — expands as far as content needs no text wrapping; can overflow parent ⚠ can cause overflow max-content w-screen = 100vw (full browser viewport — can escape parent container)
HTML — content-based width utilities
<!-- 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 vs w-fit 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).
Advertisement
Google Ad Placeholder · 728×90 Leaderboard

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

Featured Course
Master Tailwind CSS — Sizing, Layout & Production UI
From sizing utilities to full dashboard layouts. Covers all Tailwind v4 utilities, responsive grid systems, dark mode, and 5 real-world projects with live mentoring.
45 Chapters 10+ Hours 5 Real Projects Certificate Live Mentoring
↹
⭐ 4.9 · 1,200+ students
Tailwind CSS Width Cheat Sheet
Free one-page cheat sheet covering all w-* utilities — fixed scale, fractional, content-based, max-w-* containers, and responsive width patterns with real examples.
w-full w-screen w-1/2 w-1/3 w-fit w-min w-max max-w-5xl Responsive 4px Scale
Download Free Cheat Sheet
Chapter Summary
  • 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/4 are percentages of the parent container. Combine with responsive prefixes: w-full md:w-1/2 lg:w-1/3 for 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 units h-svh and h-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.

Question 1 of 4
Which class makes an element exactly 50% of its parent's width?
Question 2 of 4
What is the key difference between w-full and w-screen?
Question 3 of 4
Which class constrains a container to a maximum width of ~64rem (1024px)?
Question 4 of 4
Which class makes a block element shrink to wrap around its content — like a badge or pill?

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