Back to Tailwindcss

Width

src/docs/width.mdx

latest9.7 KB
Original Source

import { ApiTable } from "@/components/api-table.tsx"; import { CustomizingYourSpacingScale, ResponsiveDesign, UsingACustomValue } from "@/components/content.tsx"; import { Example } from "@/components/example.tsx"; import { Figure } from "@/components/figure.tsx";

export const title = "width"; export const description = "Utilities for setting the width of an element.";

<ApiTable rows={[ ["w-<number>", "width: calc(var(--spacing) * <number>);"], ["w-<fraction>", "width: calc(<fraction> * 100%);"], ["w-3xs", "width: var(--container-3xs); /* 16rem (256px) /"], ["w-2xs", "width: var(--container-2xs); / 18rem (288px) /"], ["w-xs", "width: var(--container-xs); / 20rem (320px) /"], ["w-sm", "width: var(--container-sm); / 24rem (384px) /"], ["w-md", "width: var(--container-md); / 28rem (448px) /"], ["w-lg", "width: var(--container-lg); / 32rem (512px) /"], ["w-xl", "width: var(--container-xl); / 36rem (576px) /"], ["w-2xl", "width: var(--container-2xl); / 42rem (672px) /"], ["w-3xl", "width: var(--container-3xl); / 48rem (768px) /"], ["w-4xl", "width: var(--container-4xl); / 56rem (896px) /"], ["w-5xl", "width: var(--container-5xl); / 64rem (1024px) /"], ["w-6xl", "width: var(--container-6xl); / 72rem (1152px) /"], ["w-7xl", "width: var(--container-7xl); / 80rem (1280px) */"], ["w-auto", "width: auto;"], ["w-px", "width: 1px;"], ["w-full", "width: 100%;"], ["w-screen", "width: 100vw;"], ["w-dvw", "width: 100dvw;"], ["w-dvh", "width: 100dvh;"], ["w-lvw", "width: 100lvw;"], ["w-lvh", "width: 100lvh;"], ["w-svw", "width: 100svw;"], ["w-svh", "width: 100svh;"], ["w-min", "width: min-content;"], ["w-max", "width: max-content;"], ["w-fit", "width: fit-content;"], ["w-(<custom-property>)", "width: var(<custom-property>);"], ["w-[<value>]", "width: <value>;"], ["size-<number>", "width: calc(var(--spacing) * <number>);\nheight: calc(var(--spacing) * <number>);"], ["size-<fraction>", "width: calc(<fraction> * 100%);\nheight: calc(<fraction> * 100%);"], ["size-auto", "width: auto;\nheight: auto;"], ["size-px", "width: 1px;\nheight: 1px;"], ["size-full", "width: 100%;\nheight: 100%;"], ["size-dvw", "width: 100dvw;\nheight: 100dvw;"], ["size-dvh", "width: 100dvh;\nheight: 100dvh;"], ["size-lvw", "width: 100lvw;\nheight: 100lvw;"], ["size-lvh", "width: 100lvh;\nheight: 100lvh;"], ["size-svw", "width: 100svw;\nheight: 100svw;"], ["size-svh", "width: 100svh;\nheight: 100svh;"], ["size-min", "width: min-content;\nheight: min-content;"], ["size-max", "width: max-content;\nheight: max-content;"], ["size-fit", "width: fit-content;\nheight: fit-content;"], ["size-(<custom-property>)", "width: var(<custom-property>);\nheight: var(<custom-property>);"], ["size-[<value>]", "width: <value>;\nheight: <value>;"], ]} />

Examples

Basic example

Use w-<number> utilities like w-24 and w-64 to set an element to a fixed width based on the spacing scale:

<Figure> <Example> { <div className="flex justify-center"> <div className="space-y-4 text-center font-mono text-xs font-bold text-white"> <div className="hidden w-96 rounded-lg bg-blue-500 px-4 py-2 sm:block">w-96</div> <div className="hidden w-80 rounded-lg bg-blue-500 px-4 py-2 sm:block">w-80</div> <div className="hidden w-64 rounded-lg bg-blue-500 px-4 py-2 sm:block">w-64</div> <div className="w-48 rounded-lg bg-blue-500 px-4 py-2">w-48</div> <div className="w-40 rounded-lg bg-blue-500 px-4 py-2">w-40</div> <div className="w-32 rounded-lg bg-blue-500 px-4 py-2">w-32</div> <div className="w-24 rounded-lg bg-blue-500 px-4 py-2">w-24</div> </div> </div> } </Example>
html
<!-- [!code classes:w-96,w-80,w-64,w-48,w-40,w-32,w-24] -->
<div class="w-96 ...">w-96</div>
<div class="w-80 ...">w-80</div>
<div class="w-64 ...">w-64</div>
<div class="w-48 ...">w-48</div>
<div class="w-40 ...">w-40</div>
<div class="w-32 ...">w-32</div>
<div class="w-24 ...">w-24</div>
</Figure>

Using a percentage

Use w-full or w-<fraction> utilities like w-1/2 and w-2/5 to give an element a percentage-based width:

