How It Works: Tailwind CSS (Utility‑First Styling)
What it is
Tailwind CSS is a utility‑first CSS framework: tiny, composable classes (e.g., `p-4`, `text-sm`, `bg-gray-100`) that you combine in markup to style components without switching to separate CSS files.
Quick start (SvelteKit example)
npm create svelte@latest myapp
cd myapp && npm i -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
# tailwind.config.cjs
module.exports = { content: ['./src/**/*.{svelte,html,js,ts}'], theme: { extend: {} }, plugins: [] }
# src/app.css
@tailwind base;
@tailwind components;
@tailwind utilities;
How it works
- Tailwind scans templates for class names and generates a CSS file with only the utilities you used (JIT).
- Utilities map to design tokens (spacing, colors, typography) you customize in `tailwind.config.*`.
- Extract repeated patterns into components or use `@apply` when class strings get long.
Advantages
- **Speed & focus**: tweak in place, fewer context switches.
- **Consistency**: normalized spacing/typography; fewer one‑offs.
- **Small prod CSS**: JIT purge keeps bundles lean.
- **Design tokens**: theming and dark mode are straightforward.
Disadvantages
- **Class soup** if you never extract components.
- **Learning curve** for utility/variant syntax.
- **Hard cases still need CSS** (complex animations/layout).
- **Design drift** if tokens aren’t curated.
Competitors / Alternatives
- **Bootstrap** (component kit; opinionated look; heavier overrides).
- **CSS‑in‑JS (styled‑components/Emotion)** (collocated, dynamic; runtime cost unless compiled).
- **CSS Modules** (scoped, simple; less ergonomic for design systems).
- **UnoCSS/Windi** (similar utility‑first engines).
Best practices
- Build a small `ui/` library (Button, Card, Input).
- Lock tokens (spacing/colors/fonts); lint in PRs.
- Use `md:`, `hover:`, `dark:` variants; minimize global CSS.
- Prefer tokens over arbitrary values like `[12.5px]`.
Taylor Swift
“Look what you made me do.”
Comments
Post a Comment