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.
npm install tailwindcss-capsizeVerified import paths — ran on the pinned version, not inferred.
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.
Review generated CSS for redundant `fontSize` and `lineHeight` declarations. While usage remains similar, be aware of increased CSS payload. Consider adjusting your Tailwind configuration or CSS post-processing if this is an issue.
If previous functionality (prior to v3) is required, explicitly configure the plugin to use `classic` mode: `pluginCapsize({ mode: 'classic' })`. Otherwise, ensure your styles are tested with the new modern output.Thoroughly review all text-heavy components after upgrading to `v2.0.0` to ensure the optical alignment still meets design specifications. The underlying CSS properties and values generated by Capsize will be different.
Wrap the text content within a child element. Apply the `.capsize` class to the parent, and the truncation/line-clamping classes to the child element. Example: `<p class='capsize'><span class='truncate'>...</span></p>`.
Ensure `fontMetrics` are correctly defined in `tailwind.config.js` for all font families you intend to use with `capsize`. Use tools like the Capsize website or FontDrop! to obtain these precise metrics for your fonts.
Verify the `.capsize` class is on the immediate parent of the text, and that necessary font styles (like `font-sans`, `text-base`, `leading-none`) are defined higher up or on the same element. Example: `<p class='font-sans text-base leading-none capsize'>Lorem ipsum dolor</p>`.
Add the `fontMetrics` object to your Tailwind theme, ensuring it contains the correct `ascent`, `descent`, `lineGap`, `unitsPerEm`, and `capHeight` values for each font family used. The keys in `fontMetrics` must match those in `fontFamily`.
Ensure `font-family`, `font-size`, and `line-height` Tailwind utilities are applied to the element containing the `.capsize` class, or its ancestors, to correctly define the CSS custom properties (`--font-size-px`, `--line-height-unitless`, etc.) required by the plugin.
Update to `tailwindcss-capsize@4.0.1` or newer. This issue was addressed in patch release `v4.0.1` by improving internal handling of theme value types.
Wrap the text content in a child `<span>` or similar element. Apply the `.capsize` class to the parent element, and apply the truncation/line-clamping utilities to the child element. Example: `<p class='capsize'><span class='truncate'>...</span></p>`.