Material Tailwind with CRA

Learn how to setup and install @material-tailwind/react with Create React App.


Creating an CRA Project

First, create a new Create React App project using the command below, it will create a new Create React App project.

For more details check the Create React App Official Documentation.

npx create-react-app my-project
npx create-react-app my-project

Install Tailwind CSS

Once you have created your Create React App project, you need to install and configure Tailwind CSS by running the following command:

npm install -D tailwindcss && npx tailwindcss init
npm install -D tailwindcss && npx tailwindcss init

Configure Your Template Paths

Once you have installed Tailwind CSS, you need to configure your template paths in your tailwind.config.js file.

/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
}
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
}

Add Tailwind CSS Directives to CSS

Next, you need to add the Tailwind CSS directives to your CSS file. Create a ./src/index.css file and add the @tailwind directives for each of Tailwind's layers.

@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind base;
@tailwind components;
@tailwind utilities;

Install Material Tailwind

Once you installed and configured Tailwind CSS, you need to install @material-tailwind/react by running the following command:

npm i @material-tailwind/react@beta
npm i @material-tailwind/react@beta

Add Plugin and Template Path

Once you installed @material-tailwind/react you need to add mtConfig and the template path to your tailwind.config.js file.

const mtConfig = require("@material-tailwind/react").mtConfig;

/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
"./node_modules/@material-tailwind/react/**/*.{js,ts,jsx,tsx}"
],
theme: {
extend: {},
},
plugins: [mtConfig],
}
const mtConfig = require("@material-tailwind/react").mtConfig;

/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
"./node_modules/@material-tailwind/react/**/*.{js,ts,jsx,tsx}"
],
theme: {
extend: {},
},
plugins: [mtConfig],
}

Example

Now you're ready to use Material Tailwind components in your Create React App project. Here's an example of how to use the Button component:

import { Button } from "@material-tailwind/react";

export default function App() {
return <Button>Button</Button>
}
import { Button } from "@material-tailwind/react";

export default function App() {
return <Button>Button</Button>
}