Tip 3/72

Size

Simplify Your Code with the `size-*` Utilities in TailwindCSS

TailwindCSS introduces the size-* utilities to simplify how you define square dimensions. Instead of specifying w-* (width) and h-* (height) individually, you can use size-* to apply both at once.

The Traditional Approach (w-* and h-*)

Typically, you’d define square elements like this:

<div class="w-12 h-12 bg-blue-500"></div>

This requires setting both w and h separately, which can become repetitive.

A Cleaner Alternative: size-*

With the size-* utilities, you can achieve the same result more efficiently:

<div class="size-12 bg-blue-500"></div>

Advantages:

  • Simplifies your code by combining width and height into a single utility.
  • Ensures consistent dimensions, reducing potential mismatches between w and h.
  • Saves time, especially when working with square elements.

Example: Buttons or Avatars

For icon buttons or avatars, the size-* utility works perfectly:

Méschac Irung
<!-- Avatar -->
<div class="size-10 border rounded-full overflow-hidden border-white/10">
    <img src="https://avatars.githubusercontent.com/u/47919550?v=4" alt="Méschac Irung" width={320} height={320}/>
</div>

<!-- Icon Button -->
<button aria-label="genertae" class="size-10 rounded-full border text-white flex bg-indigo-600 border-white/10 hover:bg-indigo-700">
    <svg class="size-5 m-auto" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
        <path fill-rule="evenodd" d="M9 4.5a.75.75 0 0 1 .721.544l.813 2.846a3.75 3.75 0 0 0 2.576 2.576l2.846.813a.75.75 0 0 1 0 1.442l-2.846.813a3.75 3.75 0 0 0-2.576 2.576l-.813 2.846a.75.75 0 0 1-1.442 0l-.813-2.846a3.75 3.75 0 0 0-2.576-2.576l-2.846-.813a.75.75 0 0 1 0-1.442l2.846-.813A3.75 3.75 0 0 0 7.466 7.89l.813-2.846A.75.75 0 0 1 9 4.5ZM18 1.5a.75.75 0 0 1 .728.568l.258 1.036c.236.94.97 1.674 1.91 1.91l1.036.258a.75.75 0 0 1 0 1.456l-1.036.258c-.94.236-1.674.97-1.91 1.91l-.258 1.036a.75.75 0 0 1-1.456 0l-.258-1.036a2.625 2.625 0 0 0-1.91-1.91l-1.036-.258a.75.75 0 0 1 0-1.456l1.036-.258a2.625 2.625 0 0 0 1.91-1.91l.258-1.036A.75.75 0 0 1 18 1.5ZM16.5 15a.75.75 0 0 1 .712.513l.394 1.183c.15.447.5.799.948.948l1.183.395a.75.75 0 0 1 0 1.422l-1.183.395c-.447.15-.799.5-.948.948l-.395 1.183a.75.75 0 0 1-1.422 0l-.395-1.183a1.5 1.5 0 0 0-.948-.948l-1.183-.395a.75.75 0 0 1 0-1.422l1.183-.395c.447-.15.799-.5.948-.948l.395-1.183A.75.75 0 0 1 16.5 15Z" clip-rule="evenodd"> </path>
    </svg>
</button>

When to Use size-*

  • Icons or buttons: Keep dimensions consistent with fewer classes.
  • Cards or containers: Use size- for square placeholders or design elements.
  • Responsive designs: Combine size- with breakpoints (sm:size-8, md:size-12) for adaptive sizing.

Final Takeaway

The size-* utilities are a simple yet powerful addition to TailwindCSS, making your code cleaner and more maintainable. Replace paired w-* and h-* classes with size-* wherever possible to improve clarity and efficiency in your layouts.