Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

tailwindcss-capsize

stormwarning57.5kISC4.0.0TypeScript support: included

TailwindCSS leading-trim utility classes.

tailwindcss, tailwindcss-plugin, capsize, leading-trim, text-box, text-box-trim, typography, whitespace

readme

tailwindcss-capsize

npm version

A TailwindCSS plugin that generates text-box-trim utility classes using Capsize.

npm install --save-dev tailwindcss-capsize

text-box-trim?

Formerly known as leading-trim, a proposed CSS property to remove the extra space from text bounding boxes, which affects optical alignment. This article from Microsoft Design outlines the problem and how the proposed solution works.

Configuration

A JavaScript config file is required due to the values needed by the plugin.

@config "../tailwind.config.js";

Other Tailwind settings can be configured through CSS directives or included in the config file.

This plugin requires a fontMetrics key added to your Tailwind theme. It should be an object with keys matching those in your fontFamily definitions, and each key should have an object of the following shape:

{
    ascent: number
    descent: number
    lineGap: number
    unitsPerEm: number
    capHeight: number
}

These values can be determined by using the Capsize website, fontkit, FontDrop!, or some other means.

A full example

// tailwind.config.js
import pluginCapsize from 'tailwindcss-capsize'
export default {
    theme: {
        fontFamily: {
            sans: ['Inter', 'sans-serif'],
        },
        fontMetrics: {
            // Keys here must match those in fontFamily.
            sans: {
                capHeight: 2048,
                ascent: 2728,
                descent: -680,
                lineGap: 0,
                unitsPerEm: 2816,
            },
        },
        // ...
    },
    plugins: [pluginCapsize],
}

Usage

The new .capsize utility class should be applied to the direct parent element surrounding a text node. This class requires font-family, font-size, and line-height utilities to be applied at some point above it in the cascade in order for the required custom properties to be available.

<!-- Example using default TailwindCSS config -->

<p class="font-sans text-base leading-none capsize">Lorem ipsum dolor</p>

Options

rootSize

type: number (optional, default: 16)

The plugin assumes a root font-size of 16px when converting from rem values. To use a different value, pass it in (without units) to the plugin options.

pluginCapsize({ rootSize: 12 })

className

type: string (optional, default: 'capsize')

The default .capsize utility class can be replaced with a custom class name if preferred.

pluginCapsize({ className: 'leading-trim' })

Tips and tricks

Text truncation and line clamping

Sometimes an interface calls for truncating text to a single line or clamping text to a specified number of lines. Applying these methods to the same element that the default .capsize class is applied to will cause issues since the class assigns pseudo ::before and ::after elements to that element.

<!-- ❌ Does NOT work -->

<p class="font-sans text-base leading-none capsize truncate">
    Text to be truncated to a single line
</p>

To solve this, a child element to the element with the .capsize class should wrap the text. This element should receive the styling to truncate or line clamp.

<!-- ✅ Does work! -->

<p class="font-sans text-base leading-none capsize">
    <span class="truncate">Text to be truncated to a single line</span>
</p>

🔡 tailwindcss-opentype — Utility classes for advanced typographic features.

changelog

Change Log

4.0.0

Major Changes

  • Update plugin for Tailwind v4 support (#247)

    v4 was a major change to how projects are configured as well as what plugins are allowed to modify. Previously, this plugin disabled corePlugins like fontSize in order to include custom CSS properties within the same utilities. This is no longer possible in v4, so while the usage hasn't changed, the CSS being output now includes duplicate declarations — one from the plugin and one from Tailwind itself.

    v4 also allows configuration within CSS itself. However, this plugin relies on object values which aren't supported in CSS, so a JavaScript config file is still required. You can either use the JS file for all your settings, or just the settings for this plugin and configure the rest of your project in CSS.

    The mode option has also been removed. This also removes the dependency on @capsizecss/core.

3.0.5

Patch Changes

  • Reworked internals (#214) No notable change in CSS output. Should support Tailwind TypeScript configs better now.

3.0.4

Patch Changes

  • Fix fontSize utility not including default fontWeight settings #209

3.0.3 — 2022-12-21

Patch Changes

3.0.2 — 2022-05-06

🐛 Fixed

  • Use correct custom property with default lineHeight values #153

    Fixes issue with a leading-* class being required even with fontSize values including a default lineHeight.

3.0.1 — 2022-01-17

🐛 Fixed

  • Remove unit from --font-size-px custom property #128

    Thanks @essejmclean!
    Fixes issue with calc() functions not working correctly.

3.0.0 — 2022-01-11

💣 Breaking Changes

  • Add modern output mode #123

    In this mode the fontFamily, fontSize, and lineHeight core plugins are replaced, adding custom properties to the output of each which are used in the calc() expressions in the utility class.

    modern mode is enabled by default. The previous functionality can be maintained if needed by switching to classic mode.

2.1.0 — 2021-09-21

🎁 Added

  • Allow custom activation class via new className option #103

    require("tailwindcss-capsize")({ className: "leading-trim" });

2.0.0 — 2021-09-21

💣 Breaking Changes

  • Use new @capsize/core library #94 Thanks @DylanVann!
    This will change the final output CSS, as the technique to perform the leading trim has been simplified. See the capsize release notes for more details.

🐛 Fixed

  • Fix usage when requireing plugin #95 Thanks @DylanVann!

    - require('tailwindcss-capsize').default
    + require('tailwindcss-capsize')

1.2.2 — 2021-03-03

🐛 Fixed

  • Fix issue requiring an empty options object to be passed in #68

    - require('tailwindcss-capsize')({})
    + require('tailwindcss-capsize')

1.2.1 — 2021-02-08

🐛 Fixed

  • Avoid error when normalizeValue gets an array e18c905
    Still needs to account fully for Tailwind v2 configs, but this helps.

1.2.0 — 2020-10-11

🎁 Added

  • Allow unitless or percentage-based leading values #34
    Uses the inherited font-size to determine pixel line-height value.

🐛 Fixed

  • Use correct path for types import #30
    This should clear up any TypeScript warnings during local builds.

1.1.0 — 2020-09-04

💣 Breaking Changes

  • Rename plugin using common prefix convention eac9127 \ tailwind-capsizetailwindcss-capsize

1.0.3 — 2020-09-03

♻️ Changed

  • Cleaned out unused code and fix up docs examples bc1372f

1.0.2 — 2020-09-01

🐛 Fixed

  • Fixed error when trying to set a custom root font-size value #16

1.0.1 — 2020-08-13

🐛 Fixed

  • Added actual README and package description & keywords 327886e

1.0.0 — 2020-08-11

🎉 Initial release