Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

React
  • One Request, Two Answers: the Same Post as an HTML Page and as JSON
  • A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
  • How a Web Page Works: HTML Tags, Elements and Attributes
  • Semantic HTML: header, nav, main, aside and footer as Page Landmarks
  • HTML Forms: Labels, Names, Input Types and Where the Data Goes (GET vs POST)
  • CSS Basics: Selectors, Which Rule Wins, and the Box Model
  • CSS Flexbox vs Grid: A Nav Bar, a Card Row and a Dashboard
  • Responsive CSS: Viewport, Mobile-First Media Queries, clamp() and Dark Mode with Tokens
  • JavaScript for People Who Already Code: Syntax, Modules and Five Surprises
  • JavaScript Arrays the Functional Way: map, filter, reduce, find, sort and Object.entries
  • Async JavaScript: Promises, async/await, fetch and Promise.all
  • JavaScript in the Page: the DOM, querySelector and Events
  • npm, package.json and Vite: What npm install Fetches and How Vite Serves and Builds Your Page
  • TypeScript Essentials: Types Checked, Then Erased (Interfaces, Unions, Generics)
  • Why React, JSX and Components: The Page as a Function of Data
  • React Props and Composition: Data Down, Calls Up, Children in the Slot
  • Routing With React Router: One Page, Many Addresses
  • React Component Tests With Vitest and Testing Library: Test What the User Sees
  • End-to-End Tests for a React App With Playwright: A Full Dress Rehearsal
  • Why AI-Built Websites All Look the Same, and Four Levels That Fix It
  • Visual Hierarchy, Spacing and the 8-Point Grid: Tell the Eye What to Read First
  • Colour, Contrast and Dark Palettes: 60-30-10, Semantic Tokens and Ratios You Can Compute
  • Typography That Sells: Type Scale, Line Length and Free Font Pairings
  • The UI Element Catalogue: Buttons to Toasts, Their Jobs, States and shadcn/ui Names
  • Design Tokens: Primitive and Semantic Tokens, from Figma to CSS to Tailwind
  • Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode
  • shadcn/ui in React: Components You Own, Edit and Commit
  • Design Tokens to a Tailwind Theme: Light and Dark Mode with @theme inline
  • Motion in React: Entrance, Hover, Exit and Layout Animations
  • A React Dashboard: Stat Cards, a Recharts Line Chart and a Sortable Table
  • Accessibility in Practice: Dialogs, Menus and the Keyboard in React
  • Reference-Driven Design: Extract a DESIGN.md From a Site You Admire and Build From It
  • Components, Variants and Variables: One Design System in Figma and in CSS
  • Figma MCP Server: Build a React Component From a Frame, Then Write It Back
  • Claude Design: From a Brief to a Clickable Prototype, Then Hand It Off to Claude Code
  • Critiquing AI-Generated UI: Six Checks for Every Screen
  • The AI Design Landscape: Prompt to UI, Prompt to App, and Design to Code
  • Your Brand Rules Folder: One Folder Every AI-Built Page Follows
  • The Best-Practice UI Prompt: Plain Sentences That Beat the Average AI Page
  • UI Ideas From Figma Make, Google Stitch and ChatGPT: One Prompt, Three Designs
  • Google Stitch From a Sketch or Screenshot: Show the Layout, Say What to Keep
  • Stitch to Figma to Code: Turn a Stitch Screen Into Code That Uses Your Tokens
  • Figma Make: Prompt to Prototype With a Four-Part Prompt
  • Upgrade a Dated UI From One Screenshot (or a Reference Image)
  • States and Feedback: Make Every Click Get an Answer
  • A Design-Loop Skill: Claude Screenshots, Scores and Fixes Its Own UI
  • Copy Without the AI Tells: A Copy Pass on a Landing Page
  • React State, Events, Lists and Conditional Rendering: Follow One Click
  • React useEffect: When It Runs, What Cleanup Is For, and When You Don't Need One
  • Lifting State Up, Refs and React DevTools: Put State Where It Is Needed
  • Forms and Actions in React 19: Controlled Inputs, Form Actions, useActionState and useFormStatus
  • React Context and Custom Hooks: Share One Value, Reuse the Logic
  • Server State With TanStack Query: One Key, One Request, One Cache
  • Mutations and Optimistic Updates in TanStack Query: Change Data, Then Tell the Cache
  • Generate TypeScript Types, Query Hooks and Zod From an OpenAPI Spec With Orval
  • A Mock Backend in the Browser With Mock Service Worker, Generated From the Spec
  • A React Client for a FastAPI API: The CORS Wall, CORSMiddleware and the Vite Proxy
  • Forms With React Hook Form and Zod: The API's Rules in the Browser, the Server's Errors Under the Field
  • JWT Auth in a React Client With a FastAPI Backend: Log In, Carry the Token, Let the Server Decide
  1. Blog
  2. /
  3. React
  4. /
  5. Fundamentals

