shadcn/ui with Tailwind v4: themes, dark mode and variants
Build a pricing page with shadcn/ui on a brand-new Next.js 16 project, then make it yours: your brand colour and corner radius, a light, dark and system theme menu, a colour of your own and a component variant to go with it. Every file is shown in full, and every step was run on a fresh project before this was published.
Tested step by step on with Next.js 16.4.6, the shadcn CLI 4.23.0 and Tailwind CSS 4.3.5
By the end of this guide you’ll have a pricing page with three plans, a monthly/yearly switch and a theme menu, built from shadcn/ui components. Then you’ll change how the whole page looks without touching a single component: a new brand colour, rounder corners, a dark mode that follows the visitor’s system, and a green “Save 17%” badge that uses a colour you added yourself.
The idea that makes all of it work is small. Every shadcn/ui component paints itself with a handful of named colours, like primary, muted and border, and each of those names is a CSS variable in one file, app/globals.css. Change a variable and every component that uses it changes with it. Once you see that, theming stops being a chore.
Every file is shown whole, so you can copy it as it is. The guide was run from start to finish on a fresh create-next-app project and on a fresh shadcn init project, with Next.js 16.4, the shadcn CLI 4.23, Tailwind CSS 4.3 and next-themes 0.4.
Before you start
- Node.js 20.9 or newer. Check with
node -v. - A new Next.js 16 app. Either
npx create-next-app@latest my-app --yesornpx shadcn@latest initworks. Accept the defaults. - A code editor. VS Code with the Tailwind CSS IntelliSense extension shows each colour beside its class, which helps a lot here.
Run every command from your project’s folder, the one with package.json in it.
1. Add shadcn/ui to your project
If you created your project with npx shadcn@latest init, this is already done. Skip to step 2.
On a create-next-app project, run the setup command. --defaults accepts the standard choices: the Nova style, neutral greys, and Base UI underneath the components.
It finishes with “Project initialization completed.” Here’s what it did:
- Wrote
components.json, the settings the shadcn command line reads every time you add a component: where components go, which style, which icons. - Rewrote
app/globals.csswith the theme: about thirty colour variables for light mode, the same again for dark mode, and the lines that turn them into Tailwind classes. You’ll read it in step 5. - Added
components/ui/button.tsxandlib/utils.ts, and installed the packages they need. - There is no
tailwind.config.js. With Tailwind v4 the whole theme lives in CSS, which is whycomponents.jsonlists the config as an empty string.
components/ui/button.tsx is now your file. That’s what makes the customising in steps 6 to 9 possible, and it’s also why you should change them with some care.2. Fix the layout and add dark mode support
Two things in the root layout need to change before you build anything.
- The font. The theme reads the font from a variable called
--font-sans, but thecreate-next-applayout names it--font-geist-sans. Until the names match, the page falls back to the browser’s default font, which is usually Times. - Dark mode. shadcn’s dark colours apply when the
<html>element has the classdark. Something has to add and remove that class, remember the visitor’s choice, and follow their system setting. Thenext-themespackage does exactly that.
Install next-themes (the shadcn init starter already has it, and running this again changes nothing):
next-themes has to run in the browser, but your layout is a server component. A small client wrapper solves that. Create components/theme-provider.tsx (the shadcn init starter has one already; replace it with this):
Now replace app/layout.tsx with this:
attribute="class"makesnext-themessetclass="dark"on<html>. Leave it out and it setsdata-theme="dark"instead, which shadcn’s styles don’t look at, so nothing changes.defaultTheme="system"andenableSystemstart every new visitor on their operating system’s setting.disableTransitionOnChangeswitches off CSS transitions for the moment the theme changes, so buttons don’t fade between colours one by one.suppressHydrationWarningis needed becausenext-themesadds the class to<html>before React loads, so the browser’s<html>differs from the server’s on purpose. It applies to that one element only.
3. Add the components
The pricing page uses five more components. Add them all in one command:
It lists the five files it created, all in components/ui/. Open components/ui/badge.tsx and have a look: it’s an ordinary React component with a list of variants (default, secondary, destructive and so on). You’ll add one of your own in step 8.
4. Build the pricing page
Start with one plan card. Rather than repeat the same Card markup three times, put it in a component of your own. Create components/plan-card.tsx:
Cardcomes in parts (CardHeader,CardTitle,CardDescription,CardAction,CardContent,CardFooter). Use the parts rather than putting everything inCardContent: each one sets its own spacing, andCardActionsits in the header’s top-right corner.cn()joins class names and settles conflicts. TheCardcomponent already has a thinring-1; becausecnputs your classes last and merges them,ring-2 ring-primaryreplaces it on the featured plan instead of fighting it.- Notice what isn’t here: no colour codes.
text-primary,ring-primaryandtext-muted-foregroundare theme names, so this card will pick up your brand colour in step 6 without being edited.
Next, the three plans and the monthly/yearly switch. The switch needs state, so this one is a client component. Create components/pricing.tsx:
The id on the Switch and the htmlFor on the Label connect the two, so clicking the words “Bill yearly” flips the switch, and a screen reader announces the switch by that name.
Then the theme menu: a button with a sun or a moon that opens Light, Dark and System. Create components/mode-toggle.tsx:
render={<Button … />}tells the menu’s trigger to draw itself as yourButton. The components frominit --defaultsare built on Base UI, which usesrenderfor this. Older guides, built on Radix, writeasChildinstead, which these components don’t have.- The sun and moon are both rendered, and CSS hides one:
dark:hiddenhides the sun in dark mode,hidden dark:blockshows the moon only then. Choosing the icon withthemein JavaScript would draw the wrong one on the server, which doesn’t know the visitor’s theme, and React would warn about the mismatch. DropdownMenuLabelmust sit inside aDropdownMenuGroup, as it does here. Outside one, opening the menu crashes the page (see “If something goes wrong” below).
Last, the page itself. Replace app/page.tsx with this:
Start the development server:
Open http://localhost:3000. You should see “Simple pricing”, three cards in a row (stacked on a phone) with the Pro card outlined, and the switch above them. Flip the switch: Pro goes from $15/month to $150/year. Open the sun menu and pick Dark: the whole page turns dark, and the sun becomes a moon. Pick System and it follows your computer’s setting.
Everything is black, white and grey for now. That’s shadcn’s neutral default, and it’s what you’ll change next.
5. How the theme works
Open app/globals.css. It’s long, but it has only three kinds of part. Here they are, cut down to the lines for one colour:
:rootholds the light theme: a value for each name.--primaryis the colour of main buttons and highlights;--primary-foregroundis the colour of text on top of it. Every colour comes in such a pair, so text on a coloured surface always has a colour chosen to be readable on it..darkholds the dark theme: the same names, different values. When<html>has the classdark, these win.@theme inlineturns the names into Tailwind classes. The line--color-primary: var(--primary)is what makesbg-primary,text-primaryandring-primaryexist.inlinemeans the class uses the variable itself,var(--primary), rather than a copy of its value, so the class changes the moment the variable does. That’s why a dark theme needs nodark:classes in your components.@custom-variant darkis what makesdark:classes (like the sun icon’sdark:hidden) mean “inside an element with the classdark”, rather than Tailwind’s default of following the system setting.--radiusis one length, and@theme inlinederivesrounded-smthroughrounded-4xlfrom it. Change it once and every corner changes in proportion.
The values are written in OKLCH: oklch(lightness chroma hue). Lightness runs from 0 (black) to 1 (white), chroma is how colourful it is (0 is grey), and hue is an angle on the colour wheel: about 25 is red, 155 green, 265 blue, 300 purple. The reason shadcn uses it is that the lightness number matches what your eye sees, so two colours with the same lightness look equally light, whatever their hue. That makes it much easier to pick a dark-mode version or swap a brand colour without wrecking the contrast.
6. Give it your brand colour and radius
You could edit the values in the :root and .dark blocks directly. A tidier way is to leave shadcn’s defaults alone and add your changes in one block at the end of the file. A later rule with the same selector wins, so these values replace the defaults, and everything that’s yours is in one place.
Add this to the end of app/globals.css:
--primaryis an indigo (hue 277). In light mode it’s fairly dark (0.51), so white text on it is easy to read.- In dark mode the same hue is lighter (0.72) and a little less saturated, so it doesn’t glare on a dark page, and the text on it turns dark indigo instead of white.
--ringis the focus outline you see when you tab to a button or the switch. Matching it to the brand keeps keyboard focus visible and on-brand.--radiusgoes from 0.625rem to 0.75rem: slightly rounder cards and buttons everywhere.
Save, and look at the page without reloading. The Pro card’s outline, the “Upgrade to Pro” button, the “Most popular” badge, every tick in the lists and the switch (when it’s on) are now indigo, in both themes. You didn’t edit a component.
--primary with white text, and near 0.7 for dark mode with dark text. Change the hue to taste (25 red, 155 green, 230 teal, 265 blue, 300 purple) and the contrast stays about the same. oklch.com shows any value as a colour.7. Add a colour of your own
The theme has no colour for good news, and the “Save 17%” badge should be green. Writing text-green-600 on it would work, but it would be the one thing on the page that ignores your theme and dark mode. Add a success colour instead, the same way shadcn adds its own: a value for each theme, and a line in @theme inline to turn it into classes.
Add this to the end of app/globals.css:
That gives you bg-success, text-success, border-success, bg-success/10 (10% opacity) and the rest, all switching with the theme. A second @theme inline block is fine: Tailwind reads them all.
8. Add a variant of your own
You could now write className="bg-success/10 text-success" on the badge. That works once. If “success” badges will show up all over your app, make it a variant, so every one of them looks the same and you can change them all in one place.
Open components/ui/badge.tsx. Inside variants, under variant, find the line that starts with link: and add this line after it:
bg-success/10is a pale green wash andtext-successis the full green, the same soft style as the built-indestructivebadge.dark:bg-success/20makes the wash a little stronger on a dark page, where 10% would almost vanish.- TypeScript picks the new name up by itself:
variantnow accepts"success", and your editor offers it.
Now use it. Replace components/pricing.tsx with this; the only change is the badge’s variant:
“Save 17%” is now green on a pale green, and a lighter green on a darker wash in dark mode.
9. Change components without losing your changes
You’ve now changed how components look in four different ways. Each has its place, and choosing the right one is what keeps a theme easy to maintain as the app grows:
- The theme (steps 6 and 7) for anything that should change everywhere: colours, radius, fonts. Nothing in
components/uichanges. classNameon one use for layout and one-offs:w-fullon a button, the featured plan’sring-2.cnmakes your class win over the component’s own.- A wrapper component (
PlanCard) for a combination you repeat. It builds onCardandButtonwithout changing them. - A new variant inside
components/ui(step 8) for a style the component itself should offer. This is the only one of the four that edits shadcn’s code.
That last one matters when shadcn releases a newer version of a component. Running add again with --overwrite replaces the file, and your variant with it. Before you ever do that, ask the command line what would change:
It changes nothing and prints a diff. Lines starting with - are yours that an update would remove. Here you’ll see your success: line marked -. If you do update, copy those lines back in afterwards. Keeping edits inside components/ui few and small is what keeps this easy.
10. Check it and build
- Press Tab from the top of the page. The theme button, the switch and each plan button should get a visible indigo outline in turn. That’s
--ring. - Check the contrast of anything you recoloured. In Chrome, right-click “Save 17%” → Inspect, then click the colour square next to
colorin the Styles panel: it shows the contrast ratio. 4.5 or more is the bar for normal text (WCAG AA). With the values above, “Save 17%” is about 5.2 in light mode and 7.3 in dark, and the indigo buttons about 6.0 and 7.0. - Narrow the window to phone width: the cards stack in one column.
Then stop the dev server with Ctrl+C and make sure everything compiles the way Vercel will build it:
Lint and the type check finish without errors. The build ends with a list of routes, / marked ○ (Static): the page is built once, ahead of time, and the theme still works because it’s all CSS and one small script from next-themes.
If something goes wrong
- The page is in Times (a serif font). The layout still names its font
--font-geist-sans. Use the layout from step 2, which names it--font-sans. - Picking Dark does nothing. Check
attribute="class"onThemeProviderinapp/layout.tsx. Without it,next-themeswritesdata-theme="dark"on<html>, and shadcn’s styles only look for the class. - Opening the theme menu crashes with “Base UI: MenuGroupContext is missing.” A
DropdownMenuLabelis outside aDropdownMenuGroup. Wrap the label and the items in one, as in step 4. - TypeScript says
Property 'asChild' does not exist. These components are built on Base UI, which usesrender={<Button />}instead.asChildis the Radix way, from older guides. bg-successdoes nothing, with no error. Tailwind only makes classes for names in@theme. Check the@theme inlineblock from step 7 is inapp/globals.cssand spelled--color-success.- A warning about
classNamenot matching on<html>.suppressHydrationWarningis missing from<html>in the layout.
Questions people ask
Do I still need tailwind.config.js with Tailwind v4?
No. With Tailwind v4 the theme lives in your CSS file. The @theme inline block in app/globals.css is what turns shadcn’s variables into classes like bg-primary, and a new colour is two variables plus one line there. That’s why components.json lists the Tailwind config as an empty string. You only need a config file for a plugin that asks for one.
How do I change the colours of every shadcn/ui component at once?
Change the CSS variables, not the components. Every component paints itself with names like primary and muted, and their values live in the :root (light) and .dark blocks of app/globals.css. Put your own values in a block at the end of that file and they win over the defaults, so every button, badge, switch and focus ring follows in both themes.
Why does shadcn/ui use OKLCH colours?
Because the lightness number in OKLCH matches what your eye sees. Two colours with the same lightness look equally light whatever their hue, so you can swap a blue brand for a purple one and keep the same contrast with the text on it. Making a dark-mode version is mostly raising the lightness. Every current browser supports it.
Will updating a shadcn/ui component erase my changes?
It can. Running add with --overwrite replaces the file, including any variant you added. Run npx shadcn@latest add <name> --diff first: it changes nothing and lists the lines an update would remove, marked with a minus. Keep most changes in the theme, in className and in wrapper components, and only small, deliberate edits inside components/ui.
Where to go next
- Make those plan buttons take real money: Stripe payments in Next.js builds checkout, webhooks and orders on a fresh Next.js 16 project.
- Put the pricing page behind a real product with accounts: Better Auth in Next.js.
- Browse the components and their examples at ui.shadcn.com, and try other colour themes with
npx shadcn@latest apply, which rewrites the variables for you.