60

I'm trying to use Tailwindcss @apply directive in a <style> tag of a Nuxt.js Vue file. Everything works fine, but I keep getting some annoying red squiggly lines. Please, guys, I need help... Thank you!

Below is a screenshot and a snippet:

enter image description here

<style scoped>

.title {
  @apply text-orient font-light block text-5xl pt-2;
}

.message {
  @apply font-light pb-4 text-orient text-2xl text-blue-bayoux
}
</style>
kissu
  • 40,416
  • 14
  • 65
  • 133
Kingsley F.D.
  • 611
  • 1
  • 5
  • 5

8 Answers8

58

There is no built-in way to solve this within VS Code. The recommended way to solve this is by making use of the Stylelint extension to handle your CSS linting (& SCSS and/or Less, etc.). It's very powerful and likely will improve your stylesheets beyond removing these errors for you.

  1. You need to add the styleint dependencies to your project. Run:
npm install --save-dev stylelint stylelint-config-standard

yarn add -D stylelint stylelint-config-standard
  1. Create a stylelint.config.js in the root of your project. (same location where your package.json is stored)

Place this snippet into it:

module.exports = {
  extends: ["stylelint-config-standard"],
  rules: {
    "at-rule-no-unknown": [
      true,
      {
        ignoreAtRules: [
          "tailwind",
          "apply",
          "variants",
          "responsive",
          "screen",
        ],
      },
    ],
    "declaration-block-trailing-semicolon": null,
    "no-descending-specificity": null,
  },
};

  1. Install these extensions to your VS Code setup:
  1. Last but not least, adjust your local or global VS Code settings.json file to include:
"css.validate": false,
"less.validate": false,
"scss.validate": false,

This way you will have the native linting "disabled", but are still ensuring it is linted using the Tailwind IntelliSense plugin.

Chris
  • 3,311
  • 4
  • 20
  • 34
  • I needed to run `npx stylelint "**/*.css" --fix` afterwards to help fix the highlighted issues. – jessems Dec 04 '20 at 14:44
  • My `settings.json` is this: ``` { "emmet.excludeLanguages": [ "markdown" ], "emmet.includeLanguages": { "erb": "html", "html-eex": "html" }, "editor.fontSize": 13, "workbench.iconTheme": "material-icon-theme", "workbench.colorTheme": "Material Theme Darker High Contrast", "json.schemas": [ ] } ``` Where I put these values? ``` "css.validate": false, "less.validate": false, "scss.validate": false, ```inside `json.schemas`? – rld Feb 18 '21 at 21:12
  • Thanks, this seems to work the best for my React/Prettier/SASS project! – ziale Jul 15 '21 at 10:47
  • If you want to make it work as November 2021, add in your VS Code settings: `"stylelint.validate": ["css", "less", "postcss", "vue"]`. And in stylelint config: `'"overrides": [ { "files": ["**/*.vue"], "customSyntax": "postcss-html" } ],` You also must install `npm install --save-dev postcss-html` – javifm Nov 12 '21 at 09:14
28

Disable Vetur's style validation

Such CSS warnings may originate from the Vetur extension, which may very well be the case if the warning's icon (as seen in VSCode's "Problems" pane) is a Vue logo.

By disabling Vetur's style validation, you may lose other benefits. Although, in the long-run, it's probably better to rely on a more full-featured validator/linter than this extension.

  1. Go to the Vetur extension settings and uncheck the option for style validation.

--- or ---

  1. Set the option per-project, in a .vscode/settings.json file in the project root, with the following:

    "vetur.validation.style": false


Vetur warnings look something like:

vetur is the guilty one here

Kalnode
  • 9,386
  • 3
  • 34
  • 62
kissu
  • 40,416
  • 14
  • 65
  • 133
27

I found another solution Usage of Tailwind with nuxt lead to weird @apply issue #300

Just add lang="postcss" to style tag and with this fix, I haven't any error.

<style lang="postcss" scoped>
  .title {
      @apply text-purple-600 font-bold;
  }
</style>
Ewilan R.
  • 429
  • 8
  • 10
  • I tried your suggestion and it does get rid of the error message but I would still recommend to go with the most upvoted solution and integrate stylelint because it will add the ability to reveal potential errors in your css. I think thats very valuable. – uke5tar Jan 05 '21 at 23:27