Tailwind CSS 4 in React: Utility Classes, Responsive Prefixes, Hover and Dark Mode

Style a React task card with Tailwind CSS 4: install the Vite plugin, read utility classes, go from one column to two with md:, and add hover, focus and dark mode with one prefix each.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Fundamentals

What you will learn

  • Set up Tailwind CSS 4 in a Vite React app in three steps
  • Read utility classes like mt-4, px-4 and bg-indigo-600 and know what they do
  • Make a layout responsive with md: and other breakpoint prefixes
  • Add hover, focus and dark mode styles with prefixes

Before you start

  • React components and props
  • Basic HTML and CSS

You have a task card on a page: a title, a short text and a button. It needs to look good. In this article you style it with Tailwind CSS 4 in a React app, with spacing, colour, hover, a responsive grid and dark mode.

Carve every piece, or snap bricks

With plain CSS, styling is like carving. For every piece you invent a name, like plain-card. Then you jump to another file. Then you carve the rules, one new shape every time.

Tailwind is more like LEGO. You get small ready-made bricks, and each brick does one job:

  • bg-white: a white background brick.
  • p-6: a padding brick, the space inside the card.
  • rounded-2xl: a rounded-corner brick.
  • shadow-lg: a soft shadow brick.

Small classes, snapped together right in your markup. That is what utility first means.

Setup: three steps, no config file

Step one, install Tailwind and its Vite plugin:

npm install tailwindcss @tailwindcss/vite

Step two, add the plugin to vite.config.ts, next to React:

import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [react(), tailwindcss()],
})

Step three, one line in src/index.css:

@import "tailwindcss";

That is all. There is no tailwind.config.js. Tailwind 4 reads your files and builds only the classes you use, so the final CSS stays small.

Plain CSS or utility classes: the same button

Here is the button in plain CSS, from PlainCard.css:

.plain-card button {
  margin-top: 16px;
  padding: 8px 16px;
  border-radius: 8px;
  background: #4f46e5;
  color: #fff;
  font-weight: 600;
}

.plain-card button:hover {
  background: #4338ca;
}

Margin, padding, radius, background, colour, font weight, then a second rule for hover. That is a lot of lines for one button. Here is the Tailwind version, written on the button itself:

<button className="mt-4 w-full rounded-lg bg-indigo-600
  px-4 py-2 font-semibold text-white hover:bg-indigo-700
  focus:outline-none focus:ring-4 focus:ring-indigo-300
  sm:w-auto">

Read it piece by piece:

ClassMeaning
mt-4Margin on top. The number times 4px: 4 x 4 = 16px
px-4 py-2Padding: 16px across, 8px down
rounded-lgRounded corners
bg-indigo-600Indigo background
hover:bg-indigo-700Darker indigo while the mouse is on it

No new name and no second file. The style sits right on the element.

The card: classes on each tag

Here is the full card component, Card.tsx:

type CardProps = { title: string; text: string }

export default function Card({ title, text }: CardProps) {
  return (
    <div className="rounded-2xl bg-white p-6 shadow-lg
      dark:bg-slate-800">
      <h2 className="text-2xl font-bold text-slate-900
        dark:text-white">{title}</h2>
      <p className="mt-2 text-slate-600
        dark:text-slate-300">{text}</p>
      <button className="mt-4 w-full rounded-lg bg-indigo-600
        px-4 py-2 font-semibold text-white hover:bg-indigo-700
        focus:outline-none focus:ring-4 focus:ring-indigo-300
        sm:w-auto">
        Mark done
      </button>
    </div>
  )
}

