Tailwind CSS not displaying using Create React App

keton

I created a simple test react app and followed all of the steps from the official tailwind website: "https://v2.tailwindcss.com/docs/guides/create-react-app". Here are the relevant files:

SampleComponent.js:

import React from 'react';

const SampleComponent = () => {
  return (
    <div className="bg-blue-500 p-4">
      <h1 className="text-white font-bold">This is a sample component using Tailwind CSS</h1>
    </div>
  );
};

export default SampleComponent;

App.js:

import React from 'react';
import SampleComponent from './SampleComponent';

function App() {
  return (
    <div className="bg-gray-200 min-h-screen">
      <SampleComponent />
    </div>
  );
}

export default App;

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

index.css:

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

craco.config.js:

module.exports = {
    style: {
      postcss: {
        plugins: [
          require('tailwindcss'),
          require('autoprefixer'),
        ],
      },
    },
  }

tailwind.config.js:

module.exports = {
  purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

package.json:

{
  "name": "my-project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@craco/craco": "^7.1.0",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "^5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "autoprefixer": "^9.8.8",
    "postcss": "^8.4.27",
    "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.17"
  }
}

Here is a screenshot of inspecting the element: (https://i.stack.imgur.com/D2wJi.png)

However, every time I try running npm run start, the Tailwind CSS is not being displayed.

I tried other possible solutions I found, such as running npm install react-scripts@latest and npm install postcss@latest as well as deleting npm_modules and package-lock.json and rerunning npm install, but to no avail.

Wongjn

The documentation may be for an older version of react-scripts/create-react-app. Consider removing the style.postcss.plugins key from craco.config.js or indeed removing @craco/craco altogether, since v5 of react-scripts/create-react-app supports Tailwind out of the box.

See your project working in CodeSandbox.

As an aside, Tailwind recommends more modern build systems:

Create React App does not support custom PostCSS configurations and is incompatible with many important tools in the PostCSS ecosystem, like postcss-import.

We highly recommend using Vite, Parcel, Next.js, or Remix instead of Create React App.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use Tailwind CSS in React to configure Create React App?

Create a circular Progress Bar using tailwind css in react

React App adding Pure CSS to Tailwind CSS

Tailwind CSS not loading custom classes on React app

Tailwind CSS does not work with React App

Tailwind CSS does not work with react app - no affect

How to change the primary color of the app from app's UI in React JS(Next JS) using tailwind CSS

Using material ui and Tailwind css in react projects

Using LESS CSS with the latest Create React App

Why does using Tailwind with Create React App using npm-run-all cause an inital blank white screen?

Navbar in React and Tailwind CSS

How can I stabilize floating labels in React using Tailwind CSS?

Fluid transition when displaying a div using tailwind

Tailwind classes not rendering in react app

Tailwind CSS not applying using CLI

Styling Navlink using Tailwind css

Using tailwind css or just vanilla css how can i create a radiating effect on a menubar?

ReactMarkdown is not displaying in React app

Tailwind CSS classes is not working with React, error "You need to enable JavaScript to run this app..."

"SyntaxError with Tailwind CSS in React app: 'font' class does not exist. How to resolve?"

React tailwind, cannot pass tailwind css from parent to child

Tailwind CSS partially working in NextJS app

Tailwind CSS responsive behavior on nextjs app

Title not updating in nextjs, tailwind css app

How to create a div with set size in Tailwind CSS

How to create pseudo border content with Tailwind CSS

Using create-react-app to create a library

Tailwind.css — How to implement the last-child using Tailwind?

React Tailwind - not able to start my app