Anto Subash.

Published on

Add tailwindcss to your ABP Blazor UI

Table of contents

Intro

In this post we will explore how to add TailWind CSS to your ABP Blazor application. We will configure tailwind in a way that it will not conflict with your current bootstrap theme. Tailwind comes with the JIT mode. so you will only use the css need not the whole library. This will result in smaller css file compared to another framework.

Prerequisites

Make sure to install node, npm, .net6 and abp

Create an abp App and Run Migrations

Create the App

Run the following command to create the abp app.

1abp new AbpTailwindBlazor -t app -u blazor --mobile none

Run Migrations

change directory to src/AbpTailwindBlazor.DbMigrator and run the migration project

1dotnet run

This will create the migrations and seed the data for your project.

Install Tailwind

Navigate to src/AbpTailwindBlazor.Blazor project and then init npm.

Init Npm

To init npm run the following command.

1npm init --yes

This will create the package.json file with the default values.

Install npm packages

Lets add the required packages for the tailwind css.

1npm install -D tailwindcss postcss autoprefixer

Initialize Tailwind

To initialize tailwind run the following command.

1npx tailwindcss init --postcss

This will create tailwind config file tailwind.config.js and postcss config file postcss.config.js

Update the tailwind config

1module.exports = {
2  content: [
3    '!**/{bin,obj,node_modules}/**',
4    '**/*.{razor,html}',
5  ],
6  theme: {
7    extend: {},
8  },
9  corePlugins: {
10    preflight: false,
11  },
12  prefix: 'tw-',
13  plugins: [],
14}

We have make 3 changes in the default config.

  • We have updated the content to look for razor and html files
  • We have remove the preflight
  • We have add prefix to all the tailwind css

Create Tailwind css files

We need to create 2 css files

  • /Style/tailwind.css // This is the input
  • /wwwroot/tailwind.css // This is the output

Update the input file with the imports

1@import "tailwindcss/components";
2@import "tailwindcss/utilities";

Update the package.json

We will update the scripts section of the package.json

1"scripts": {
2"test": "echo \"Error: no test specified\" && exit 1",
3"buildcss:dev": "npx tailwindcss --config tailwind.config.js --postcss postcss.config.js -i ./Style/tailwind.css -o ./wwwroot/tailwind.css",
4"buildcss:release": "npx tailwindcss --config tailwind.config.js --postcss postcss.config.js -i ./Style/tailwind.css -o ./wwwroot/tailwind.css --minify",
5"watch": "npx tailwindcss --config tailwind.config.js --postcss postcss.config.js -i ./Style/tailwind.css -o ./wwwroot/tailwind.css --watch"
6},

We have add the 3 new scripts to the our package.json

  • buildcss:dev this will build the css based on the tailwind config
  • buildcss:release this will build minified css based on the tailwind config
  • watch will watch for the new changes and create update the output file

Update the Index page

The Index page is the index.html file inside the wwwroot folder.

We have add our generated css file to the blazor.

1<link href="tailwind.css" rel="stylesheet"/>

Update csproj

We need to build the css before the project is launched. To do that we will update the csproj and create a build task.

1<Target Name="BuildCSS" BeforeTargets="Compile">
2    <Exec Command="npm run buildcss:dev" Condition=" '$(Configuration)' == 'Debug' " />
3    <Exec Command="npm run buildcss:release" Condition=" '$(Configuration)' == 'Release' " />
4</Target>

This command will run the npm command before the build so that we will have our css complied and ready to be displayed.

Usage

To use the tailwind css you should use the tw- prefix. so to apply a background color use tw-bg-gray-200 you will see the color change.

Repo: https://github.com/antosubash/AbpTailwindBlazor