Tailwind CSS Reference

Kip Landergren

(Updated: )

My cheat sheet for TailwindCSS.

Contents#

Layers#

Tailwind uses native CSS layers that, when you @import "tailwindcss"; are divided as:

This explanation of default theme variables has a full overview.

Directives#

Frequently used:

More info, with a complete list of functions and directives:

Theme Variables#

From Theme variables documentation:

Use @theme when you want a design token to map directly to a utility class, and use :root for defining regular CSS variables that shouldn't have corresponding utility classes.

Variants#

pseudo-classes (AKA “state variants”) e.g. :hover, :focus
pseudo-elements e.g. ::before, ::after
media and feature queries (AKA “responsive variants”) e.g. for responsive breakpoints like sm:, lg:, or preferences like prefers-reduced-motion
attribute selectors e.g. [dir="rtl"]
child selectors e.g. & > *

Note that variants:

... only apply the styles from a utility class when the condition for that variant matches.

(Meaning the generated CSS is narrower in its selection and application, which prevents callers from having to know internal state and manage whether class names conflict).

Full explanation and list of variants in Hover, Focus, and Other States documentation.

Colors#

Tips#

Avoid “Styling at a Distance”#

Write your Tailwind classes on the HTML (or component) that is being displayed. The further you are from the location of the applied style the harder it is to reason about how it is affected by intermediate logic.

Use the Prettier Plug-In#

tailwindlabs/prettier-plugin-tailwindcss will sort your class names making development and code review much easier.

Avoid Dynamic Class Name Construction#

A static class name string is unambiguous for Tailwind to parse and easier for users and tools to determine if there are conflicts.

If dynamic construction is necessary (e.g. due to combinatorial explosion: 6 boolean params results in 26 or 64 variants) consider:

Working with Tailwind CSS#

Resources#

References#

Articles#

Terminology#

atomic CSS#
1.

One utility class name maps to one CSS rule. e.g. TailwindCSS’ flex maps to display: flex;

Also: atomic styles

Related:

design token#
1.
theme#
1.

contains ... typography, colors, shadows, breakpoints, and more

Related:

theme variable#
1.

variable that maps to color palette, type scale, and shadows

Related:

utility class#
1.
variant#
1.