TailwindCSS Capsize Plugin

4.0.1 · active · verified Sun Apr 19

tailwindcss-capsize is a TailwindCSS plugin that addresses the long-standing problem of inconsistent text alignment caused by the "extra space" in font bounding boxes, often referred to as leading-trim or text-box-trim. It leverages the Capsize library to generate utility classes that optically align text by adjusting content box edges to match the capital height and baseline of the font. The current stable version is 4.0.1, with patch releases occurring regularly and major versions typically aligning with significant updates to TailwindCSS itself, such as the recent v4 release. Its key differentiator is providing a robust, configuration-driven solution within the Tailwind ecosystem for precise typographic control, relying on `fontMetrics` specific to the fonts used in a project. This plugin is crucial for designers and developers aiming for pixel-perfect typography and consistent vertical rhythm in their web applications, overcoming browser inconsistencies in text rendering. It requires a `tailwindcss` peer dependency and ships with TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure `tailwindcss-capsize` in a `tailwind.config.ts` file with required font metrics and its basic usage in HTML for optically aligned text.

// tailwind.config.ts
import type { Config } from 'tailwindcss';
import pluginCapsize from 'tailwindcss-capsize';
import type { CapsizeOptions } from 'tailwindcss-capsize';

const config: Config = {
  content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {
      fontFamily: {
        sans: ['Inter', 'sans-serif'],
      },
      fontMetrics: {
        // These values should be obtained from your font files using tools like Capsize website, fontkit, or FontDrop!
        // The keys here MUST match those defined in your `fontFamily` object.
        sans: {
          capHeight: 2048,
          ascent: 2728,
          descent: -680,
          lineGap: 0,
          unitsPerEm: 2816,
        },
        // Add metrics for other font families you use, e.g., serif: {...}
      },
    },
  },
  plugins: [
    pluginCapsize({
      // Optional: Adjust rootSize if your HTML root font-size is not 16px
      rootSize: 16,
      // Optional: Customize the utility class name from 'capsize' to something else
      // className: 'leading-trim-active'
    } as CapsizeOptions), // Casting helps ensure type safety for plugin options
  ],
};

export default config;

// index.html or a React/Vue component usage
/*
<style>
  @tailwind base;
  @tailwind components;
  @tailwind utilities;
</style>
<div class="p-8">
  <p class="font-sans text-xl leading-relaxed capsize">
    <span>This text will have precise optical alignment due to Capsize.</span>
  </p>
  <p class="font-sans text-base leading-normal capsize">
    <span>Another example of precisely aligned text.</span>
  </p>
  <p class="font-serif text-lg leading-tight capsize">
    <span>Even serif fonts can benefit from leading-trim.</span>
  </p>
</div>
*/

view raw JSON →