Adding Basic Tailwind CSS Support to Rails6
One common task when starting a new Rails application is to integrate with a CSS library or design framework. Because the CSS landscape has enjoyed quite a bit of expansion over the past few years, one of the more recent popular design frameworks is Tailwind CSS.
With that in mind, the focus of this tutorial is to go through the process of adding tailwindcss to your Rails 6 project.
Let's dive in!
Assumptions & Goals
- You are on a system that has Ruby on Rails 6 setup and available to you
- We want to have TailwindCSS available for use with our front end
Getting Started
Open your shell and head to your favorite project directory. Then let's rails new
the project!
{% highlight ruby linenos %}
rails new rails-use-tailwindcss {% endhighlight %}
Once Rails and the bundler finish setting up the new project, let's get it committed to the git
repository that
Rails initializes for you.
{% highlight ruby linenos %}
cd rails-use-tailwindcss git add . git commit -m "initial commit" {% endhighlight %}
NB: I may forget to write / mention it from time to time, but it's usually a good idea to "sanity check" your
system configuration by running bin/rails s
before you do anything else, just to quickly verify your app loads
ok.
add Tailwind CSS via Yarn
To help deal with CSS and JavaScript for your Rails application, a default Rails 6 project comes with 2 front end technologies: Yarn and Webpack:
Yarn is a package manager, which is a tool similar to Bundler for Rails, only we strictly use it for JavaScript packages. While Webpack is a utility that takes all of our JavaScript, CSS, and creates an optimized JavaScript "blob" for us to use. Try not to worry if that wording didn't make too much sense, as you start using them you begin to see where they fit into the overall picture.
Back on the command line, use Yarn to install Tailwind CSS.
{% highlight ruby linenos %}
yarn add tailwindcss {% endhighlight %}
generate a Tailwind configuration file
Once yarn finishes installing everything, you need to create a new folder and file:
{% highlight ruby linenos %}
mkdir app/javascript/css touch app/javascript/css/application.css {% endhighlight %}
To the newly created application.css
, you need to add some basic tailwindcss
imports:
{% highlight ruby linenos %}
@import "tailwindcss/base"; @import "tailwindcss/components"; @import "tailwindcss/utilities"; {% endhighlight %}
now make use of tailwind
itself to generate a configuration file,
{% highlight ruby linenos %}
yarn tailwind init app/javascript/css/tailwind.js {% endhighlight %}
updating the main application layout
With those updates to the CSS, it's time to add a new stylesheet_pack_tag
to the main application layout. Open
app/views/layouts/application.html.erb
, and add a new entry under the original two:
{% highlight ruby linenos %}
<%= stylesheetlinktag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascriptpacktag 'application', 'data-turbolinks-track': 'reload' %> <%= stylesheetpacktag 'application', 'data-turbolinks-track': 'reload' %> {% endhighlight %}
updating the main javascript application pack
In order to ensure the new CSS files we've created make it into the application javascript pack, we need to add a reference to it.
Open up app/javascript/packs/application.js
and add a new require
at the end of the file:
{% highlight ruby linenos %}
require("../css/application.css") {% endhighlight %}
updating the postcss.config.js
We're almost done. Tailwind makes use of Postcss
, so we need to make some modifications to the postcss.config.js
in
your application root. Open it up and update it to look like the following:
{% highlight ruby linenos %}
module.exports = { plugins: [ require('postcss-import'), require('postcss-flexbugs-fixes'), require('tailwindcss')('./app/javascript/css/tailwind.js'), require('autoprefixer'), require('postcss-preset-env')({ autoprefixer: { flexbox: 'no-2009' }, stage: 3 }) ] }; {% endhighlight %}
generate a static home page
Let's generate a static home page to verify our updated CSS setup.
{% highlight ruby linenos %}
rails g controller pages home {% endhighlight %}
Update the root
Route in config/routes.rb
to use the new static page:
{% highlight ruby linenos %}
Rails.application.routes.draw do get '/home' => 'pages#home' root 'pages#home' end {% endhighlight %}
Finally, let's put some basic Tailwind in the new app/views/pages/home.html.erb
view.
{% highlight ruby linenos %}
{% endhighlight %}You can find the code for this project on my github - https://github.com/erikyuzwa/rails-use-bootstrap