tw-type-css

A collection of Tailwind CSS v4.0 utilities with typographic primitives. Unlike the all-or-nothing reliance on prose in @tailwind/typography, this plugin offers both primitives and a tw-prose utility.

This package is a replacement for @tailwindcss/typography. It embraces the new CSS-first architecture, providing a pure CSS solution without relying on the legacy JavaScript plugin system. Furthermore, unlike the original plugin, this plugin does not make color choices for you.

Usage

  1. Install the package with npm

    npm install -D tw-type-css
  2. Add the following line to your root CSS file

    @import "tw-animate-css";
  3. Start using the utilities

    <article class="tw-prose">
      <h1>Garlic bread with cheese: What the science tells us</h1>
      <p>
        For years parents have espoused the health benefits of eating garlic bread with cheese to their
        children, with the food earning such an iconic status in our culture that kids will often dress
        up as warm, cheesy loaf for Halloween.
      </p>
      <p>
        But a recent study shows that the celebrated appetizer may be linked to a series of rabies cases
        springing up around the country.
      </p>
      <!-- ... -->
    </article>

    Utility classes text-{100,200,300,400,500,600,700,800,900,1000}, leading-{160,150,130,120,110,100}, tracking-{003,002,001,000}, font-weight-{300,400,500,700}. heading-{1-5} and body-{base,lg,sm} allow using the same type scale without having to use tw-prose. Consider the following example that attempts to use semantic HTML while still conforming to the defined type scale.

    <h1 class="heading-3">Heading that shouldn't be as large as an h1 but should be an h1 for semantics</h1>

This plugin assumes fonts are defined in the tailwind default --font-mono, --font-sans variables. Furthermore, this plugin uses --spacing() utilities to align with the base project.

Customization

The plugin simple rules to generate font sizes for the type scale, and percentage based measures for line height. The font sizes are controlled by the --tw-type-scale-ratio (defaults to 1.25 Major Scale) and --tw-type-scale-base (defaults to 1 rem). It is possible to change the overall size and variance in size by controlling these two.

Scale Controls

What to expect from here on out

What follows from here is just a bunch of absolute nonsense written to dogfood the plugin itself. It includes every sensible typographic element I could think of, like bold text, unordered lists, ordered lists, code blocks, block quotes, and even italics.

It’s important to cover all of these use cases for a few reasons:

  1. We want everything to look good out of the box.
  2. Really just the first reason, that’s the whole point of the plugin.
  3. Here’s a third pretend reason though a list with three items looks more realistic than a list with two items.

Now we’re going to try out another header style.

Typography should be easy

So that’s a header for you — with any luck if we’ve done our job correctly that will look pretty reasonable.

Something a wise person once told me about typography is:

Typography is pretty important if you don’t want your stuff to look like trash. Make it good then it won’t be bad.

It’s probably important that images look okay here by default as well:

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.

Now I’m going to show you an example of an unordered list to make sure that looks good, too:

And that’s the end of this section.

What if we stack headings?

We should make sure that looks good

Sometimes you have headings directly underneath each other. In those cases you often have to undo the top margin on the second heading because it usually looks better for the headings to be closer together than a paragraph followed by a heading should be.

When a heading comes after a paragraph…

When a heading comes after a paragraph, we need a bit more space, like I already mentioned above. Now let’s see what a more complex list would look like.

After this sort of list I usually have a closing statement or paragraph, because it kinda looks weird jumping right to a heading.

What about nested lists?

Nested lists basically always look bad which is why editors like Medium don’t even let you do it, but I guess since some of you goofballs are going to do it we have to carry the burden of at least making it work.

  1. Nested lists are rarely a good idea.
    • You might feel like you are being really “organized” or something but you are just creating a gross shape on the screen that is hard to read.
    • Nested navigation in UIs is a bad idea too, keep things as flat as possible.
    • Nesting tons of folders in your source code is also not helpful.
  2. Since we need to have more items, here’s another one.
    • I’m not sure if we’ll bother styling more than two levels deep.
    • Two is already too much, three is guaranteed to be a bad idea.
    • If you nest four levels deep you belong in prison.
  3. Two items isn’t really a list, three is good though.
    • Again please don’t nest lists if you want people to actually read your content.
    • Nobody wants to look at this.
    • I’m upset that we even have to bother styling this.

The most annoying thing about lists in Markdown is that <li> elements aren’t given a child <p> tag unless there are multiple paragraphs in the list item. That means I have to worry about styling that annoying situation too.

And finally a sentence to close off this section.

We didn’t forget about description lists

Well, that’s not exactly true, we first released this plugin back in 2020 and it took three years before we added description lists. But they’re here now, so let’s just be happy about that…okay? They can be great for things like FAQs.

Why do you never see elephants hiding in trees?
Because they're so good at it. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas cupiditate laboriosam fugiat.
What do you call someone with no body and no nose?
Nobody knows. Lorem ipsum dolor sit amet consectetur adipisicing elit. Culpa, voluptas ipsa quia excepturi, quibusdam natus exercitationem sapiente tempore labore voluptatem.
Why can't you hear a pterodactyl go to the bathroom?
Because the pee is silent. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam, quas voluptatibus ex culpa ipsum, aspernatur blanditiis fugiat ullam magnam suscipit deserunt illum natus facilis atque vero consequatur! Quisquam, debitis error.

There are other elements we need to style

I almost forgot to mention links, like this link to the Tailwind CSS website.

We even included table styles, check it out:

WrestlerOriginFinisher
Bret “The Hitman” HartCalgary, ABSharpshooter
Stone Cold Steve AustinAustin, TXStone Cold Stunner
Randy SavageSarasota, FLElbow Drop
VaderBoulder, COVader Bomb
Razor RamonChuluota, FLRazor’s Edge

We also need to make sure inline code looks good, like if I wanted to talk about <span> elements.

Sometimes I even use code in headings

Another thing I’ve done in the past is put a code tag inside of a link, like if I wanted to tell you about the tailwindcss/docs repository. .

We haven’t used an h4 yet

But now we have. Please don’t use h6 in your content.

We don’t style them at all out of the box because h5 elements are already so small that they are the same size as the body copy.

We still need to think about stacked headings though.

Let’s make sure we don’t screw that up with h4 elements, either.

Phew, with any luck we have styled the headings above this text and they look pretty good.

Let’s add a closing paragraph here so things end with a decently sized block of text. I can’t explain why I want things to end that way but I have to assume it’s because I think things will look weird or unbalanced if there is a heading too close to the end of the document.

What I’ve written here is probably long enough, but adding this final sentence can’t hurt.