<Figure> <Example> { <div className="space-y-4 font-mono text-xs font-bold text-white"> <div className="flex gap-x-4"> <div className="w-1/2 rounded-lg bg-violet-500 px-4 py-2 text-center">w-1/2</div> <div className="w-1/2 rounded-lg bg-violet-500 px-4 py-2 text-center">w-1/2</div> </div> <div className="flex gap-x-4"> <div className="w-2/5 rounded-lg bg-violet-500 px-4 py-2 text-center">w-2/5</div> <div className="w-3/5 rounded-lg bg-violet-500 px-4 py-2 text-center">w-3/5</div> </div> <div className="flex gap-x-4"> <div className="w-1/3 rounded-lg bg-violet-500 px-4 py-2 text-center">w-1/3</div> <div className="w-2/3 rounded-lg bg-violet-500 px-4 py-2 text-center">w-2/3</div> </div> <div className="hidden gap-x-4 sm:flex"> <div className="w-1/4 rounded-lg bg-violet-500 px-4 py-2 text-center">w-1/4</div> <div className="w-3/4 rounded-lg bg-violet-500 px-4 py-2 text-center">w-3/4</div> </div> <div className="hidden gap-x-4 sm:flex"> <div className="w-1/5 rounded-lg bg-violet-500 px-4 py-2 text-center">w-1/5</div> <div className="w-4/5 rounded-lg bg-violet-500 px-4 py-2 text-center">w-4/5</div> </div> <div className="hidden gap-x-4 sm:flex"> <div className="w-1/6 rounded-lg bg-violet-500 px-4 py-2 text-center">w-1/6</div> <div className="w-5/6 rounded-lg bg-violet-500 px-4 py-2 text-center">w-5/6</div> </div> <div className="w-full rounded-lg bg-violet-500 px-4 py-2 text-center font-mono text-white">w-full</div> </div> } </Example>
html
<!-- [!code classes:w-1/2,w-2/5,w-3/5,w-1/3,w-2/3,w-1/4,w-3/4,w-1/5,w-4/5,w-1/6,w-5/6,w-full] -->
<div class="flex ...">
  <div class="w-1/2 ...">w-1/2</div>
  <div class="w-1/2 ...">w-1/2</div>
</div>
<div class="flex ...">
  <div class="w-2/5 ...">w-2/5</div>
  <div class="w-3/5 ...">w-3/5</div>
</div>
<div class="flex ...">
  <div class="w-1/3 ...">w-1/3</div>
  <div class="w-2/3 ...">w-2/3</div>
</div>
<div class="flex ...">
  <div class="w-1/4 ...">w-1/4</div>
  <div class="w-3/4 ...">w-3/4</div>
</div>
<div class="flex ...">
  <div class="w-1/5 ...">w-1/5</div>
  <div class="w-4/5 ...">w-4/5</div>
</div>
<div class="flex ...">
  <div class="w-1/6 ...">w-1/6</div>
  <div class="w-5/6 ...">w-5/6</div>
</div>
<div class="w-full ...">w-full</div>
</Figure>

Using the container scale

Use utilities like w-sm and w-xl to set an element to a fixed width based on the container scale:

<Figure> <Example> { <div className="flex justify-center"> <div className="space-y-4 text-center font-mono text-xs font-bold text-white"> <div className="hidden w-xl rounded-lg bg-sky-500 px-4 py-2 sm:block">w-xl</div> <div className="hidden w-lg rounded-lg bg-sky-500 px-4 py-2 sm:block">w-lg</div> <div className="hidden w-md rounded-lg bg-sky-500 px-4 py-2 sm:block">w-md</div> <div className="w-sm rounded-lg bg-sky-500 px-4 py-2">w-sm</div> <div className="w-xs rounded-lg bg-sky-500 px-4 py-2">w-xs</div> <div className="w-2xs rounded-lg bg-sky-500 px-4 py-2">w-2xs</div> <div className="w-3xs rounded-lg bg-sky-500 px-4 py-2">w-3xs</div> </div> </div> } </Example>
html
<!-- [!code classes:w-xl,w-lg,w-md,w-sm,w-xs,w-2xs,w-3xs] -->
<div class="w-xl ...">w-xl</div>
<div class="w-lg ...">w-lg</div>
<div class="w-md ...">w-md</div>
<div class="w-sm ...">w-sm</div>
<div class="w-xs ...">w-xs</div>
<div class="w-2xs ...">w-2xs</div>
<div class="w-3xs ...">w-3xs</div>
</Figure>

Matching the viewport

Use the w-screen utility to make an element span the entire width of the viewport:

html
<!-- [!code classes:w-screen] -->
<div class="w-screen">
  <!-- ... -->
</div>

Alternatively, you can match the width of the large, small or dynamic viewports using the w-lvw, w-svw, and w-dvw utilities.

Resetting the width

Use the w-auto utility to remove an element's assigned width under a specific condition, like at a particular breakpoint:

html
<!-- [!code classes:w-full,w-auto] -->
<div class="w-full md:w-auto">
  <!-- ... -->
</div>

Setting both width and height

Use utilities like size-px, size-4, and size-full to set both the width and height of an element at the same time:

<Figure> <Example> { <div className="grid grid-flow-col justify-center gap-4 text-center font-mono text-xs font-bold text-white"> <div className="grid size-16 items-center justify-center rounded-lg bg-indigo-500">size-16</div> <div className="grid size-20 items-center justify-center rounded-lg bg-indigo-500">size-20</div> <div className="grid size-24 items-center justify-center rounded-lg bg-indigo-500">size-24</div> <div className="hidden size-32 items-center justify-center rounded-lg bg-indigo-500 sm:grid">size-32</div> <div className="hidden size-40 items-center justify-center rounded-lg bg-indigo-500 md:grid">size-40</div> </div> } </Example>
html
<!-- [!code classes:size-16,size-20,size-24,size-32,size-40] -->
<div class="size-16 ...">size-16</div>
<div class="size-20 ...">size-20</div>
<div class="size-24 ...">size-24</div>
<div class="size-32 ...">size-32</div>
<div class="size-40 ...">size-40</div>
</Figure>

Using a custom value

<UsingACustomValue utility="w" value="5px" name="width" variable="width" />

Responsive design

<ResponsiveDesign property="width" defaultClass="w-1/2" featuredClass="w-full" />

Customizing your theme

<CustomizingYourSpacingScale utilities={["w", "size"]} />