5

Problem:

Button class being overridden by default tailwind base classes. Not sure why my classes on the element aren't being applied.

Question:

How can I get my styles to apply properly?

Screenshot:

Chrome Tools displaying CSS issue

As you can see background color on .documentCategory__row is being overridden by button, [type=button] on index.scss which is being defined within @tailwind/base.

/* index.scss */
:root {
  --color-primary: #00a3e0;
  --color-secondary: #470a68;
  --color-success: #87d500;
  --color-accent: #e87722;

  /* Dark themes below */
  --color-dark-primary: rgba(31, 41, 55, 1);
  --dark-text: rgba(187, 193, 198, 1);
}

@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

I'm not sure if this has to do with me switching to dart-scss so here is my webpack configuration in case I am missing something

import path from 'path'
import { Configuration as WebpackConfiguration, HotModuleReplacementPlugin } from 'webpack'
import { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
import HtmlWebpackPlugin from 'html-webpack-plugin'
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import ESLintPlugin from 'eslint-webpack-plugin'
import tailwindcss from 'tailwindcss'
import autoprefixer from 'autoprefixer'

const CopyPlugin = require('copy-webpack-plugin');

interface Configuration extends WebpackConfiguration {
  devServer?: WebpackDevServerConfiguration;
}

const config: Configuration = {
  mode: 'development',
  devServer: {
    static: path.join(__dirname, 'build'),
    historyApiFallback: true,
    port: 4000,
    open: true,
    hot: true,
  },
  output: {
    publicPath: '/',
  },
  entry: './src/index.tsx',
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/i,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              '@babel/preset-env',
              '@babel/preset-react',
              '@babel/preset-typescript',
            ],
          },
        },
      },
      {
        test: /\.(sa|sc|c)ss$/i,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader',
          {
            loader: 'postcss-loader', // postcss loader needed for tailwindcss
            options: {
              postcssOptions: {
                ident: 'postcss',
                plugins: [tailwindcss, autoprefixer],
              },
            },
          },
        ],
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        loader: 'file-loader',
        options: {
          outputPath: '../fonts',
        },
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'public/index.html',
    }),
    new HotModuleReplacementPlugin(),
    new CopyPlugin({
      patterns: [
      // relative path is from src
        { from: 'public/images', to: 'images' },
      ],
    }),
    // Add type checking on dev run
    new ForkTsCheckerWebpackPlugin({
      async: false,
    }),

    // Add lint checking on dev run
    new ESLintPlugin({
      extensions: ['js', 'jsx', 'ts', 'tsx'],
    }),
  ],
  devtool: 'inline-source-map',
};

export default config

If there are other files I am missing that are needed let me know!

William Zink
  • 189
  • 4
  • 15

4 Answers4

5

without seeing what your index.tsx looks like I can only make a guess, but here's what caused this issue in our app:

in our index.tsx we were importing index.css after importing our component tree with import App from 'src/App. thus the css was loaded into the site in the wrong order. imports from components first (css modules, normal css imports), tailwind last.

go to your entry file (probably index.tsx) and try moving your import 'index.scss' line above importing the root component.

like this for example

/* index.tsx */
import React from 'react';
import ReactDOM from 'react-dom';

import './index.css'; // this file holds all tailwind styles
import { App } from 'src/App';
// ...

read more here:

https://github.com/tailwindlabs/tailwindcss/discussions/7304#discussioncomment-2256226

1

Even i faced the same issue but I am using Vue3 + element-ui-plus, after spending more than 6 hours my solution is to set :native-type='null':

<el-button type='primary' round @click='handleClick' :native-type='null'>Click Me</el-button>

but this is kinda "hack", this either need to be fixed by Tailwind or by element-ui team. Anyhow, for now enjoy ;)

And the discussion is on here

Syed
  • 15,657
  • 13
  • 120
  • 154
0

I got the same issue using tailwindcss v3 and NextUI, and button's background were "transparent". By adding type = {null}, to button's I solve the issue

DOUHADJI
  • 21
  • 5
0

For Vite users: If you're getting transparent buttons while using vite with tailwindcss and material-tailwind:

  • You need to wrap your tailwindcss config with the WithMT function
  • You need to wrap your entire application with the ThemeProvider coming from @material-tailwind/react

Examples here: https://www.material-tailwind.com/docs/react/guide/vite