最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Tailwind purging styles which are being used - Stack Overflow

programmeradmin3浏览0评论

I am transitioning from Bootstrap to Tailwind css and am facing a problem. I am using tailwind classes for background colors in my div tags but they are not being show. Please find the code attached below.

tailwind.confing.js

    export default {
  content: [
    './index.html',
    './src/**/*.{js,jsx,ts,tsx}', // Check the extensions of your React components
  ],
  safelist: [
    'bg-black-500', // Add any background color classes or other classes that you are using
    'bg-red-500',
    'bg-green-500',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

index.css

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

and App.jsx

import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'

function App() {
  const [count, setCount] = useState(0)

  return (
    <>

     <div className="grid grid-cols-3">
     <div className='bg-black-500'>hi 1</div>
      <div className='bg-red-500'>hi</div>
      <div className="bg-green-500">hi</div>
    
     </div>
    </>
  )
}

export default App

postcss.config.cjs

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

I have checked using dev tools to ensure that tailwind is being detected and that background color is being purged.

I am transitioning from Bootstrap to Tailwind css and am facing a problem. I am using tailwind classes for background colors in my div tags but they are not being show. Please find the code attached below.

tailwind.confing.js

    export default {
  content: [
    './index.html',
    './src/**/*.{js,jsx,ts,tsx}', // Check the extensions of your React components
  ],
  safelist: [
    'bg-black-500', // Add any background color classes or other classes that you are using
    'bg-red-500',
    'bg-green-500',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

index.css

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

and App.jsx

import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'

function App() {
  const [count, setCount] = useState(0)

  return (
    <>

     <div className="grid grid-cols-3">
     <div className='bg-black-500'>hi 1</div>
      <div className='bg-red-500'>hi</div>
      <div className="bg-green-500">hi</div>
    
     </div>
    </>
  )
}

export default App

postcss.config.cjs

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

I have checked using dev tools to ensure that tailwind is being detected and that background color is being purged.

Share Improve this question edited Feb 17 at 18:22 rozsazoltan 8,2555 gold badges18 silver badges38 bronze badges asked Feb 16 at 10:01 Divyansh KumarDivyansh Kumar 232 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The bg-black-500 class does not exist by default, but can found bg-black. This could cause issues during the compilation.

Either add a custom color, or review the available default color palette.

  • Default Color Palette: slate, gray, zinc, neutral, stone, ... - TailwindCSS v3 Docs
  • Adding additional colors - TailwindCSS v3 Docs

You can only add existing classes to the safelist; otherwise, it cannot "retain" non-existing class names, as they never existed in the first place.

export default {
  content: [
    './index.html',
    './src/**/*.{js,jsx,ts,tsx}',
  ],
  safelist: [
    'bg-black-500',
    'bg-red-500',
    'bg-green-500',
  ],
  theme: {
    extend: {
      colors: {
        black: {
          400: '#444',
          500: '#555',
          // etc.
        },
      },
    },
  },
  plugins: [],
}
发布评论

评论列表(0)

  1. 暂无评论