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
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:::before
and::after
selectors:checked
So you could refactor your code like this:
Unfortunately, the
@apply
directive with thepeer
variant in Tailwind might not work as expected in some cases due to its complexity. However, you can use: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 withJIT
, thepeer
andpeer-checked
variants still work best with inline utility styles – not via your CSS files where you are using@apply
directive.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.