SkillPilotAI

Styling options and Tailwind v4

Styling decides what the user sees first. This note covers why plain CSS class names clash, how CSS Modules scope them, how Tailwind v4 is set up and used (utilities, variants, theme), and the cn() helper used in almost every Tailwind project.

51.1 Why is it used?

  • In plain CSS every class is global: a .title in Card.css and a .title in Modal.css hit each other.
  • When two files use the same class name, the CSS that loads later wins, so the Card title turns red.
  • Such bugs are hard to find in big teams, so we need scoped styling that stays inside one component.
  • Tailwind adds speed and a consistent design: spacing and colours come from a fixed scale.
CSS
/* Card.css */.title { color: green; } /* Modal.css */.title { color: red; }
Output
Card title: red (expected green)Modal title: red
Why the Card title turned red
  1. 1.Card.css defines .title { color: green }

  2. 2.Modal.css defines .title { color: red }

  3. 3.Both classes are global: they apply to the whole app

  4. 4.Same name, so the CSS loaded later wins

  5. 5.Card title shows red instead of green

Fix: scoped styling (CSS Modules) or utility classes (Tailwind)

51.2 Fix 1: CSS Modules

  • Name the file Card.module.css and write normal CSS inside it.
  • Import it as styles and use className={styles.title}.
  • Vite adds a unique hash to every class name, so .title of Card can never clash with .title of Modal.
React (JSX)
import styles from "./Card.module.css"; export function Card({ name }) {  return (    <div>      <h3 className={styles.title}>{name}</h3>    </div>  );}
Output
Card title: greenModal title: red

51.3 Tailwind v4 setup (Vite)

  • Install tailwindcss and its Vite plugin, then add tailwindcss() to the plugins list. That is the whole config.
  • In your main CSS file, one line @import "tailwindcss" brings in all of Tailwind.
  • v3 needed a separate tailwind.config.js; in v4 the config lives in CSS inside @theme.
  • A theme variable like --color-brand automatically creates classes such as bg-brand and text-brand.
