Fix "Unknown at rule @tailwind" errors in VS Code
This post contains affiliate links. This means at no extra cost to you, I may earn a commission if you purchase through my links. Please see my website disclaimer for more info.
> Click here to jump straight to the solution <
TailwindCSS is something I’ve only just come into recently, while working on an existing project started by someone else. So I’m a complete beginner when it comes to Tailwind.
Initially I couldn’t really see the point of Tailwind. I mean it really just looked to me like writing inline-style rules, but in the class
attribute instead of the style
attribute. But since the project was already using it, I had to at least understand the basics.
Well, after using it for a while, and reading through some of the Tailwind docs, I can now see the benefits, and I’m actually starting to really like it. So when I started bootstrapping a new project a few weeks ago, I thought “Hey, why not use Tailwind?”!
So after bootstrapping my app, and since I’m building the app using Next.JS, I started by installing TailwindCSS following the Tailwind docs for a Next JS installation.
The Problem
After getting Tailwind installed, I checked out a few of the files, and I noticed an issue that had been bothering me in my last project … Visual Studio Code’s IntelliSense wasn’t working for the @tailwind
and @apply
directives in my global styles file! 😡
So while the directives should have looked something like this, with no errors:
@tailwind base;
@tailwind components;
@tailwind utilities;
Visual Studio Code was underlining them with the yellow squiggly line, indicating a problem:
Specifically, Visual Studio Code was throwing the errors “Unknown at rule @tailwind” or “Unknown at rule @apply”, depending on which one(s) I was using.
On the last project, laziness got the better of me and I just ignored it, and it didn’t seem to be causing any issues. But this time, starting a new project, I had to do something about it or I knew it would just drive me nuts! 😒
So after some quick digging, I found a few stackoverflow posts that addressed the issue a few different ways, but this more recent one was the most up to date, and to the point.
However, like the other answers I found, it was missing a crucial step I needed to get it working. So after doing some more digging, and a bit of trial and error, I was finally able to get things working.
Here’s what I did to fix the issue:
Fix VS Code’s IntelliSense for TailwindCSS
-
First off, if you haven’t already, start by installing the TailwindCSS package in your project:
# Using npm npm install tailwindcss # Using Yarn yarn add tailwindcss
Or, for a Next.JS project, like I’m doing:
# Using npm npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p # Using Yarn yarn add -D tailwindcss postcss autoprefixer yarn dlx tailwindcss init -p
-
Next, in your
tailwind.config.css
, which was just created with thetailwindcss init -p
command, add the following to the module.exportscontent
key:content: [ "./app/**/*.{js,ts,jsx,tsx,mdx}", "./pages/**/*.{js,ts,jsx,tsx,mdx}", "./components/**/*.{js,ts,jsx,tsx,mdx}", ],
Or, if you’re using the ‘src’ folder in your project:
content: [ "./src/**/*.{js,ts,jsx,tsx,mdx}", ],
-
Once that’s done, and this was the crucial missing step in the answers I found online:
- Go into your extensions in Visual Studio Code, and
- Install the Tailwind CSS IntelliSense extension.
This is what will allow Visual Studio Code to actually recognize the directives.
-
And then finally, after the TailwindCSS IntelliSense extension is installed, you’ll need to add a file association. To do this:
-
Go into your Visual Studio Code settings:
- On a Mac, you can do this by going to Code > Settings… > Settings, or by using the keyboard shortcut ⌘ + ,.
- On a PC, you can do this by going to File > Preferences > Settings, or by using the keyboard shortcut Ctrl + ,.
-
Then, in the search bar, type in “files associations”, and click on the Add Item button under the ‘Files: Associations’ section.
-
Under the Item column, add “*.css” (without the quotes), and under the Value column, add “tailwindcss” (again, without the quotes). Click the Ok button to save the changes.
-
And that’s all there is to it!
Simple eh? 😁
Now, when you use the @tailwind
and @apply
directives in your CSS files, Visual Studio Code will no longer throw the “Unknown at rule @tailwind” or “Unknown at rule @apply” errors, and you’ll get the IntelliSense you’re used to with CSS!
Notes
If you’ve followed posts online that miss the installation of the TailwindCSS IntelliSense extension, you’ll end up with broken IntelliSense for CSS, and your CSS files will look like regular text files without any highlighting:
So if you’ve followed other instructions online that left you with non-highlighted CSS files and broken IntelliSense, you’re just missing the TailwindCSS IntelliSense extension to fix things up!
Hope this post was able to help if you were in the same boat as me!
Until next time,
michael 😀
Share this post:
Google Apps Script: 2 Caching Methods You Need to Use!
Google Sheets: Use Built-in Functions to Fetch JSON API Data
Using LAMBDAs in Google Sheets to Create Filler Data
In a World with AI, Where Will the Future of Developers Lie?
Odd Behaviour with Alternating Colors in Google Sheets
How to Redirect URLs with Netlify
Is Hosting on Netlify Going to Bankrupt you?
Develop Google Apps Script Code in Your Local Code Editor
Create an Investment Gain Forecaster using Google Sheets
Limit the Number of Pre-rendered Pages in Next.js
Understanding PostgreSQL RLS, and When to Use it
Build a Weather Widget Using Next.js
Filtering Data in Google Spreadsheets
5 Ways to Redirect URLs in Next.JS
Create a Budget Tool with Google Sheets
Fix Path Name Mismatches in Local & Remote Git Repos
Fix "Cannot find module ..." TypeScript errors in VS Code
Build a Simple Contact Form with Next.JS and Netlify
Fix "Hydration failed ..." Errors in React
Updating Turborepo to use Yarn Berry
The Pros and Cons of Using a Monorepo
Git Cheat Sheet
Create an Expense-tracker with Google Sheets - Part 2
Create an Expense tracker with Google Sheets - Part 1
Quick and Dirty Portfolio Tracker Part 2 - Crypto
Quick and Dirty Portfolio Tracker Part 1 - Stocks
Comments