Tailwind CSS Utility Classes
The core of Tailwind is its vast and comprehensive library of utility classes. These classes are carefully designed with a "utility-first" philosophy, allowing you to build any design directly in your HTML. This chapter will outline the structure of utility classes and some common categories.
Structure of Utility Classes
Tailwind's utility classes typically follow a predictable format:
{property}-{value} or {prefix}:{property}-{value}
{property}: Usually an abbreviation of the CSS property, for examplebgforbackground,pforpadding,textforcolorandfont-size, etc.{value}: Represents a specific value, usually coming from the design system defined in yourtailwind.config.js. For example,red-500represents a specific red color,4represents1remof spacing.{prefix}: Optional prefix used to apply styles under specific conditions, such ashover:(hover state) ormd:(medium screen size).
Common Utility Class Categories
Here are some of the most common utility class categories you'll encounter in daily development:
Layout
- Display:
block,inline-block,inline,flex,grid,hidden, etc. - Position:
static,relative,absolute,fixed,sticky. - Top / Right / Bottom / Left:
top-0,left-4,bottom-auto, etc., supports arbitrary values liketop-[10px]. - Z-Index:
z-0,z-10,z-auto, etc.
Flexbox & Grid
- Flex Direction:
flex-row,flex-col. - Flex Wrap:
flex-wrap,flex-nowrap. - Align Items:
items-start,items-center,items-end. - Justify Content:
justify-start,justify-center,justify-between. - Grid Template Columns/Rows:
grid-cols-3,grid-rows-2. - Gap:
gap-4,gap-x-8.
Spacing
- Padding:
p-4(all directions),px-6(horizontal direction),pt-2(top). - Margin:
m-4(all directions),mx-auto(horizontal centering),mt-8(top).
Sizing
- Width:
w-16,w-1/2,w-full,w-screen,w-auto. - Height:
h-16,h-full,h-screen. - Min/Max Width/Height:
min-w-0,max-h-screen.
Typography
- Font Family:
font-sans,font-serif,font-mono. - Font Size:
text-xs,text-base,text-lg,text-4xl. - Font Weight:
font-light,font-normal,font-bold. - Text Color:
text-slate-500,text-sky-600. - Text Align:
text-left,text-center,text-right. - Text Decoration:
underline,line-through,no-underline.
Backgrounds
- Background Color:
bg-white,bg-gray-800,bg-emerald-500. - Background Image:
bg-cover,bg-center.
Borders
- Border Radius:
rounded-none,rounded,rounded-lg,rounded-full. - Border Width:
border,border-2,border-t-4(top border only). - Border Color:
border-gray-200,border-indigo-500.
Effects
- Box Shadow:
shadow-sm,shadow,shadow-xl,shadow-inner. - Opacity:
opacity-0,opacity-75,opacity-100.
This is just the tip of the iceberg. Tailwind provides hundreds of utility classes. The best way to learn is not to memorize them, but to consult the Tailwind CSS official documentation when needed. As you gain experience, you'll become more familiar with commonly used classes.