Learn how to setup and install @material-tailwind/html with Blazor Web App.
First, create a new Blazor Web App project using the .NET CLI:
dotnet new blazor -n BlazorMaterial
cd BlazorMaterial
The Blazor Web App will have the following structure:
BlazorMaterial/
├── Components/
│ ├── App.razor
│ ├── Layout/
│ │ └── MainLayout.razor
│ └── Pages/
│ └── Home.razor
├── wwwroot/
│ ├── css/
│ │ └── input.css
│ └── ...
└── Program.cs
└── ...
ensure that you’re still in the BlazorMaterial Directory then run this command in your terminal:
- Initialize your project as an npm package:
npm init -y
- Install Tailwind CSS and its dependencies:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
- Create a
postcss.config.js
file:
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}
}
- Update your
tailwind.config.js
:
import withMT from "@material-tailwind/html/utils/withMT";
/** @type {import('tailwindcss').Config} */
export default withMT({
content: ["./**/*.{razor,html,cshtml}"],
theme: {
extend: {},
},
plugins: [],
});
- Create a new CSS file at
wwwroot/css/input.css
:
@tailwind base;
@tailwind components;
@tailwind utilities;
Install @material-tailwind/html using your preferred package manager:
npm i @material-tailwind/html
yarn add @material-tailwind/html
pnpm i @material-tailwind/html
Add the ripple effect script to your App.razor
:
<!-- from node_modules -->
<script async src="node_modules/@@material-tailwind/html/scripts/ripple.js"></script>
<!-- from cdn -->
<script async src="https://unpkg.com/@material-tailwind/html@latest/scripts/ripple.js"></script>
Here's an example of using Material Tailwind components in your Blazor components:
<button type="button"
data-ripple-light="true"
class="align-middle select-none font-sans font-bold text-center uppercase transition-all
disabled:opacity-50 disabled:shadow-none disabled:pointer-events-none text-xs py-3
px-6 rounded-lg bg-gray-900 text-white shadow-md shadow-gray-900/10
hover:shadow-lg hover:shadow-gray-900/20 focus:opacity-[0.85]
focus:shadow-none active:opacity-[0.85] active:shadow-none">
Button
</button>
To run your Blazor application with Material Tailwind:
- Start the Tailwind CSS build process:
npx tailwindcss -i ./wwwroot/css/input.css -o ./wwwroot/css/output.css --watch
- Run the Blazor application:
dotnet watch run
Your Blazor Web App with Material Tailwind is now ready to use!