The outer div has rounded-2xl shadow-lg bg-white p-6, which makes the white card with soft corners. The heading has text-2xl font-bold, so big, bold text. The button has the margin, padding and indigo classes you just read. Run it with npm run dev.

Do not worry about memorising class names. AI can write them for you. What you need is to be able to read them.

Responsive: a prefix means "from this width up"

The page lays out the cards in App.tsx:

import Card from './Card'

export default function App() {
  return (
    <main className="min-h-screen bg-slate-100 p-6
      dark:bg-slate-900">
      <h1 className="mb-6 text-3xl font-bold text-slate-900
        dark:text-white">Task cards</h1>
      <div className="grid gap-6 md:grid-cols-2">
        <Card title="Write report" text="Summarise the quarter." />
        <Card title="Email Jack" text="Send the budget." />
      </div>
    </main>
  )
}

Look at grid gap-6 md:grid-cols-2. On a phone, 375px wide, the md: rule is ignored, so you get one column. On an 800px screen, the md: rule kicks in and you get two columns.

The Task cards page at 800px wide: two white cards side by side, Write report and Email Jack, each with a short line of text and an indigo Mark done button.
At 800px, md:grid-cols-2 gives two columns.
The same page at 375px wide: the two cards stacked in one column, each with a full-width indigo Mark done button.
At 375px the md: rule is ignored: one column, and w-full makes the button stretch.

Same code, no media query. The prefixes start at fixed widths:

  • sm: from 640px
  • md: from 768px
  • lg: from 1024px

So think mobile first. Write the small screen, then add md: and lg: when there is more room. The button does the same thing: w-full on a phone, sm:w-auto from 640px up.

Hover and focus are just prefixes

hover:bg-indigo-700 turns the button darker while the mouse is on it. Focus works the same way: focus:ring-4 focus:ring-indigo-300 draws a ring around the button when the keyboard reaches it, which matters for accessibility.

Two copies of the Write report card. Left: the Mark done button in a darker indigo while hovered. Right: the button with a light indigo focus ring around it.
Left: hover. Right: keyboard focus with the ring.

You write the state before the class, and Tailwind applies it only at that moment.

Dark mode: one extra class

For dark mode, each tag gets one more class: dark:bg-slate-800 on the card, dark:text-white on the heading, dark:text-slate-300 on the text.

The Write report card twice. Left: system set to light, a white card on a light grey page. Right: system set to dark, a slate card on a near-black page with white heading text.
Same card, system set to light (left) and dark (right).

There is no toggle code and no extra file. By default, Tailwind's dark: follows the system setting. If you want a button that switches the theme, and colours that come from your design instead of the Tailwind palette, read design tokens to a Tailwind theme.

Common mistakes

  • Looking for tailwind.config.js. Tailwind 4 with the Vite plugin does not need one. The @import "tailwindcss"; line is the setup.
  • Thinking md: means "only on medium screens". It means from 768px up. Write the phone style without a prefix.
  • Removing the focus ring because it looks odd. Keyboard users need it to know where they are.
  • Forgetting the dark text classes. dark:bg-slate-800 alone gives you dark text on a dark card.

Recap

NeedTailwind
Spacingp-6, mt-4, px-4 py-2 (number x 4px)
Colourbg-indigo-600, text-slate-600
Screen sizesm: md: lg:, from that width up
Stateshover: focus:
Dark modedark:, follows the system by default

These are the foundations: utilities, prefixes and how to read them. With AI you can generate the long class lists, and you will know what they mean.

Keep going in React · 26 of 59

Previous
Design Tokens: Primitive and Semantic Tokens, from Figma to CSS to Tailwind
Next post
shadcn/ui in React: Components You Own, Edit and Commit
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Related posts

One Request, Two Answers: the Same Post as an HTML Page and as JSON

Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.

A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx

Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.

How a Web Page Works: HTML Tags, Elements and Attributes

A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.

Semantic HTML: header, nav, main, aside and footer as Page Landmarks

The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.

On this page

  • Carve every piece, or snap bricks
  • Setup: three steps, no config file
  • Plain CSS or utility classes: the same button
  • The card: classes on each tag
  • Responsive: a prefix means "from this width up"
  • Hover and focus are just prefixes
  • Dark mode: one extra class
  • Common mistakes
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

The Python Architect Masterclass: Full Stack to AI Engineer

Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.