Sub topic 5
Width and height: scale values, fractions, full, screen, and size
This chapter covers the utilities that set the width and height of an element. It assumes you know the CSS box model, units such as px, rem, and percentages, and how to use Tailwind classes in HTML. It also assumes you have met the spacing scale from the earlier chapters. Min and max constraints are covered in the next chapter, so this one only mentions them where a width or height rule needs them.
The width and height families
Tailwind sets width with w-* and height with h-*. Each class maps to one CSS declaration, so w-64 produces width: 16rem and h-64 produces height: 16rem. Both families draw their values from the same sources.
| Kind of value | Example class | CSS it produces |
|---|---|---|
| Spacing scale | `w-4`, `h-12` | `width: 1rem`, `height: 3rem` |
| Fraction | `w-1/2`, `h-2/3` | `width: 50%`, `height: 66.666667%` |
| Percentage keyword | `w-full`, `h-full` | `width: 100%`, `height: 100%` |
| Viewport | `w-screen`, `h-screen` | `width: 100vw`, `height: 100vh` |
| Content keyword | `w-auto`, `w-min`, `w-max`, `w-fit` | `auto`, `min-content`, `max-content`, `fit-content` |
| Combined | `size-10` | `width: 2.5rem; height: 2.5rem` |
Scale values
The number after w- or h- is a step on the spacing scale. One step is 0.25rem, which is 4px when the root font size is 16px. The same scale drives padding and margin, so w-4 and p-4 are both 1rem.
<div class="w-16 h-16 bg-blue-500"></div>
<div class="w-48 h-8 bg-blue-500"></div>
<div class="w-px h-px bg-blue-500"></div>The first element is 4rem square. The second is 12rem wide and 2rem tall. w-px and h-px give a single CSS pixel, which is useful for hairline dividers.
Because the values are in rem, they scale if the user changes the browser's default font size. This is usually what you want for layout. It also means that a fixed w-64 box is not exactly 256px for every user.
Fractions
A fraction class sets a percentage of the parent's size. The numerator and denominator are written in the class name, as in w-1/2, w-2/5, or w-5/12. Tailwind ships halves through twelfths for width. Percentages are calculated by the browser, so the parent must have a width to measure against.
<div class="flex">
<div class="w-1/3 bg-gray-200 p-4">Sidebar</div>
<div class="w-2/3 bg-gray-100 p-4">Main</div>
</div>The two children add up to 100% of the flex container. Here is a common gotcha. Padding and borders can push a box past its fraction if the box uses the default content-box sizing. Tailwind's preflight, its base stylesheet, sets box-sizing: border-box on every element. That keeps p-4 inside the w-1/3 box instead of adding to it. If you disable preflight, you must set border-box yourself.
Fractions also behave differently for height. A percentage height only resolves when the parent has a definite height. If the parent's height comes from its content, h-1/2 does nothing useful. This catches many people the first time they try h-full.
full, screen, and the other keywords
w-full means width: 100% of the parent. h-full means height: 100% of the parent, with the same definite-height requirement described above.
w-screen and h-screen use viewport units, so they ignore the parent and match the browser window. Two edge cases matter here.
w-screenis100vw, and100vwincludes the width of a vertical scrollbar. On systems that show classic scrollbars,w-screencan cause a horizontal scrollbar.w-fullon a top-level block usually avoids that.h-screenis100vh. On mobile browsers the address bar can make100vhtaller than the visible area. Tailwind v3.4 and later addh-dvh,h-svh, andh-lvh, which use the dynamic, small, and large viewport units. Usemin-h-dvhorh-dvhfor full-height mobile layouts when your version supports them.
The content keywords size an element from what is inside it.
w-autoreturns to the browser default, which for a block element means filling the available width.w-fitshrinks the element to its content but not beyond the available space.w-minshrinks it to the narrowest the content can be, for example the longest word.w-maxmakes it as wide as the content wants, even if that overflows.
<span class="block w-fit rounded bg-green-100 px-2 py-1">Shrink-wrapped badge</span>A block element normally stretches across its parent. w-fit makes the background hug the text instead.
The size utilities
size-* sets width and height in one class. size-10 is equivalent to w-10 h-10. It accepts the same values as w-* and h-*, including fractions, size-full, and arbitrary values.
<div class="size-10 rounded-full bg-indigo-500"></div>
<img class="size-12 rounded-full" src="avatar.jpg" alt="User avatar">The size-* utilities arrived in Tailwind v3.4. On an earlier v3 release, the class does nothing and the element collapses to its content. If a size-* class has no effect, check your installed version first.
size-* is best for square things such as avatars, icons, and buttons that are only an icon. Use separate w-* and h-* when the two dimensions are independent.
How sizing interacts with layout
A width class is a request, not a guarantee. Flexbox and grid can override it.
In a flex row, children have flex-shrink: 1 by default. A w-64 child inside a narrow container can shrink below 16rem. Add shrink-0 to the child to keep its width fixed.
<div class="flex gap-4">
<img class="size-16 shrink-0 rounded" src="thumb.jpg" alt="">
<p>Long text that wraps beside the image without squeezing it.</p>
</div>Without shrink-0, a long paragraph could compress the image. Inline elements such as span and a ignore width and height entirely, so add block or inline-block first.
The decision flow below summarizes how to choose a width.
Responsive widths
Sizing utilities take the same breakpoint prefixes as other utilities. An unprefixed class applies everywhere, and a prefixed class applies from that breakpoint upward.
<div class="w-full md:w-1/2 lg:w-1/3">
Full width on small screens, half from md, a third from lg.
</div>This pattern is the basis of many card grids. Always start from the small-screen value without a prefix.
Common errors
- Using
h-fullwith no parent height. The percentage has nothing to resolve against. Give the parent a height, or useh-screenormin-h-*as appropriate. - Putting
w-*on an inline element. Switch the element toblockorinline-block. - Expecting
w-1/2to include gaps. Twow-1/2children plus agap-4overflow the row, and flexbox then shrinks them. Useflex-1, a grid, orw-[calc(50%-0.5rem)]instead. - Using
size-*on Tailwind older than v3.4. The class is not generated. - Building a dynamic class name such as `
w-${n}`. Tailwind scans for complete class strings, so an assembled name is never generated. Write the full names and select between them.
Putting it together
This example combines a fixed avatar, a fractional column, and a responsive width.
<div class="flex flex-col gap-4 md:flex-row">
<img class="size-20 shrink-0 rounded-full" src="avatar.jpg" alt="Profile photo">
<div class="w-full md:w-2/3">
<h2 class="text-xl font-semibold">Profile</h2>
<p class="mt-1 text-gray-600">Details sit beside the avatar from md upward.</p>
</div>
</div>On small screens the items stack, and the text block fills the width. From md upward the row layout applies and the text block takes two thirds of the parent.
Exercise
A card has class="w-1/2 p-6" and sits inside a 800px-wide parent. Preflight is active. How wide is the card's border box, and why does the padding not make it wider? Also, which single class replaces w-12 h-12?
Show answer
The border box is 400px, because 1/2 of 800px is 400px. Preflight sets box-sizing: border-box, so the 1.5rem of padding on each side comes out of the 400px instead of being added. size-12 replaces w-12 h-12, on Tailwind v3.4 or later.
Exercise
Your page uses h-full on a child, but the child stays as tall as its text. The parent has no height set. Give two ways to fix it.
Show answer
Give the parent a definite height, for example h-64 or h-screen, so the percentage can resolve. Alternatively, make the parent a flex container, where children stretch to the row's height by default, and remove h-full from the child.