Skip to the content

Lesson 26 of 296

Tailwind CSS

What you will learn

  • What Tailwind is and the idea of utility classes
  • How to read and write Tailwind classes
  • Responsive, hover and dark-mode classes
  • Customising the design
  • Advantages and disadvantages
  • What interviewers want to hear about it
On this page

What is Tailwind?

Tailwind CSS is a CSS framework made of thousands of small utility classes. Each class does one thing. You style elements by combining classes in the HTML, instead of writing your own CSS file.

Traditional CSS:

<button class="save-button">Save</button>
.save-button {
  background: #2563eb;
  color: white;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 600;
}
.save-button:hover { background: #1d4ed8; }

Tailwind:

<button class="bg-blue-600 text-white px-4 py-2 rounded-md font-semibold hover:bg-blue-700">
  Save
</button>

Reading Tailwind classes

ClassCSS it adds
p-4padding: 1rem (the spacing scale: 1 = 0.25rem)
px-4 / py-2Left and right padding / top and bottom padding
mt-6margin-top: 1.5rem
w-fullwidth: 100%
max-w-3xlA maximum width
text-lgA larger font size
font-boldfont-weight: 700
text-gray-700A grey text colour
bg-blue-600A blue background (colours go from 50 light to 950 dark)
rounded-lgRounded corners
shadow-mdA medium shadow
flex / griddisplay: flex / display: grid
items-centeralign-items: center
justify-betweenjustify-content: space-between
gap-4gap: 1rem
grid-cols-3Three equal columns

Responsive classes

Tailwind is mobile first. A prefix means "from this width upwards":

<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">

One column on phones, two from 768px, three from 1024px.

PrefixFrom
sm:640px
md:768px
lg:1024px
xl:1280px
2xl:1536px

State classes

<a class="text-blue-600 hover:underline focus-visible:outline-2">Link</a>
<input class="border disabled:opacity-50">
<div class="bg-white dark:bg-gray-900">

hover:, focus-visible:, disabled: and dark: work like the pseudo-classes and media queries you learned in this part.

Customising

Every project can set its own colours, fonts and spacing. In Tailwind v4 (released in 2025) this is done in your CSS file:

@import "tailwindcss";

@theme {
  --color-brand: #1f5fbf;
  --font-display: "Inter", sans-serif;
}

This creates classes like bg-brand and font-display. Older projects using Tailwind v3 set these in a tailwind.config.js file instead. You will meet both at work.

Values outside the scale can be written in square brackets: w-[327px], bg-[#ff5733]. Use these rarely; too many means you are leaving the design system.

Only the classes you use

Tailwind scans your files and puts only the classes you actually used into the final CSS file. So even though there are thousands of possible classes, the real CSS file is usually small.

Trap: Tailwind cannot see class names built from pieces at runtime.

Bad:   class={"text-" + color + "-600"}
Good:  class={color === "red" ? "text-red-600" : "text-blue-600"}

In the bad version, the full class name never appears in your code, so Tailwind does not create it.

Reusing styles

Long class lists repeated everywhere are hard to maintain. The usual answer in React projects is a component: write the button once as a <Button> component, and reuse the component. You will learn components in Part 12.

Tailwind also has @apply to group utilities into a normal CSS class, but the Tailwind team recommends using it sparingly.

Advantages and disadvantages

AdvantagesDisadvantages
Very fast to build with once you know the class namesThe HTML gets long and busy
No time spent inventing class namesYou must learn the class names
Consistent spacing and colours from a fixed scaleCan hide weak CSS understanding
Small final CSS fileLong class lists repeated if you do not use components
No unused CSS left behind after changesAnother tool and build step to maintain

What interviewers want to hear

  • That you understand the CSS underneath. flex items-center is just Flexbox. If you cannot explain Flexbox without Tailwind, that is a red flag.
  • That you know the trade-offs, not only that you like it.
  • That you avoid repetition with components.

Common mistakes

  • Building class names from string pieces.
  • Copying the same 15-class button into 40 places instead of making a component.
  • Using square-bracket values everywhere and breaking the design system.
  • Not knowing the CSS that a class produces.

How to say it in an interview

"Tailwind is a utility-first framework: each class does one thing, and I compose them in the markup. It is mobile first with responsive prefixes like md and lg, and state prefixes like hover and dark. It only ships the classes I actually use. To avoid repetition I wrap common patterns in components. The main trade-off is busier markup, and it only works well if you already understand the CSS underneath, like Flexbox and Grid."

Key words

WordMeaning
Utility classA class that does exactly one small styling job
Utility-firstBuilding designs mainly from utility classes
Responsive prefixA prefix like md: that applies from a screen width
ThemeThe project's set of colours, fonts and spacing
Arbitrary valueA one-off value in square brackets, like w-[327px]
@applyGroups utilities into a normal CSS class

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.