Tailwind CSS - The Basics

An introduction to Tailwind CSS, a utility-first CSS framework that streamlines styling with low-level utility classes.

10 min read
Tailwind CSS - The Basics

Tailwind CSS - The Basics

Tailwind CSS is a utility-first CSS framework that shifts how we write styles. Instead of creating many custom class names and separate CSS files, Tailwind gives you a rich set of low-level utility classes (e.g., p-4, text-center, bg-blue-500) that you compose directly in your markup. This leads to faster iteration, easier theming, and highly predictable results once you get used to the mental model.

Why utility-first?

  • Predictability: Each utility does exactly one thing. There is little surprise about what mt-6 or gap-4 will do.
  • Consistency: Reusing utilities across components keeps spacing, typography, and colors consistent without copying CSS rules.
  • Speed of iteration: You change UI by editing HTML/JSX classes, which is faster than switching between markup and stylesheets.

Getting started (quick)

  1. Install Tailwind with your build setup. For PostCSS-based or modern frameworks the installation is typically:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
  1. Configure content in tailwind.config.js so Tailwind can tree-shake unused utilities (very important for bundle size):
// tailwind.config.js
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: { extend: {} },
  plugins: [],
};
  1. Add the Tailwind directives to your main CSS file:
@tailwind base;
@tailwind components;
@tailwind utilities;

After that, you can apply utilities directly in your templates or components.

Core concepts

  • Utility classes: Small, single-purpose classes like text-sm, px-3, rounded-lg.
  • Responsive modifiers: Prefixes like sm:, md:, lg: let you apply utilities at breakpoints: md:text-lg.
  • State variants: hover:, focus:, active: and others allow state-based styling without custom CSS.
  • Design tokens via theme: Tailwind exposes a centralized theme for colors, spacing, and fonts, which you can extend in tailwind.config.js.

Customizing the theme

Tailwind's power comes from a configurable theme. Rather than overriding styles in many places, you define or extend tokens once:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: { brand: "#1c7ed6" },
      spacing: { 72: "18rem" },
    },
  },
};

Now bg-brand and w-72 become available across your app.

Composition: extracting components

While utility classes reduce the need for CSS, real projects still benefit from composability. Use one of these approaches:

  • @apply: Create semantic component classes in a CSS file with @apply to compose utilities:
.btn-primary {
  @apply bg-brand inline-flex items-center rounded-md px-4 py-2 text-white;
}
  • Component wrappers: In React/Next.js, create small wrapper components that house common utility sets, e.g., a Button component that already includes btn-primary classes.

Both approaches keep markup readable while preserving the benefits of utility-first styling.

Productivity tips and best practices

  • Keep your content paths accurate so unused utilities are purged in production.
  • Favor descriptive component wrappers for complex UIs to avoid very long class lists inline.
  • Use prettier-plugin-tailwindcss to automatically sort and group classes. This plugin reorders classes into a recommended, consistent order — great for collaboration and diffs.
  • Use variants and plugins when you need more advanced utilities (forms, typography, aspect-ratio, etc.). Tailwind Labs and the community offer useful official plugins.

Common gotchas

  • Large bundles: Without proper content configuration, Tailwind adds the whole utility set. Always configure and build for production.
  • Readability: Long class strings can be intimidating. Break them into semantic components or use @apply when appropriate.
  • Specificity: If you mix traditional CSS and Tailwind utilities, remember Tailwind utilities have low specificity. Use component classes or !important sparingly.

Example workflow

  1. Prototype: Build the layout fast with utilities (spacing, grid, flex).
  2. Extract: Identify repeated patterns and extract them to components or @apply classes.
  3. Polish: Customize theme tokens for brand colors, type scale, and spacing.
  4. Optimize: Verify tailwind.config.js content paths and build for production to purge unused styles.

Conclusion

Tailwind CSS changes how you approach styling by leaning into composition and small, reusable utilities. For many teams it speeds development, reduces CSS drift, and improves consistency. The learning curve is mainly a change in how you think about styles — once you adapt, Tailwind becomes a powerful tool for building predictable, maintainable UI quickly.

If you'd like, I can also:

  • Add an example React/Next.js component using these utilities.
  • Add a short code sandbox or Storybook snippet.
  • Configure prettier-plugin-tailwindcss sorting in your project and show prettier commands to check formatting on save.
Work together

Let's build something great together

Have an idea you want to bring to life? Let's shape it into something you're proud to show.