Installation
Setting up Tailwind CSS in a .NET project.
Start by creating a new .NET Blazor project if you don’t have one set up already.
The steps in this guide will work not only for Blazor, but for any .NET Web project including: MVC, Razor Pages and WebForms.
dotnet new blazor --empty -n my-app
The Tailwind integration is composed by 2 packages:
dotnet watch
dotnet publish
dotnet add package Tailwind.Hostingdotnet add package Tailwind.Hosting.Build
In order to support dontet watch
you need to add the following settings to your launch profile.
{ "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5164", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Tailwind.Hosting" } }, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:7207;http://localhost:5164", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Tailwind.Hosting" } } }}
Create a new stylesheet at wwwroot/styles.css
touch wwwroot/styles.css
Add an @import
to wwwroot/styles.css
that imports Tailwind CSS.
@import "tailwindcss";
By default the compiler will generate an wwwroot/app.css
that is already linked in .NET Template, you find it in your Components/App.razor
<!-- For .NET 9+ --><link rel="stylesheet" href="@Assets["app.css"]" /><!-- For .NET 8 --><link rel="stylesheet" href="app.css" />
Start using Tailwind’s utility classes to style your content.
<h1 class="text-3xl font-bold underline"> Hello world!</h1>
At this moment the there's a bug in .NET 9 compiler for windows that prevents wwwroot/app.css
to be rebuild
In order to solve the fingerprinting error, you must include the following lines to your .csproj
<Target Name="CleanUpTailwindStaticCache" BeforeTargets="PrepareForBuild" > <ItemGroup> <Content Remove="$(TailwindOutputCssFile)"/> </ItemGroup></Target>
Build the project and start the application with dotnet watch
or from your IDE start button.
dotnet watch