Using Tailwind CLI with Blazor
Last year I wrote an article about using TailwindCSS with Blazor. It was a struggle, since Blazor had to be tricked to install and compile TailwindCSS via PostCSS using NodeJS. Yesterday, TailwindCSS launched a new tool called Tailwind CLI. So itâs time to revisit trying a combination of TailwindCSS with Blazor!
Bootstrapping a new Blazor project
Installing dotnet
First of all, you should install a dotnet
version on your local system. Since I use windows, I use choco
to install my dotnet SDK.
You can also install it from their website.
choco install dotnetcore-sdk -y
Note: I used version dotnet-core 5 for this âprojectâ
Making the project
Starting a new project is easy, just put the follow command in your terminal:
dotnet new blazorserver -o YourAppName
now when you enter the YourAppName
directory, you should be able to start the project in dev mode with dotnet watch run
.
You should be sent to localhost:5001
which is the default for dotnet apps. Here youâll see an example welcome page built with bootstrap.
Switching from Bootstrap to Tailwind
Get rid of Bootstrap
While Bootstrap still is a widely used and good CSS framework, weâll be using TailwindCSS (duh!), so letâs delete all files in the css
directory.
Also delete the <link>
elements refering to them in Pages/_host.cshtml
Tailwind all the way
Now letâs try out Tailwind. First of all we need an executable. The executables can be found on their GitHub repo here. Be sure to grab the exact one for your OS.
Once the file is downloaded, move it to the current project directory.
Now we can make a new config file by running the following command in the project root
./tailwind init
We can now see that a file called tailwind.config.js
is made in the project root! Yay!
Now letâs make a tailwind.css
file in the wwwroot/css
directory with the standard Tailwind imports
@tailwind base;
@tailwind components;
@tailwind utilities;
Letâs compile tailwind into usable CSS
The only thing we need to do now, is build our Tailwind config into browser ready CSS and link it from our HTML.
The TailwindCLI has a command for building and watching the css file. At the moment, we still need to give the entire path to the input and output path, but you can build a seperate script to take care of this ofcourse!
./tailwindcss -i ./wwwroot/css/tailwind.css -o ./wwwroot/css/output.css
If youâre working on the CSS, you can add the --watch
flag to the command above, the CSS wil then recompile on edits.
Note: add your output.css
file to the .gitignore
if youâre using git, so the file wonât get uploaded.
Now letâs add the file to Pages/_host.cshtml
by adding
<link rel="stylesheet" href="css/output.css" />
You can now use TailwindCSS as usual!
Written by Elian Van Cutsem
← Back to blog