What is Tailwind CSS?
Tailwind CSS is a utility-first framework that lets you style elements quickly using pre-built classes, reducing the need for custom CSS.
Why Use Tailwind CSS?
Installation & Setup
Gemfile
:gem "tailwindcss-rails", "~> 4.0"
bundle install
to install the gem.rails tailwindcss:install
This will:
app/assets/tailwind/application.css
for managing styles.Procfile.dev
to run the Tailwind watcher alongside the Rails server..gitignore
to exclude asset builds.rails tailwindcss:watch
How to Use Tailwind CSS in Views
Once Tailwind is installed, you can start applying utility classes directly in your views.
Example: Styling a button
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Save Expense
</button>
Troubleshooting Common Issues
Issue: Styles not applying
Ensure rails tailwindcss:watch
is running in a separate terminal window. If styles still aren’t applying:
Restart the Tailwind watcher:
bin/rails tailwindcss:watch
Refresh the browser.
Issue: "Command Not Found" Errors
rails tailwindcss:install
fails, ensure that tailwindcss-rails
is installed by running:bundle info tailwindcss-rails
Issue: If rails tailwindcss:watch
doesn't update styles, try manually building them:
rails tailwindcss:build
This generates the final CSS file used for styling.