DEV Community

Cover image for Learn how to install Blazor (.NET) with Flowbite and Tailwind CSS
Zoltán Szőgyényi for Themesberg

Posted on • Originally published at flowbite.com

Learn how to install Blazor (.NET) with Flowbite and Tailwind CSS

Blazor is an open source .NET framework for building dynamic and interactive frontends for your applications with HTML, C#, and Razor templates. Blazor allows you to compose components directly for your server or for the client-side. This flexibility enables developers to create fullstack web and mobile applications with a single-page UI framework.

Typically, most frontend frameworks use JavaScript under the hood but with Blazor, you can build both frontends and backends with C#. Developers who are well versed in C# can easily build fullstack applications without switching to a different framework.

More companies are adopting Blazor into their development workflows because a developer can write client-side and server-side logic with only C# and .NET. Some examples include GE Aviation, BurnRate, The Postage, and Pernod Ricard.

Blazor provides all the scaffolding, abstractions, tooling and optimizations you need on a project.

Tailwind CSS is a popular and open-source utility first CSS framework that has grown in adoption the past few years with the rise of UI libraries.

Flowbite is another popular and open-source UI component library that is based on Tailwind CSS and can help you build websites even faster where you can leverage elements like dropdowns, modals, navbars, and more.

Requirements

In this guide you will learn how to build a new Blazor Project, and how to integrate Flowbite UI components into your application. We'll use a modal component for this exercise to demonstrate a real use case.

You'll need to install and configure the .NET SDK, Tailwind CSS, Blazor and Flowbite into your application. Ensure you have installed NPM and Node.js on your local environment. Let's get started!

Create a new Blazor project

Start by downloading and installing the .NET SDK. The SDK allows us to develop apps with .NET frameworks. The Blazor website detects which version you'll need for your local environment.

Start by installing the Microsoft package repository that contains the package signing key:

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
Enter fullscreen mode Exit fullscreen mode

If you're running on MacOS or another Linux distribution, visit the Microsoft website to know how to install the SDK on your local environment. .NET can be installed on Windows, MacOS, and Linux.

Install the .NET SDK (software development kit):

sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-7.0
Enter fullscreen mode Exit fullscreen mode

You can also install the .NET SDK via HomeBrew:

brew install --cask dotnet
Enter fullscreen mode Exit fullscreen mode

Open your terminal and run this command to confirm a successful installation:

-$ dotnet
Enter fullscreen mode Exit fullscreen mode

This is the output you should see to confirm that you installed the .NET SDK successfully:

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.
Enter fullscreen mode Exit fullscreen mode

Run this command in your terminal to create a new Blazor project.

dotnet new blazorserver -o BlazorApp --no-https -f net7.0
Enter fullscreen mode Exit fullscreen mode

This command creates your project's scaffold and a directory called BlazorApp.

Next, you should navigate into the BlazorApp directory:

cd BlazorApp
Enter fullscreen mode Exit fullscreen mode

Run this command in your terminal to launch your application and watch for changes:

dotnet watch
Enter fullscreen mode Exit fullscreen mode

Your terminal will show that your app is listening on http://localhost:<port number> and should launch on your web browser. You can also click on the port to run your application.

Congratulations! You have now installed and ran your first Blazor project.

In the next section, we will configure Tailwind CSS with Blazor.

Install Tailwind CSS

There are two ways to install Tailwind in a Blazor Project: by using the Tailwind CLI or PostCSS.

PostCSS helps in transforming tailwindcss to styling that is relevant to your project. It also helps you remove unnecessary styling which helps in reducing the size of your files.

Start by removing the pre-installed stylesheets in the wwwroot/ folder. Keep the CSS folder because that's what we'll use to store our Tailwind input & output CSS files

Next, ensure that you're still in the BlazorApp Directory then run this command in your terminal:

npm install tailwindcss postcss autoprefixer postcss-cli
Enter fullscreen mode Exit fullscreen mode

Create and configure the PostCSS file

Create a postcss.config.js file in the BlazorApp directory or your root directory and add these configurations:

module.exports = {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    }
  }
Enter fullscreen mode Exit fullscreen mode

Run this command in your root directory to generate a Tailwind CSS configuration file:

npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode

You'll now "tell" Tailwind to watch for files containing utility classes classes so that the .NET CLI can watch for changes in your project. For our Blazor project, the files that Tailwind needs to track are .html, .cshtml or Razor files. Add the template paths to the content section in your Tailwind config file:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./**/*.{razor,html,cshtml}"],
  theme: {
    extend: {},
  },
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Next, create an app.css file in the wwwroot/ folder (within the CSS folder that you did not delete earlier) Add these CSS directives to app.css:

