Back to Wasp

CSS Frameworks

web/versioned_docs/version-0.17/project/css-frameworks.md

0.23.02.3 KB
Original Source

import useBaseUrl from '@docusaurus/useBaseUrl';

Tailwind

Wasp works great with Tailwind CSS, a utility-first CSS framework. Currently, Wasp supports Tailwind CSS v3, but we are working on supporting v4 as well. You can use Tailwind CSS in your Wasp project by following the steps below.

Adding Tailwind to your Wasp project

:::caution Make sure to use the .cjs extension for the Tailwind CSS and PostCSS config files, if you name them with a .js extension, Wasp will not detect them. :::

  1. Install Tailwind as a development dependency.
bash
npm install -D [email protected]
  1. Add ./tailwind.config.cjs.
js
const { resolveProjectPath } = require('wasp/dev')

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [resolveProjectPath('./src/**/*.{js,jsx,ts,tsx}')],
  theme: {
    extend: {},
  },
  plugins: [],
}

:::note The resolveProjectPath function

Because Wasp copies the configuration files to the generated project, you must wrap any paths in the content array with the resolveProjectPath function. This function resolves the path to the generated project, so that Tailwind can find your source files.

:::

  1. Add ./postcss.config.cjs.
js
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
  1. Import Tailwind into your CSS file. For example, in a new project you might import Tailwind into Main.css.
css
@tailwind base;
@tailwind components;
@tailwind utilities;

/* ... */
  1. Start using Tailwind 🥳
jsx
// ...

<h1 className="text-3xl font-bold underline">
  Hello world!
</h1>

// ...

Adding Tailwind Plugins

To add Tailwind plugins, install them as npm development dependencies and add them to the plugins list in your tailwind.config.cjs file:

shell
npm install -D @tailwindcss/forms
npm install -D @tailwindcss/typography

and also

js
/** @type {import('tailwindcss').Config} */
module.exports = {
  // ...
  plugins: [
    require('@tailwindcss/forms'),
    require('@tailwindcss/typography'),
  ],
  // ...
}