20

i think you are using prettier and that plugin get error when you make @apply in one line so try this:

<style scoped>
.title {
  @apply text-orient;
  @apply font-light;
  @apply block;
  @apply text-5xl;
  @apply pt-2;
}

.message {
  @apply font-light;
  @apply pb-4;
  @apply text-orient;
  @apply text-2xl;
  @apply text-blue-bayoux;
}
</style>

Ali Hosseini
  • 879
  • 6
  • 13
  • 3
    Ali Hosseini, I am not using prettier. However, your suggestion worked; but I would also want to know how to avoid having @apply across multiple lines. Thank you! – Kingsley F.D. Apr 27 '20 at 10:39
  • What is your ide?! – Ali Hosseini Apr 27 '20 at 14:38
  • i'm not familiar with svcode and i don't know it at all, but if you mean vscode so right click on the page and select `Format Document With...` and check what is your code formater ( I believe the problem is with your formater) – Ali Hosseini Apr 28 '20 at 06:41
  • 2
    Sorry, I intend to say vscode. However, it is resolved. I had to disable a certain extension (vetur) that was using prettier to format the codebase. All is fine now. Thanks! – Kingsley F.D. Apr 28 '20 at 15:58
  • 2
    thanks, this works specifically in angular project using prettier for linting with tailwind – J King Apr 04 '21 at 13:28
11

2 Steps

  1. Add the lines below into: .vscode > settings.json
"css.validate": false,
"less.validate": false,
"scss.validate": false,
  1. Install StyleLint Extension
https://marketplace.visualstudio.com/items/shinnn.stylelint

Restart, it's done!

char
  • 2,063
  • 3
  • 15
  • 26
Pedro Cardoso
  • 163
  • 2
  • 6
  • 1
    I'm using less, I just added "less.validate": false, to .vscode settings.json file and it worked, thanks! – Tellisense May 20 '21 at 15:38
7

I had the same problem with Nuxt and the solution was to follow the steps in this blog, the TL:DR is:

  1. Install the stylelint vscode extension
  2. Add this configuration in your stylelint.config.js
rules: {
   'at-rule-no-unknown': [
     true,
     {
       ignoreAtRules: [
         'tailwind',
         'apply',
         'variants',
         'responsive',
         'screen',
       ],
     },
   ],
   'declaration-block-trailing-semicolon': null,
   'no-descending-specificity': null,
 },
  1. In the vscode configuration uncheck the option css validate or add this to your vscode settings.json file "css.validate": false

with this steps all will work perfect.

Just in case, I had a problem with the prettier formatting too, and it was fixed with this option in my vscode settings.json file "vetur.format.defaultFormatter.html": "prettier",

Oscar Velandia
  • 1,157
  • 1
  • 7
  • 9
7

Try adding the extension PostCSS Language Support; it adds support for modern and experimental CSS within Visual Studio Code.

Kalnode
  • 9,386
  • 3
  • 34
  • 62
Imane Erguiti
  • 107
  • 1
  • 4
  • 2
    This answer is too short and not specific enough. You could've added at least an explanation and a link to the documentation for the suggested package. – Julian Kleine Jan 22 '21 at 23:47
  • This is more of a comment, than an answer. At 50+ reputation, you may post comments. See [here](https://stackoverflow.com/help/privileges/comment) for details. – costaparas Jan 23 '21 at 03:58
  • 1
    I edited Imane's good answer to clarify what I believe their intent was. – Pier-Luc Gendreau Jan 26 '21 at 15:12
  • This extension did nothing for me... because the warnings I had originated from yet another extension. The general answer is: Find out what extension is reporting the warning, and deal with it. – Kalnode Apr 04 '21 at 14:04
  • Note that you will likely need to select PostCSS as the current language for the document you're working in. This was necessary for me before it worked properly. – DeveloperRyan Jan 05 '22 at 16:05
0

I use VS Code and added the following to my settings:

"files.associations": {
  "*.vue": "html"
}

Afterwards, the error was gone.

mfeyx
  • 57
  • 4