Tailwind css with Rails

How to Setup Tailwind css with rails

Installation

First we need to install webpacker, we begin with adding webpacker to our Gemfile

# Gemfile

gem 'webpacker', '~> 3.4'

next we need to run bundle and install webpacker

$ bundle install

$ bundle exec rails webpacker:install

Now we can start on setting up tailwind for our project by running in our terminal

$ yarn add tailwindcss

Next we need to create a folder to contain ?webpacker and tailwindcss files?

$ mkdir app/javascript/css

We need to create a configuration file for tailwind and we run from node_modules tailwind init.

$ ./node_modules/.bin/tailwind init app/javascript/css/tailwind.js

This file `tailwind.js` is where default css configuration for tailwind resides and you have free reign to go in and modify it. Some of the things you can modify are

colors

fonts

textsizes

screensizes

Now we will create our custom css configuration file.

And we paste into that file the example stylesheet from tailwindcss.

Now we need to import our custom css file into webpacks application.js file

Now we need to add javascipt pack tag and stylesheet pack tag into our main layout file.

Next we need to modify a webpacker configuration file .postcssrc.yml which resides in our root project folder. This file is where we define tailwind css and it will allow us to use the @tailwind directive to inject Tailwind's preflight and utilities styles into your CSS. We need to point it to the tailwind.js file that we created earlier and it should look like this.

Tailwind default documentation - https://tailwindcss.com/docs/what-is-tailwind/

Github repo that contains a lot of useful guides and demos for tailwind - https://github.com/merchedhq/awesome-tailwindcss

Last updated