Installation
Setting up Tailwind CSS in a Phoenix project.
Start by creating a new Phoenix project if you don't have one set up already. You can follow their installation guide to get up and running.
mix phx.new myprojectcd myproject
Add the Tailwind plugin to your dependencies and run mix deps.get
to install it.
defp deps do [ # … {:tailwind, "~> 0.2", runtime: Mix.env() == :dev}, ]end
In your config/config.exs
file you can set which version of Tailwind CSS you want to use and customize your asset paths.
config :tailwind, version: "4.0.0", myproject: [ args: ~w( --input=css/app.css --output=../priv/static/assets/app.css ), cd: Path.expand("../assets", __DIR__) ]
Configure your assets.deploy
alias to build your CSS on deployment.
defp aliases do [ # … "assets.deploy": [ "tailwind myproject --minify", "esbuild myproject --minify", "phx.digest" ] ]end
Add Tailwind to your list of watchers in your ./config/dev.exs
file.
watchers: [ # Start the esbuild watcher by calling Esbuild.install_and_run(:default, args) esbuild: {Esbuild, :install_and_run, [:myproject, ~w(--sourcemap=inline --watch)]}, tailwind: {Tailwind, :install_and_run, [:myproject, ~w(--watch)]}]
Run the install command to download the standalone Tailwind CLI.
mix tailwind.install
Add an @import
to ./assets/css/app.css
that imports Tailwind CSS. Additionally, tell Tailwind CSS where to scan for utilities.
@import "tailwindcss" source("../..");
Remove the CSS import from ./assets/js/app.js
, as Tailwind is now handling this for you.
// Remove this line if you add your own CSS build pipeline (e.g postcss).import "../css/app.css"
Run your build process with mix phx.server
.
mix phx.server
Start using Tailwind’s utility classes to style your content.
<h1 class="text-3xl font-bold underline"> Hello world!</h1>