Set the width and height of an element relative to its parent or the viewport, with responsive fraction widths.
Relative to the parent
Width utilities (.w-25, .w-50, .w-75, .w-100, .w-auto) set the width of an element as a percentage of its parent. Height utilities (.h-25, .h-50, .h-75, .h-100, .h-auto) do the same for the height. The keys of the $sizes Sass map add a class per named size, such as .w-md.
<div class="w-25 p-md">Width 25%</div>
<div class="w-50 p-md">Width 50%</div>
<div class="w-75 p-md">Width 75%</div>
<div class="w-100 p-md">Width 100%</div>
<div class="w-auto p-md">Width auto</div><div style="height: 100px;">
<div class="h-25 d-inline-block" style="width: 19%;">Height 25%</div>
<div class="h-50 d-inline-block" style="width: 19%;">Height 50%</div>
<div class="h-75 d-inline-block" style="width: 19%;">Height 75%</div>
<div class="h-100 d-inline-block" style="width: 19%;">Height 100%</div>
<div class="h-auto d-inline-block" style="width: 19%;">Height auto</div>
</div>.max-w-100 and .max-h-100 cap the width or height of an element at 100% of its parent.
<div style="width: 50%; height: 100px;">
<div class="mw-100" style="width: 200%;">Max-width 100%</div>
</div><div style="height: 100px;">
<div class="mh-100" style="width: 100px; height: 200px;">Max-height 100%</div>
</div>Fractions
.w-{n}/12 sets the width to n twelfths of the parent, from .w-1/12 to .w-11/12. Unlike .w-25, .w-50 and .w-75, the fractions have a variant per breakpoint (.md:w-6/12), which sizes an element responsively outside a grid: an image beside floated text, a skeleton line, the image of a horizontal card.
<div class="w-4/12 p-md mb-xs">.w-4/12</div>
<div class="w-8/12 p-md mb-xs">.w-8/12</div>
<div class="w-100 md:w-6/12 p-md">.w-100 .md:w-6/12</div>.w-100 and .w-auto have a variant per breakpoint too, which ends a fraction at a breakpoint: .w-6/12 .lg:w-auto is half of the parent below lg and back to its default width from lg up, and .md:w-100 returns an element to the full width.
<div class="w-6/12 lg:w-auto p-md mb-xs">.w-6/12 .lg:w-auto</div>
<div class="w-8/12 md:w-100 p-md">.w-8/12 .md:w-100</div>The fractions, .w-100, and .w-auto also have a variant per container width, under the @ prefix: .@md:w-6/12 applies when the nearest query container is at least as wide as the md breakpoint.
The names and values are those of the Tailwind fraction widths: .w-6/12 declares width: calc(6 / 12 * 100%). In a stylesheet the slash is escaped (.w-6\/12); in HTML the class is written as is.
A flex item with a fraction width still shrinks. Add .flex-shrink-0 to keep the width when siblings need the room.
Relative to the viewport
Viewport-relative utilities set the width and height in viewport units instead of percentages of the parent.
<div class="min-vw-100">Min-width 100vw</div>
<div class="min-vh-100">Min-height 100vh</div>
<div class="vw-100">Width 100vw</div>
<div class="vh-100">Height 100vh</div>CSS
Sizing utilities are generated via the utilities API.
Reference
The table lists all sizing utilities and their CSS properties.
| Class | Properties |
|---|---|
.w-2xs | width: 1rem; |
.w-xs | width: 1.5rem; |
.w-sm | width: 2rem; |
.w-md | width: 2.5rem; |
.w-lg | width: 3rem; |
.w-xl | width: 3.5rem; |
.w-2xl | width: 4rem; |
.w-25 | width: 25%; |
.w-50 | width: 50%; |
.w-75 | width: 75%; |
.w-1/12 | width: calc(1 / 12 * 100%); |
.w-2/12 | width: calc(2 / 12 * 100%); |
.w-3/12 | width: calc(3 / 12 * 100%); |
.w-4/12 | width: calc(4 / 12 * 100%); |
.w-5/12 | width: calc(5 / 12 * 100%); |
.w-6/12 | width: calc(6 / 12 * 100%); |
.w-7/12 | width: calc(7 / 12 * 100%); |
.w-8/12 | width: calc(8 / 12 * 100%); |
.w-9/12 | width: calc(9 / 12 * 100%); |
.w-10/12 | width: calc(10 / 12 * 100%); |
.w-11/12 | width: calc(11 / 12 * 100%); |
.w-100 | width: 100%; |
.w-auto | width: auto; |
.h-2xs | height: 1rem; |
.h-xs | height: 1.5rem; |
.h-sm | height: 2rem; |
.h-md | height: 2.5rem; |
.h-lg | height: 3rem; |
.h-xl | height: 3.5rem; |
.h-2xl | height: 4rem; |
.h-25 | height: 25%; |
.h-50 | height: 50%; |
.h-75 | height: 75%; |
.h-100 | height: 100%; |
.h-auto | height: auto; |
.min-w-2xs | min-width: 1rem; |
.min-w-xs | min-width: 1.5rem; |
.min-w-sm | min-width: 2rem; |
.min-w-md | min-width: 2.5rem; |
.min-w-lg | min-width: 3rem; |
.min-w-xl | min-width: 3.5rem; |
.min-w-2xl | min-width: 4rem; |
.min-w-auto | min-width: auto; |
.min-w-100 | min-width: 100%; |
.min-h-2xs | min-height: 1rem; |
.min-h-xs | min-height: 1.5rem; |
.min-h-sm | min-height: 2rem; |
.min-h-md | min-height: 2.5rem; |
.min-h-lg | min-height: 3rem; |
.min-h-xl | min-height: 3.5rem; |
.min-h-2xl | min-height: 4rem; |
.min-h-auto | min-height: auto; |
.min-h-100 | min-height: 100%; |
.max-w-2xs | max-width: 1rem; |
.max-w-xs | max-width: 1.5rem; |
.max-w-sm | max-width: 2rem; |
.max-w-md | max-width: 2.5rem; |
.max-w-lg | max-width: 3rem; |
.max-w-xl | max-width: 3.5rem; |
.max-w-2xl | max-width: 4rem; |
.max-w-auto | max-width: auto; |
.max-w-100 | max-width: 100%; |
.max-h-2xs | max-height: 1rem; |
.max-h-xs | max-height: 1.5rem; |
.max-h-sm | max-height: 2rem; |
.max-h-md | max-height: 2.5rem; |
.max-h-lg | max-height: 3rem; |
.max-h-xl | max-height: 3.5rem; |
.max-h-2xl | max-height: 4rem; |
.max-h-auto | max-height: auto; |
.max-h-100 | max-height: 100%; |
.vw-25 | width: 25vw; |
.vw-50 | width: 50vw; |
.vw-75 | width: 75vw; |
.vw-100 | width: 100vw; |
.vh-25 | height: 25vh; |
.vh-50 | height: 50vh; |
.vh-75 | height: 75vh; |
.vh-100 | height: 100vh; |
.min-vw-25 | min-width: 25vw; |
.min-vw-50 | min-width: 50vw; |
.min-vw-75 | min-width: 75vw; |
.min-vw-100 | min-width: 100vw; |
.min-vh-25 | min-height: 25vh; |
.min-vh-50 | min-height: 50vh; |
.min-vh-75 | min-height: 75vh; |
.min-vh-100 | min-height: 100vh; |
Sass maps
$sizes in scss/maps/_sizing.scss holds the named sizes of the width, height, and min and max utilities.
$sizes: (
2xs: $size-2xsmall,
xs: $size-xsmall,
sm: $size-small,
md: $size-medium,
lg: $size-large,
xl: $size-xlarge,
2xl: $size-2xlarge,
);Utilities API
Sizing utilities are declared in scss/utilities/_sizing.scss using the utilities API.
"width": (
property: width,
class: w,
values: map.merge(
$sizes,
(
25: 25%,
50: 50%,
75: 75%
)
)
),
// The widths with a variant per breakpoint: the fractions, and `w-100` and
// `w-auto`, which reset a fraction at a breakpoint (`w-6/12 lg:w-auto`).
"width-fraction": (
responsive: true,
container: true,
property: width,
class: w,
values: map.merge(
$-width-fractions,
(
100: 100%,
auto: auto
)
)
),
"height": (
property: height,
class: h,
values: map.merge(
$sizes,
(
25: 25%,
50: 50%,
75: 75%,
100: 100%,
auto: auto
)
)
),
"min-width": (
property: min-width,
class: min-w,
values: map.merge($sizes, (auto: auto, 100: 100%))
),
"min-height": (
property: min-height,
class: min-h,
values: map.merge($sizes, (auto: auto, 100: 100%))
),
"max-width": (
property: max-width,
class: max-w,
values: map.merge($sizes, (auto: auto, 100: 100%))
),
"max-height": (
property: max-height,
class: max-h,
values: map.merge($sizes, (auto: auto, 100: 100%))
),
"viewport-width": (
property: width,
class: vw,
values: (
25: 25vw,
50: 50vw,
75: 75vw,
100: 100vw
)
),
"viewport-height": (
property: height,
class: vh,
values: (
25: 25vh,
50: 50vh,
75: 75vh,
100: 100vh
)
),
"min-viewport-width": (
property: min-width,
class: min-vw,
values: (
25: 25vw,
50: 50vw,
75: 75vw,
100: 100vw
)
),
"min-viewport-height": (
property: min-height,
class: min-vh,
values: (
25: 25vh,
50: 50vh,
75: 75vh,
100: 100vh
)
),
"dynamic-viewport-height": (
property: height,
class: dvh,
values: (
25: 25dvh,
50: 50dvh,
75: 75dvh,
100: 100dvh
)
),