Tailwind CSS Typography Plugin

repository·main·Indexed 27 days ago

https://github.com/tailwindlabs/tailwindcss-typography

A Tailwind CSS plugin that provides a set of `prose` classes to automatically style plain HTML content, such as Markdown or CMS-generated HTML, with professional typographic defaults. It includes support for gray scale modifiers, type scales, dark mode via `prose-invert`, and element-specific modifiers. The plugin is compatible with Tailwind CSS v3 and v4, allowing for custom color themes and configurable base class names.

Tokens
3.2K
Snippets
13
Records
15
Agent score
88%

What's inside @tailwindcss/typography

  1. Change the default prose class name

    main

    If you want to use a class name other than prose for your typography styles, you can specify a className option when registering the plugin in your CSS.

    When a custom name is provided, all related utilities (like prose-xl or not-prose) will use that new name as a prefix (e.g., wysiwyg and not-wysiwyg).

    @import 'tailwindcss';
    @plugin "@tailwindcss/typography" {
      className: wysiwyg;
    }
    <article class="wysiwyg wysiwyg-slate lg:wysiwyg-xl">
      <h1>My Heading</h1>
      <div class="not-wysiwyg">
        <!-- Prose-free content -->
      </div>
    </article>
  2. Use the `prose` class to style HTML content

    main

    The @tailwindcss/typography plugin provides a prose class that automatically applies beautiful, well-formatted typography styles to a block of vanilla HTML content. This is ideal for styling content rendered from a CMS, Markdown, or a rich-text editor where you want to avoid manual CSS for every element (headings, paragraphs, lists, etc.).

    <article class="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...
      </p>
      <!-- ... -->
    </article>
  3. Override the default max-width of prose

    main

    By default, size modifiers include a max-width to maintain readability. To make the prose content fill the entire width of its parent container, add the max-w-none utility class.

    <div class="col-span-3">
      <article class="prose max-w-none">{{ markdown }}</article>
    </div>
  4. Add custom color themes

    main

    You can create custom color themes for the typography plugin using either CSS @utility directives or the Tailwind JavaScript configuration.

    Add a @utility directive to your CSS file to define a new theme (e.g., prose-pink) by mapping internal typography CSS variables to your desired colors.

    Using Tailwind JavaScript Config (Tailwind v3)

    Update the typography section in your tailwind.config.js and provide your colors under the css key. This allows you to define named themes like pink that can be used as prose-pink.

    /* CSS @utility approach */
    @utility prose-pink {
      --tw-prose-body: var(--color-pink-800);
      --tw-prose-headings: var(--color-pink-900);
      /* ... other variables */
    }
    // tailwind.config.js approach
    /** @type {import('tailwindcss').Config} */
    module.exports = {
      theme: {
        extend: {
          typography: () => ({
            pink: {
              css: {
                '--tw-prose-body': 'var(--color-pink-800)',
                '--tw-prose-headings': 'var(--color-pink-900)',
                // ...
              },
            },
          }),
        },
      },
    }
  5. Customize typography CSS via JavaScript API

    main

    To customize the raw CSS generated by the plugin (e.g., changing specific element colors or hover states), use the JavaScript-based theme API in your tailwind.config.js.

    1. Ensure your CSS file uses the @config directive to point to your configuration file.
    2. In tailwind.config.js, extend the typography object.
    3. Customizations must be applied to a specific modifier (like DEFAULT or xl) and must be placed under the css property.
    4. Use CSS-in-JS syntax for defining styles.
    /* In your CSS file */
    @import "tailwindcss";
    @plugin "@tailwindcss/typography";
    @config "./tailwind.config.js";
    // tailwind.config.js
    /** @type {import('tailwindcss').Config} */
    module.exports = {
      theme: {
        extend: {
          typography: {
            DEFAULT: {
              css: {
                color: '#333',
                a: {
                  color: '#3182ce',
                  '&:hover': {
                    color: '#2c5282',
                  },
                },
              },
            },
          },
        },
      },
    }
  6. Use the `prose` classes for basic typography

    main

    Apply the prose class to a container (like an <article> or <div>) to add beautiful typographic defaults to vanilla HTML content (e.g., Markdown or CMS output).

    You can combine prose with size and color modifiers to adjust the look and feel.

    <article class="prose lg:prose-xl">
      <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...
      </p>
    </article>
  7. Install the Tailwind CSS Typography plugin

    main

    Install the @tailwindcss/typography plugin via npm as a development dependency.

    For Tailwind CSS v4 (using CSS imports): Add the plugin to your main style.css file using the @plugin directive.

    For Tailwind CSS v3 (using tailwind.config.js): Add the plugin to the plugins array in your configuration file.

    npm install -D @tailwindcss/typography
    /* Tailwind CSS v4 */
    @import "tailwindcss";
    @plugin "@tailwindcss/typography";
    // Tailwind CSS v3
    module.exports = {
      theme: {
        // ...
      },
      plugins: [
        require('@tailwindcss/typography'),
        // ...
      ],
    }
  8. Undo typography styles with not-prose

    main

    To prevent a block of markup from inheriting prose styles when it is nested inside a prose container, use the not-prose class. This creates a sandbox that is free from typography plugin styles.

    Note:

    • You cannot nest new prose instances within a not-prose block at this time.
    • Even if you use a prefix for your typography utilities, not-prose should not have a prefix.
    <article class="prose">
      <h1>My Heading</h1>
      <p>...</p>
    
      <div class="not-prose">
        <!-- Some example or demo that needs to be prose-free -->
      </div>
    
      <p>...</p>
    </article>
  9. Register the @tailwindcss/typography plugin

    main

    To use the typography plugin, add it to your tailwind.config.js file using the plugins array. You can pass an options object to customize the base class name and the CSS target mode.

    Options:

    • className: The base class name used to trigger typography styles (defaults to 'prose').
    • target: Determines the CSS selector strategy. Use 'modern' (default) for :where() and :is() selectors to reduce specificity, or 'legacy' for standard descendant selectors.
    // tailwind.config.js
    module.exports = {
      // ...
      plugins: [
        require('@tailwindcss/typography'),
        // Or with custom options:
        require('@tailwindcss/typography')({ className: 'my-prose', target: 'modern' }),
      ],
    }
  10. Supported typographic elements in `prose`

    main

    The prose class includes default styling for a wide range of common HTML elements, including:

    • Headings: h1 through h4 (Note: h5 and h6 are not styled by default as they are similar in size to body copy).
    • Text: Paragraphs (p), bold (strong), italics (em), and links (a).
    • Lists: Unordered lists (ul), ordered lists (ol), and description lists (dl with dt and dd).
    • Code: Inline code elements and code blocks.
    • Quotes: Blockquotes (blockquote).
    • Media: Figures (figure) and images (img) with captions (figcaption).
    • Tables: Standard HTML tables.
    • Complex Layouts: Handles nested lists and spacing for headings that follow paragraphs or other headings.