@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

Go to your terminal and run the Tailwind CLI to generate the output CSS watch for changes in your project:

npx tailwindcss -i wwwroot/css/app.css -o wwwroot/css/app.min.css --watch
Enter fullscreen mode Exit fullscreen mode

Add a CSS reference to the host file in the Pages directory:

<!DOCTYPE html>
<html lang="en">
<head>
    ...
    <base href="~/" />
    <link href="~/css/app.min.css" rel="stylesheet" />
    </head>
Enter fullscreen mode Exit fullscreen mode

Finally, run dotnet watch to start adding Tailwind classes to your Blazor project.

You have now successfully created:

  • a new Blazor project
  • installed and configured Tailwind CSS with Blazor

Up next, we'll install and configure Flowbite in our Blazor project. We'll also showcase how to use the Flowbite UI components in a Blazor project through a dropdown component demo.

Install Flowbite

Flowbite is an open-source UI component library that is built with Tailwind CSS and vanilla JavaScript. Here's how you can install and configure it by following these steps to make it work with our Blazor project:

First, you need to install Flowbite via NPM:

npm install flowbite
Enter fullscreen mode Exit fullscreen mode

Require Flowbite in the Tailwind configuration file as a plugin:

module.exports = {
  // other options
  plugins: [
    require('flowbite/plugin')
  ],
}
Enter fullscreen mode Exit fullscreen mode

Add the Flowbite source files to the content module to start applying utility classes from the interactive UI components such as the dropdowns, modals, and navbars:

module.exports = {
  content: [
    // other files...
    "./node_modules/flowbite/**/*.js"
  ],
...
}
Enter fullscreen mode Exit fullscreen mode

Add a script tag with this path before the end of the body tag in the host.cshtml page:

    <!-- ... -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.8.1/flowbite.min.js"></script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

You have successfully installed Flowbite and can start using the components to build your Blazor project.

UI components for Blazor

Now that you have successfully installed Blazor.NET, Tailwind CSS and Flowbite, you can start using Flowbite's UI components such as navbars, buttons, and modals in your project.

Copy and paste this dropdown component example into your Pages/Index.razor file:

@page "/"

<button id="dropdownDefaultButton" data-dropdown-toggle="dropdown" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" type="button">Dropdown button <svg class="w-2.5 h-2.5 ml-2.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 4 4 4-4"/>
  </svg>
</button>

<!-- Dropdown menu -->
<div id="dropdown" class="z-10 hidden bg-white divide-y divide-gray-100 rounded-lg shadow w-44 dark:bg-gray-700">
    <ul class="py-2 text-sm text-gray-700 dark:text-gray-200" aria-labelledby="dropdownDefaultButton">
      <li>
        <a href="#" class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Dashboard</a>
      </li>
      <li>
        <a href="#" class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Settings</a>
      </li>
      <li>
        <a href="#" class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Earnings</a>
      </li>
      <li>
        <a href="#" class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">Sign out</a>
      </li>
    </ul>
</div>
Enter fullscreen mode Exit fullscreen mode

Now that you've set up Flowbite with Blazor you can explore the whole collection of UI components from the Flowbite Library or use the Flowbite Blocks collection to start building websites.

WASM integration

This guide does not include WebAssembly (WASM) support but you can still use Flowbite with Blazor WASM by setting up the Flowbite init functions using an interop layer that ensures the DOM rendering before applying the event listeners via the data attributes API.

Help needed: if you have experience with Blazor WASM and would like to contribute to this guide, then you can edit this file on GitHub and create a pull request. Thank you!

Blazor starter project

The open-source community created an open-source Blazor starter project that you can clone and use as a starting point for building websites with Blazor, .NET, Tailwind CSS and Flowbite.

There's also an official Flowbite Blazor UI library that the open-source community is working on which should be the native solution for Blazor and .NET applications. Contributions are more than welcome!

Credits

This guide was first published on the official Flowbite integration guide on how you can install Blazor with Tailwind CSS with the help of the open-source community.

Top comments (1)

Collapse
 
rasheedmozaffar profile image
Rasheed K Mozaffar • Edited

This guide is AWESOME, I was looking for Flowbite blazor on the library's website a couple days ago, I saw everything but Blazor, and now it's finally there!
This means future projects are going to be so much fun to build.
For the WASM integration, it's almost identical to Blazor Server's, the main change being the starting point of each project type, so I created a pull request that demonstrates how the integration is done for Blazor WebAssembly.
Thanks for sharing 🤩🙏