Install Tailwind CSS on a Rails Project

This was found in this GoRails guide: https://www.youtube.com/watch?v=L2nyKwrEY8w

Install Tailwind

yarn add tailwindcss

If when running the server tailwind is not working and you see this JS error: Error: PostCSS plugin tailwindcss requires PostCSS 8. it means you will need to run this (according to tailwind docs)

npm uninstall tailwindcss postcss autoprefixer
npm install tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9

Create the stylesheets folder and application.scss file

Create this file and folder:

app/javascript/stylesheets/application.css

Drop in the Tailwind styles

From Tailwind docs: https://tailwindcss.com/docs/installation/#2-add-tailwind-to-your-css

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

Require the stylesheet in app/javascripts/packs/application.js

In app/javascripts/packs/application.js add this line:

Add the stylesheet_pack_tag to the head

This should be present in addition to the stylesheet_link_tag. This will ensure that styles from webpack (inside app/javascript/stylesheets) get compiled and included.

Update postcss.config.js

Found in Tailwind docs here: https://tailwindcss.com/docs/installation/#using-tailwind-with-postcss

Add this line below require('postcss-import')

If it is not below postcss-import, then @apply rules won't work in the CSS files.

Generate the Tailwind config file

From Tailwind docs: https://tailwindcss.com/docs/installation/#3-create-your-tailwind-config-file-optional

This will create the empty tailwind.config.js file in the project root. This is where you will place configs to override Tailwind's defaults.

Create a full version of the Tailwind config so you can reference it

This will be used to reference and copy/paste settings that you want to override inside tailwind.config.js.

To override any config settings, place those overrides inside the theme: { ... } section in tailwind.config.js

Add my custom utilities CSS

Create another file at app/javascript/stylesheets/global/custom-utilities.scss

Reference this file in application.scss, in between tailwindcss/base and tailwindcss/components:

These are my common custom utilities to include in custom-utilities.scss:

Last updated