Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I want to use Tailwinds apply feature in my Gridsome project. But error gets thrown that css classes from tailwind cannot be found.
Steps to reproduce:
1) Create new Gridsome project
2) Install tailwind plugin
Github page
Npm page
3) Create css file in assets folder and import in main.js
import "~/assets/style.css"
4) Create css class in css file
5) Use @apply in css with tailwind component
Nothing happens... css file is properly imported bacause normal css gets applied
Some elements like rounded seem too work but others like bg-blue-500 dont.
–
–
–
From what I can figure out, there seems to be a disconnect with Tailwind's documented behavior and its actual behavior.
As per the documentation on extracting components using @apply
:
..Classes like this should be added directly after the @tailwind components directive to avoid specificity issues.
In practice this results in a main style.css
file which looks like the following:
@tailwind base;
@tailwind components;
// Note: you could also just add a custom CSS file here.
.btn-blue {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
.btn-blue:hover {
@apply bg-blue-700;
@tailwind utilities;
In reality, however, this always throws a build error, and for a totally logical reason. Tailwind injects the utilities
file with classes derived from the tailwind.config.js
file.
CSS is compiled linearly, so bg-blue-700
doesn't exist for reference via apply until utilities
is imported and read through.
Even though the documentation advises against it, moving component classes after utilities
resolves the compile error:
@tailwind base;
@tailwind components;
@tailwind utilities;
// Note: you could also just add a custom CSS file here.
.btn-blue {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
.btn-blue:hover {
@apply bg-blue-700;
Not ideal, but that's what worked for me.
–
According to the documentation, wrap it in @layers components {} to prevent unwanted behavior
like this:
@tailwind base;
@tailwind components;
@tailwind utilities;
@layers components {
// Note: you could also just add a custom CSS file here.
.btn-blue {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
.btn-blue:hover {
@apply bg-blue-700;
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.