Skip to content

Tailwind CSS - Width

Tailwind’s width utilities (w-*) are fundamental for building layouts. They provide a comprehensive and responsive system for setting the width of elements using fixed values, percentages, viewport units, and content-based sizing.

Below is a curated list of common width utilities. Tailwind’s Just-in-Time (JIT) engine also allows you to use arbitrary values like w-[330px] for complete flexibility.

ClassCSS Properties
w-0width: 0px;
w-pxwidth: 1px;
w-4width: 1rem; /* 16px */
w-16width: 4rem; /* 64px */
w-64width: 16rem; /* 256px */
w-96width: 24rem; /* 384px */
w-autowidth: auto;
w-1/2width: 50%;
w-1/3width: 33.333333%;
w-fullwidth: 100%;
w-screenwidth: 100vw;
w-svwwidth: 100svw;
w-lvwwidth: 100lvw;
w-dvwwidth: 100dvw;
w-minwidth: min-content;
w-maxwidth: max-content;
w-fitwidth: fit-content;
  • Fixed Width (w-16, w-px): Sets an absolute width based on Tailwind’s spacing scale (1 unit = 0.25rem = 4px). Ideal for elements that should not change size, like icons or avatars.
  • Percentage Width (w-1/2, w-full): Sets the width relative to the parent container. Essential for fluid layouts and grid systems.
  • Viewport Width (w-screen, w-dvw): Sets the width relative to the viewport. w-screen uses the full viewport width, while w-dvw (dynamic viewport width) is often better on mobile as it accounts for dynamic browser toolbars.
  • Content-Based Width (w-min, w-max, w-fit): Sizes the element based on its content. w-max expands to fit the widest unbreakable line of content, useful for preventing text wrapping in some cases.

Core Application: Building a Responsive Grid

Section titled “Core Application: Building a Responsive Grid”

The true power of Tailwind’s width utilities shines when combined with responsive breakpoints (sm:, md:, lg:, etc.). You can define different widths for different screen sizes, creating layouts that adapt seamlessly.

This example shows a responsive grid of product cards. On small screens, each card takes the full width (w-full). On medium screens and up, they become two columns (md:w-1/2), and on large screens, four columns (lg:w-1/4). This is a standard pattern for modern web design.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind CSS Responsive Width</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-50">
<div class="container mx-auto p-4 md:p-8">
<h2 class="text-3xl font-bold text-slate-800 mb-6">Our Products</h2>
<!-- Flex container for the grid -->
<div class="flex flex-wrap -mx-4">
<!-- Card 1 -->
<div class="w-full md:w-1/2 lg:w-1/4 p-4">
<div class="bg-white rounded-lg shadow-md p-6 h-full">Product A</div>
</div>
<!-- Card 2 -->
<div class="w-full md:w-1/2 lg:w-1/4 p-4">
<div class="bg-white rounded-lg shadow-md p-6 h-full">Product B</div>
</div>
<!-- Card 3 -->
<div class="w-full md:w-1/2 lg:w-1/4 p-4">
<div class="bg-white rounded-lg shadow-md p-6 h-full">Product C</div>
</div>
<!-- Card 4 -->
<div class="w-full md:w-1/2 lg:w-1/4 p-4">
<div class="bg-white rounded-lg shadow-md p-6 h-full">Product D</div>
</div>
</div>
</div>
</body>
</html>
  • w-screen and Overflow: Be cautious with w-screen. If any parent element has padding, w-screen can cause a horizontal scrollbar. It’s often better to use w-full within a container that has no horizontal padding.
  • Parent Context Matters: Percentage widths (w-1/2, etc.) are always calculated based on the parent element’s width. If the parent has no defined width, it can lead to unexpected results.
  • Use max-w-* for Content: For text-heavy containers, it’s often better to use max-w-prose or max-w-4xl to cap the line length for readability, rather than a fixed percentage width.
  • Arbitrary Values: Don’t be afraid to use arbitrary values like w-[500px] when you have a specific design requirement that doesn’t fit the standard spacing scale. This is a key feature of the modern JIT engine.