skip to Main Content

I started using Tailwind to style my app. It is very useful but I would like to put all the css in one file. I don’t want to see it with HTML code.

I tried converting the following div:

<div className="relative w-9 h-5 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"></div>

with the following command (that i put in my app.css file):

.checkbox-style {
  @apply relative w-9 h-5 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600;
}

But it doesn’t work. I get error unknown rule at apply. Would you know how to correct it?

I’m using tailwind css 3.4.1 and react 18.2.0.

2

Answers


  1. Applying complex styles with Tailwind CSS @apply directive can be trickier than simply copying and pasting the utility classes from the element to the CSS file. The @apply directive can’t be used with styles that insert:

    • content, like ::before and ::after selectors
    • pseudo-classes such as :checked
    • styles that have complex variants.

    So you could refactor your code like this:

    HTML

    <div className="checkbox-style"></div>
    

    CSS

    .checkbox-style {
      @apply relative w-9 h-5 bg-gray-200 rounded-full dark:bg-gray-700;
    }
    
    .checkbox-style:after {
      @apply absolute top-2 start-2 bg-white border-gray-300 border rounded-full h-4 w-4 transition-all;
    }
    
    .checkbox-style:focus-within {
      @apply outline-none ring-4 ring-blue-300 dark:ring-blue-800;
    }
    
    .checkbox-style:checked {
      background-color: #3182ce; /* blue-600 */
      transform: translateX(100%);
    }
    
    .checkbox-style:checked:after {
      border-color: white;
    }
    

    Unfortunately, the @apply directive with the peer variant in Tailwind might not work as expected in some cases due to its complexity. However, you can use:

    JavaScript or React to handle the state of the checkbox and apply
    styles based on its state

    import React, { useState } from "react";
    import "./App.css";
    
    function App() {
      const [isChecked, setChecked] = useState(false);
      
      const toggleChecked = () => {
        setChecked(!isChecked);
      };
    
      return (
        <div 
          className={`checkbox-style ${isChecked ? "checked" : ""}`}
          onClick={toggleChecked}
        ></div>
      );
    }
    
    export default App;
    

    Mantain them inline, like you are doing.


    Tailwind CSS Just-In-Time (JIT) is an handle on-the-fly arbitrary styles that arise from using JavaScript, thus enabling better handling of pseudo-classes.

    With the peer variants, you specify a state at an element’s level, but you want to apply a style to a "peer" element. However, even with JIT, the peer and peer-checked variants still work best with inline utility styles – not via your CSS files where you are using @apply directive.

    Login or Signup to reply.
  2. In Tailwind CSS, the @apply directive is used within a CSS preprocessor like Post CSS or SCSS to apply existing utility classes to a custom CSS class. However, in your example, it seems like you’re trying to use @apply directly in a regular CSS file, which won’t work because regular CSS does not support this directive.

    To achieve what you want, you have a few options:

    Inline Styles: You can directly apply Tailwind CSS utility classes within the JSX code of your React components. This keeps all styling within your JavaScript/JSX files and eliminates the need for a separate CSS file.

    Using Tailwind’s JIT mode: Tailwind CSS introduced a Just-In-Time (JIT) mode, which allows you to use arbitrary CSS classes directly in your HTML or JSX without the need for a separate CSS file. This mode dynamically generates the necessary CSS based on the classes you use in your project.

    To enable JIT mode, you need to configure your Tailwind CSS setup accordingly. Please refer to the Tailwind CSS documentation for instructions on how to set up JIT mode.

    CSS-in-JS Libraries: If you prefer to keep your styles in separate CSS files but still want the convenience of using JavaScript to style your components, you can consider using CSS-in-JS libraries like Styled Components or Emotion. These libraries allow you to write CSS directly within your JavaScript files using template literals.

    Choose the option that best fits your project’s requirements and preferences.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search