Get started with 33% off your first certification using code: 33OFFNEW

How dimensions work in Tailwind (widths, heights, font sizes)

2 min read
Published on 20th October 2023

Tailwind CSS, with its utility-first approach, has transformed the landscape of frontend development. One of the standout features of Tailwind is its system for dealing with dimensions. From setting container widths to defining text sizes, Tailwind provides a flexible and consistent method to manage your layout. Let's explore how.

Widths & Heights: The Basics

In Tailwind, width and height utilities are generated based on your configuration file's spacing/sizing section, which by default contains values from 0 to spacing[64], and special values like auto, px, full, and fractions for percentages.

  • Fixed Dimensions: Use the w- or h- prefix followed by the desired size.
<div class="w-32 h-32">...</div>  <!-- Sets the width and height to 8rem (128px by default) -->
  • Responsive Dimensions: Use the combination of a breakpoint prefix and size.
<div class="md:w-48 md:h-48">...</div>  <!-- Sets the width and height to 12rem (192px) on medium screens and above -->
  • Percentage Dimensions: Handy when you want an element to take up a specific fraction of its container.
<div class="w-1/2 h-1/4">...</div>  <!-- 50% width and 25% height of its parent container -->

Font Sizes

Font sizes in Tailwind are managed through the text utility. The configuration file provides a list of sizes from text-xs to text-6xl for out-of-the-box use.

<p class="text-lg">This is a large text.</p>

Additionally, you can add your custom sizes in the configuration:

// tailwind.config.js
module.exports = {
  theme: {
    fontSize: {
      '5xl': '3rem',
      '6xl': '4rem',
      '7xl': '5rem',  // custom size
    }
  }
}

Extending Dimensions

While Tailwind provides a broad range of sizes, there might be scenarios where you need a very specific size. The tailwind.config.js file is your playground.

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      width: {
        '96': '24rem',
        'screen-90': '90vw',
      },
      height: {
        'half-screen': '50vh',
      },
      fontSize: {
        'xxs': '0.65rem',
      }
    }
  }
}

Dynamic Dimensions with Directives

Tailwind offers directives like @apply, which allows the use of utilities directly in your CSS. This is powerful when you want to combine multiple utilities or add dynamic behavior.

.button-width {
  @apply w-32 md:w-64;
}

Dimensions play a crucial role in shaping the visual landscape of a website or application. Tailwind CSS simplifies this by providing utility classes that are both comprehensive and customizable. Whether you're defining the footprint of a container, setting the height of an image, or choosing the font size for your text, Tailwind has got you covered. Embrace the utility-first approach, and you'll find that managing dimensions in your project becomes a breeze!