Tailwind CSS Tutorial menu

Tailwind CSS Utility Classes

Tailwind class names follow simple patterns, so once you learn the patterns you can guess most classes.

The Pattern of a Class Name

Most classes are made of a property part and a value part:

ClassProperty partValue partMeaning
text-lgtextlgLarge font size
text-red-500textred-500Red text colour
bg-whitebgwhiteWhite background
p-4p4Padding on all sides
mt-2mt2Margin on the top
w-fullwfullWidth 100%
rounded-mdroundedmdMedium rounded corners

Some classes have no value part because they do one fixed thing: flex, hidden, italic, underline, uppercase.

The Spacing Scale

Numbers in spacing and size classes come from one scale. Each step is 0.25rem, which is 4px with the normal font size.

NumberSize
10.25rem (4px)
20.5rem (8px)
41rem (16px)
61.5rem (24px)
82rem (32px)
123rem (48px)

So p-4 is 16px padding, and p-8 is 32px. The same numbers work for margin, gap, width and height.

Combining Classes

You build a design by stacking classes. The order of classes in the class attribute does not matter.

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-8 space-y-4">

<p class="text-gray-700">Plain text.</p>
<p class="text-gray-700 font-bold">Bold text.</p>
<p class="text-gray-700 font-bold uppercase">Bold uppercase text.</p>
<p class="text-white font-bold uppercase bg-cyan-600 p-3 rounded-md">Now with a background, padding and rounded corners.</p>

</body>
</html>
Try it Yourself »

space-y-4 on the body adds vertical space between its children.

Building a Button

Here is a typical button. Read the classes one by one; each adds one style.

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-8">

<button class="bg-cyan-500 text-white font-semibold px-5 py-2 rounded-lg shadow-md">
  Enrol Now
</button>

<button class="border-2 border-cyan-500 text-cyan-600 font-semibold px-5 py-2 rounded-lg ml-3">
  Learn More
</button>

</body>
</html>
Try it Yourself »

Arbitrary Values

If the scale does not have the exact value you need, put any CSS value in square brackets:

Example

html
<div class="w-[350px] bg-[#1da1f2] mt-[13px] text-[15px]">
  Exact values in brackets
</div>
ClassCSS
w-[350px]width: 350px
bg-[#1da1f2]background-color: #1da1f2
text-[15px]font-size: 15px

Use arbitrary values only when you really need them. Sticking to the normal scale keeps your design consistent.

Negative Values

Put a minus sign in front of margin classes to pull an element: -mt-4 gives margin-top: -1rem.

Exercise

Exercise

With the default Tailwind scale, how much padding does p-6 add?

Learn Tailwind CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp