DocsFrontend

Styling and Tailwind

Tailwind out of the box, with nothing to install or configure; theme tokens, your own CSS, and cn to join and merge classes.

Write Tailwind’s classes anywhere in your app and they’re styled: Sluurp reads the classes your files use and serves the CSS for them. There’s no npm install, no tailwind.config and no build step. To style an element, give it classes:

app.tsx
<button class="rounded-lg bg-primary px-4 py-2 text-primary-foreground hover:opacity-90 md:px-6">Save</button>

Features include:

  • Tailwind’s classes, variants and arbitrary values, with the CSS made from your files as you save
  • Theme tokens for light and dark, named for what they’re for
  • Layouts that fit any room, with container variants
  • Your own stylesheets, swapped in place, and stylesheets components bring
  • cn and cva, as shadcn’s components use them

Tailwind, with nothing to set up

Every page loads two stylesheets, in this order:

  • /_/tw.css: Tailwind’s preflight and the utilities your app uses. It’s generated from your current files, and regenerated as you save, while the page updates in place.
  • /_/theme.css: the theme, meaning colours, fonts and radii as CSS variables, for light and dark, and its base styles (borders in the theme’s border colour).

The theme comes second, as your own CSS comes after @import "tailwindcss" in a Tailwind project: its base styles follow the preflight they adjust.

What Tailwind offers is there:

  • variants such as hover:, focus-visible:, md:, lg: and dark:;
  • arbitrary values, such as w-[312px] and bg-[#0d99ff];
  • opacity, as in bg-primary/80;
  • and the rest of Tailwind’s classes.

Fitting every device

Lay pages out from the room they have, not from the screen’s width: grids of auto-fit columns (grid-cols-[repeat(auto-fit,minmax(16rem,1fr))]), rows that wrap (flex flex-wrap), and container variants (@3xl:grid-cols-2). Then an app fits a phone, a tablet, a desktop or a window beside a sidebar.

When a component needs a different arrangement at some size, use a container variant. Make the page’s content a container (container-type: inline-size), and the body one too, for dialogs and sheets. Keep screen breakpoints for the app’s frame, and for preferences like dark mode and reduced motion.

Two cautions:

  • A container takes no width from what it holds, so give it a width: the Flow pieces fill theirs.
  • A container is also the frame for position: fixed inside it. Put overlays on the body, as the kit’s dialogs, menus and sheets already are.

Type sizes stay in rem, with clamp() and the screen’s width (vw) for anything fluid, so zooming still makes text bigger. Buttons, links and fields have touch-action: manipulation, so a quick double tap on a phone presses rather than zooms.

Details the components take care of

  • hover: applies only on devices that can hover, so a tapped item on a phone doesn’t stay highlighted.
  • Buttons shrink a little when pressed, unless reduced motion is on.
  • Switch themes with switchTheme("dark" | "light" | null) from the registry’s theme lib (@/lib/theme): transitions are held while the colours change, so the page switches rather than fades.
  • Menus grow from what opened them; the context menu appears at once and only fades when it closes.
  • Pictures (Image) and avatars have a hairline just inside their edge, 8% black in light mode and 8% white in dark.
  • The sidebar layout’s first Tab stop is a “Skip to content” link.

Theme tokens

Colours are named for what they’re for, the way most component kits name them, so light and dark themes, and your own palette, change them all at once:

bg-background text-foreground bg-card bg-muted text-muted-foreground bg-primary text-primary-foreground bg-accent border-border ring-ring bg-destructive

Change them in your app’s stylesheet, or with the registry’s palette-editor and theme-picker (Components).

Your own CSS

A .css file in your app is served unchanged. Link it from your layout or your index.html. It swaps in place as you save, without a reload.

Stylesheets a component needs

A component can bring its own stylesheet by rendering a link with a precedence:

chart.tsx
function Chart() {
  return (
    <>
      <link rel="stylesheet" href="/chart.css" precedence="default" />
      <div className="chart">…</div>
    </>
  );
}

The link goes into the page’s <head> once, however many components render it, in the order of its precedence. Content never shows before its CSS. While the stylesheet is still loading, the update that brought the component in waits and the screen keeps what it showed. This covers a navigation, a startTransition, a state change from an effect or a request, and the page’s first render. Inside a <Suspense>, a change made straight from a click shows the fallback until the stylesheet is ready. A link without precedence is an ordinary element and gets none of this.

Motion on scroll

Scroll-in motion needs no script: motion-fade-in, motion-fade-up, motion-scale-in, motion-slide-left, motion-slide-right and motion-blur-in make an element come in as it scrolls into view.

cn and variants

Components join their classes with shadcn’s cn (in lib/utils.ts: clsx and tailwind-merge), so a caller’s className wins over theirs, and give themselves named looks with cva (class-variance-authority), as shadcn’s own do:

card.tsx
import { cn } from "@/lib/utils";

function Card({ className, children }) {
  return <div className={cn("rounded-xl border p-4", className)}>{children}</div>;
}

<Card className="p-8">…</Card>   // p-8 wins over p-4

View as Markdown