Sub topic 5
Width and height: scale values, fractions, full, screen, and size
This chapter assumes you know the CSS box model, units such as px, rem, and percentages, and that you have a project with Tailwind CSS installed. You should also know how the spacing scale works from the earlier chapters.
Sizing reuses the spacing scale
The w-* and h-* utilities accept the same numbers as padding and margin. On the default scale, one step is 0.25rem, which is 4px at a 16px root font size.
| Class | CSS | Result at 16px root |
|---|---|---|
| `w-4` | `width: 1rem` | 16px |
| `w-16` | `width: 4rem` | 64px |
| `h-10` | `height: 2.5rem` | 40px |
| `w-px` | `width: 1px` | 1px |
Because the values are in rem, sizes grow if the user raises their browser font size. This is usually what you want for text-related boxes.
Fractions, full, and screen
Sizing utilities also describe a size relative to something else.
w-1/2,w-1/3,w-2/3,w-3/4and similar fractions set a percentage width of the parent element.w-fullsetswidth: 100%andh-fullsetsheight: 100%.w-screensetswidth: 100vwandh-screensetsheight: 100vh, relative to the viewport.w-autoandh-autorestore the browser default.
<div class="w-1/2 max-w-md">Half of the parent, but never wider than md</div>
<div class="size-10">Square</div>Percentage heights only work when the parent has a definite height. If h-full seems to do nothing, check the parent first.
Be careful with w-screen. 100vw includes the width of a vertical scrollbar, so an element with w-screen can cause a small horizontal scroll on systems that show classic scrollbars. Prefer w-full unless you need the element to ignore its parent's width.
The size utility
size-* sets width and height together. size-10 produces both width: 2.5rem and height: 2.5rem. It replaces the pair w-10 h-10 and is useful for avatars, icons, and square buttons. It requires Tailwind v3.4 or later. On older versions, write both classes.
<img class="size-12 rounded-full" src="avatar.jpg" alt="User avatar">Constraining with min and max
A fixed width breaks on small screens, and a percentage width stretches on large ones. Combining a flexible width with a cap solves both. The max-w-* classes (max-w-md, max-w-prose, max-w-screen-lg) set an upper limit, and min-w-* and min-h-* set a lower one. min-h-screen is a common way to make a page fill at least the viewport height.
<main class="min-h-screen">
<div class="mx-auto w-full max-w-md">A flexible column capped at md</div>
</main>Here w-full lets the box shrink with its parent, max-w-md stops it from growing past 28rem, and mx-auto centers it.
How sizing is resolved
Common errors
- Using
h-fullinside a parent with no set height. The percentage has nothing to resolve against. - Using
w-screenfor a full-width section and getting a horizontal scrollbar. - Using
size-*on a project pinned to Tailwind older than v3.4. The class does nothing. - Writing
w-1/2on a flex child and expecting it to hold. Flex items can shrink, so addshrink-0if the width must stay fixed.
Exercise
You want a square avatar that is 48px wide and tall at a 16px root font size. Which single class does it, and what two CSS declarations does it generate?
Show answer
size-12. It generates width: 3rem and height: 3rem, which are 48px each. (On Tailwind older than v3.4, use w-12 h-12.)
Exercise
A card should fill its parent on phones but never exceed 28rem on wide screens, and it should be centered. Write the classes.
Show answer
w-full max-w-md mx-auto. w-full fills the parent, max-w-md caps the width at 28rem, and mx-auto centers the block.