Sub topic 2
Width and Height: Scale Values, Fractions, Full, Screen, and Size
Assume you know HTML, the CSS box model, common CSS units, and have Tailwind CSS installed. Tailwind utility classes map directly to CSS declarations. For example, w-64 sets width: 16rem, because 64 is a key in Tailwind’s spacing scale.
Width and Height Utilities
Use w-* for width and h-* for height.
w-64andh-32use scale values.w-1/2sets width to half of the parent.w-fulluseswidth: 100%.w-screenuses the viewport width.h-screenuses the viewport height.size-10sets both width and height to the same value.
<div class="mx-auto w-1/2 max-w-md p-4">
<div class="size-10 rounded-full bg-blue-600"></div>
</div>A fraction depends on the parent’s available size. w-screen depends on the viewport and can cause horizontal overflow when scrollbars are included.
Constraining Size
Minimum and maximum utilities prevent content from becoming too small or too large. Examples include min-w-0, min-h-screen, max-w-md, and max-h-96.
max-w-prose is useful for paragraphs because it limits line length to a readable measure. A width utility and a maximum width can work together:
<article class="w-full max-w-prose">
Readable content stays within a comfortable line length.
</article>Font Size and Default Line Height
text-sm, text-base, text-lg, and text-3xl set font size. Tailwind’s size utilities also provide default line heights. You can override the line height with leading-*, such as text-lg leading-7.
Font Weight and Family
Use font-normal, font-medium, font-semibold, or font-bold for weight. Use font-sans, font-serif, and font-mono for font family. These utilities change inherited text styles without requiring custom selectors.
Leading and Tracking
leading-* controls line height. tracking-tight, tracking-normal, and tracking-wide control letter spacing. Tight tracking often suits headings, while normal or wider tracking improves some small labels.
Exercise
Which classes make a block half as wide as its parent, limit it to a medium maximum width, and add a tight heading style?
Show answer
Use w-1/2 max-w-md for the block, and text-3xl font-bold tracking-tight for the heading.