04 March 2020
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!
Let’s begin at the beginning.
Open your shell and head to your favorite project directory. Then let’s
rails new the project!
rails new use-tailwindcss cd use-tailwindcss
Back on the command line, use Yarn to install Tailwind CSS.
yarn add tailwindcss
Once yarn finishes installing everything, you need to create a new folder and file:
To the newly created
application.css, you need to add some basic
@import "tailwindcss/base"; @import "tailwindcss/components"; @import "tailwindcss/utilities";
now make use of
tailwind itself to generate a configuration file,
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:
require at the end of the file:
We’re almost done. Tailwind makes use of
postcss, so we need to make some modifications to the
your application root. Open it up and update it to look like the following:
Let’s generate a static home page to verify our updated CSS setup.
rails g controller home index
root Route in
config/routes.rb to use the new static page:
Rails.application.routes.draw do root 'home#index' end
Finally, let’s put some basic Tailwind specific CSS in the new
<nav class="bg-white px-8 pt-2 shadow-md"> <div class="-mb-px flex justify-center"> <a class="no-underline text-teal-dark border-b-2 border-teal-dark uppercase tracking-wide font-bold text-xs py-3 mr-8" href="#"> Home </a> <a class="no-underline text-grey-dark border-b-2 border-transparent uppercase tracking-wide font-bold text-xs py-3 mr-8" href="#"> About </a> <a class="no-underline text-grey-dark border-b-2 border-transparent uppercase tracking-wide font-bold text-xs py-3 mr-8" href="#"> Pricing </a> </div> </nav>
Where to next?