Terminal
npm install tailwindcss @tailwindcss/vite
JavaScript
import { defineConfig } from "vite";import react from "@vitejs/plugin-react";import tailwindcss from "@tailwindcss/vite"; export default defineConfig({  plugins: [react(), tailwindcss()],});
CSS
@import "tailwindcss"; @theme {  --color-brand: #0b7fa6;}

51.4 Utility classes

  • Write small single-purpose classes straight in className: rounded-xl, border, p-4, text-lg, font-semibold.
  • Each class does one job, so you can read the JSX and know how the card looks without opening a CSS file.
React (JSX)
export function ProductCard({ name, price }) {  return (    <div className="rounded-xl border p-4">      <h3 className="text-lg font-semibold">        {name}      </h3>      <p className="text-gray-600">        ₹{price}      </p>      <button className="mt-3 bg-brand px-4">        Add to cart      </button>    </div>  );}
Output
# Wireless Mouse₹799[button] Add to cart

51.5 Variants: md, hover, dark

  • A variant is a condition in front of a class: md:, hover:, disabled:, dark:.
  • Tailwind is mobile first: w-full applies on mobile, md:w-auto takes over from 768px upwards.
  • hover:bg-sky-700 and disabled:opacity-50 style states; dark:bg-sky-500 applies in dark mode.
React (JSX)
export function BuyButton({ disabled }) {  return (    <button      disabled={disabled}      className="w-full bg-brand px-4 py-2        text-white md:w-auto        hover:bg-sky-700 disabled:opacity-50        dark:bg-sky-500"    >      Buy now    </button>  );}
Output
Desktop 768px+: auto width[button] Buy now
How Tailwind works
  1. 1.You write classes in className in JSX

  2. 2.At build time Tailwind scans your source files as plain text

  3. 3.It generates CSS only for the classes it actually found

  4. 4.The final CSS file stays small, with zero runtime cost in the browser

Tailwind never runs your code: it only reads text

51.6 Checkpoint: dynamic class names

  • Question: Badge builds `bg-${color}-500` and we pass color="red". Will the background be red?
  • Answer: no. Tailwind scans text, and the full name bg-red-500 is written nowhere, so no CSS is generated.
  • Fix: write full class names in an object and pick one with the prop.
React (JSX)
function Badge({ color }) {  return (    <span className={`bg-${color}-500 px-2`}>      New    </span>  );} export function App() {  return <Badge color="red" />;}
Output
New  (no background)
React (JSX)
const colors = {  red: "bg-red-500",  green: "bg-green-500",}; function Badge({ color }) {  return <span className={`${colors[color]} px-2`}>New</span>;}

51.7 cn() = clsx + tailwind-merge

  • clsx adds classes based on conditions (false/undefined values are dropped).
  • tailwind-merge resolves conflicts: when px-2 and px-4 both appear, the last one wins.
  • cn() combines both and is found in almost every Tailwind project (shadcn/ui creates it for you).
JavaScript
import { clsx } from "clsx";import { twMerge } from "tailwind-merge"; export function cn(...inputs) {  return twMerge(clsx(inputs));} const sale = true;console.log(cn("px-2", sale && "bg-brand"));console.log(cn("px-2", !sale && "bg-brand"));console.log(cn("px-2 py-1", "px-4"));
Output
> px-2 bg-brand> px-2> py-1 px-4

51.8 A Button with variants

  • A variants object maps a variant name (primary, outline) to its classes.
  • Order inside cn(): base classes, then the variant, then the parent's className last, so the parent can override.
  • <Button>Save</Button> is primary by default; <Button variant="outline">Cancel</Button> gets the border style.
React (JSX)
import { cn } from "./lib/utils"; const variants = {  primary: "bg-brand text-white",  outline: "border border-gray-300",}; export function Button({  variant = "primary", className, ...rest}) {  const cls = cn("rounded-lg px-4 py-2",    variants[variant], className);  return <button className={cls} {...rest} />;}
Output
[button] Save[button] Cancel

51.9 Comparison: CSS Modules vs Tailwind vs CSS-in-JS

PointCSS ModulesTailwindCSS-in-JS (styled-components)
How you write itNormal CSS in .module.cssUtility classes in classNameCSS inside JavaScript
ScopingHashed class namesNo custom class names to clashGenerated class names
Runtime costNoneNone (build-time CSS)Yes, styles created at runtime
Server ComponentsWorksWorksRuntime libraries do not work directly
DownsideSeparate file, naming still neededLong className stringsBundle and runtime cost
Use whenTeam prefers plain CSSDefault for new React/Next.js appsExisting codebases already on it

51.10 Common mistakes

  • Joining base classes and the parent's className with a template string: both px-4 and px-2 end up on the button and px-4 wins (it comes later in Tailwind's CSS).
  • Building class names dynamically (bg-${color}-500): the class is never generated.
  • Mixing CSS Modules, Tailwind and CSS-in-JS in one project: hard to maintain.
  • Thinking Tailwind = inline styles: inline styles cannot do hover, media queries or dark mode; Tailwind generates real CSS classes.
React (JSX)
function Button({ className, ...props }) {  return (    <button      className={`px-4 py-2 ${className}`}      {...props}    />  );}// <Button className="px-2">Save</Button>// Both px-4 and px-2, px-4 wins
React (JSX)
import { cn } from "./lib/utils"; function Button({ className, ...props }) {  return (    <button      className={cn("px-4 py-2", className)}      {...props}    />  );}// <Button className="px-2">Save</Button>// Result: py-2 px-2

51.11 Best practices

  • One project, one main styling approach.
  • Always write full Tailwind class names; in reusable components use cn() with the parent's className last.
  • Install the Tailwind CSS IntelliSense extension and the Tailwind Prettier plugin for autocomplete and class sorting.

51.12 Interview tips

  • "Which styling approach?" It depends on the team, the design system and the rendering setup.
  • CSS Modules give scoped plain CSS; Tailwind is utility first and generates only the classes you use. Both have zero runtime cost.
  • Runtime CSS-in-JS (styled-components) does not work directly with React Server Components.
  • Know cn(): clsx for conditions + tailwind-merge for conflicts; the parent's className goes last.
  • Tailwind v4: @import "tailwindcss" + @theme in CSS, no tailwind.config.js needed.