20

I'm working on a project using Vue.js and Tailwind CSS and I get this error twice for the same line of code

Unknown at rule @apply css(unknownAtRules)

VS Code Screenshote

when using the follwing style

<style scoped>
     #home {
       @apply bg-accent-gradient;
     }
</style>

I found a soultion to add PostCSS Language Support Extensions and the following to my vscode settings

"css.lint.unknownAtRules": "ignore"

I added it but it removed one error only not both.

0

10 Answers 10

18

Make sure you have csstools.postcss extension installed in the VScode before starting debugging the issue as other answers suggested.

Sign up to request clarification or add additional context in comments.

2 Comments

Glad I read your answer first. That's all it was. VSCode didn't know what it was doing. MS should build this language support into VSCode
This should be the "Accepted Answer". Does exactly what is needed.
15

I had the same case, I went to vs code settings, searched for Unknown At Rules and select ignore there.

enter image description here

1 Comment

I've already done that and changed it to ignor fore LESS and SCSS too but it didn't work too
12

1- First Install VS Code plugin stylelint

2- Then disable css and scss validation for the project. (ctrl+shift+p) and search "settings json".

"scss.validate": false
"css.validate": false

3- Create stylelint plugin config file in your project root stylelint.config.js

4- Add the following content to it in order to ignore at rules apply, tailwind,etc:

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

3 Comments

someonne posted it inn the comments and it worked but now the warninings returned and I dont know why
@MuhammadMahmoud Try to restart VS Code
Just adding "scss.validate": false "css.validate": false into settings.json did the trick for me
10

Solution proposed in https://github.com/tailwindlabs/tailwindcss/discussions/5258#discussioncomment-1979394

Copy pasting here to follow StackOverflow Guidelines.

  1. Create .vscode at the root of your project with the two files named
  • settings.json
  • tailwind.json

Step 1

  1. Update the settings.json file
{
  "css.customData": [".vscode/tailwind.json"]
}
  1. Update the tailwind.json file
{
  "version": 1.1,
  "atDirectives": [
    {
      "name": "@tailwind",
      "description": "Use the `@tailwind` directive to insert Tailwind's `base`, `components`, `utilities` and `screens` styles into your CSS.",
      "references": [
        {
          "name": "Tailwind Documentation",
          "url": "https://tailwindcss.com/docs/functions-and-directives#tailwind"
        }
      ]
    },
    {
      "name": "@apply",
      "description": "Use the `@apply` directive to inline any existing utility classes into your own custom CSS. This is useful when you find a common utility pattern in your HTML that you’d like to extract to a new component.",
      "references": [
        {
          "name": "Tailwind Documentation",
          "url": "https://tailwindcss.com/docs/functions-and-directives#apply"
        }
      ]
    },
    {
      "name": "@responsive",
      "description": "You can generate responsive variants of your own classes by wrapping their definitions in the `@responsive` directive:\n```css\n@responsive {\n  .alert {\n    background-color: #E53E3E;\n  }\n}\n```\n",
      "references": [
        {
          "name": "Tailwind Documentation",
          "url": "https://tailwindcss.com/docs/functions-and-directives#responsive"
        }
      ]
    },
    {
      "name": "@screen",
      "description": "The `@screen` directive allows you to create media queries that reference your breakpoints by **name** instead of duplicating their values in your own CSS:\n```css\n@screen sm {\n  /* ... */\n}\n```\n…gets transformed into this:\n```css\n@media (min-width: 640px) {\n  /* ... */\n}\n```\n",
      "references": [
        {
          "name": "Tailwind Documentation",
          "url": "https://tailwindcss.com/docs/functions-and-directives#screen"
        }
      ]
    },
    {
      "name": "@variants",
      "description": "Generate `hover`, `focus`, `active` and other **variants** of your own utilities by wrapping their definitions in the `@variants` directive:\n```css\n@variants hover, focus {\n   .btn-brand {\n    background-color: #3182CE;\n  }\n}\n```\n",
      "references": [
        {
          "name": "Tailwind Documentation",
          "url": "https://tailwindcss.com/docs/functions-and-directives#variants"
        }
      ]
    }
  ]
}

Credits:

  1. Article by Duncan Leung
  2. @SeyyedKhandon

Comments

3

Use <style lang="postcss">. The only problem with this fix is that VS Code's pretty syntax highlighting for CSS won't work on PostCSS code.

1 Comment

This fixed for me in Vue 3 and IntelliJ IDE
2

Adding lang="scss" worked for me:

<style lang="scss">
/* my css with @apply */
<style>

Comments

2

It's like you are using vscode. For me i juste install PostCSS Language Support extension from vscode and it fix my error !

Comments

0

For folks using neovim-lspconfig, the following configuration has worked for me both for svelte and volar. (vue)

lspconfig.volar.setup({
  -- other configuration options...
  settings = {
    css = {
      validate = true,
      lint = {
        unknownAtRules = 'ignore',
      },
    },
  },
})

Got that idea from https://github.com/LazyVim/LazyVim/discussions/520, as the volar lsp might be using a library and pass its configuration to it.

Comments

0

According to this Github issue the above @apply is no longer global. For me replacing it with @reference worked perfectly.

2 Comments

Which "above" do you mean? Something from the question? Something from one of the answers? From which one? Keep in mind that on StackOverflow "above" is ambigous because the order of shown answers is unpredictably configurable, For me e.g., your answer is the one directly beneath the question. I recommend to use the link you get from the "share" benath whatever you refer to. Also please reconsider your decision to post without taking the tour.
The question is old and relates to v3 of Tailwind. The link you shared is related to v4 and while the topic is similar to the question, it's not really the same thing.
0

Although the question itself is interesting as to why VSCode can't be forced to do this, while the code itself runs perfectly with Vue + TailwindCSS, using TailwindCSS features inside styles in various Vue files might be convenient for hobby projects or projects with a few components.

However, this results in more separatelly run for each , which makes things less efficient. Therefore, it's better to consider @adamwathan's suggestions and look for alternatives, avoiding the use of TailwindCSS features inside in Vue and other files.

<template>
...
</template>

<style scoped>
.my-element {
  background-color: var(--bg-red-500);
}
</style>

Since you avoid using it primarily for performance reasons, the issue raised in the question is no longer relevant.

A ton of confusion amongst Tailwind users comes from not realizing that if you are using CSS modules, or blocks in Vue/Svelte/Astro, your CSS pipeline separately for every single one of those blocks.

50 Vue components using means Tailwind runs 50 separate times.


Source: @adamwathan

For the best build performance, don't use Tailwind features in CSS modules or Vue/Svelte/Astro blocks, just rely on CSS variables.

<template>
  <div>
    <h1>Hello world!</h1>
    <p>Lorem ipsum dolor sit amet ...</p>
  </div>
</template>

<style>
  div {
    background-color: var(--color-blue-500);
  }
  h1 {
    font-size: var(--text-3xl);
    line-height: var(--text-3xl--line-height);
    font-weight: var(--font-weight-bold);
    margin-bottom: calc(2 * var(--spacing));
  }
</style>

Source: @adamwathan

Or even better, just use the classes in your markup like you're supposed to

<template>
  <div class="bg-blue-500">
    <h1 class="mb-2 text-3xl font-bold">Hello world!</h1>
    <p>Lorem ipsum dolor sit amet ...</p>
  </div>
</template>

Source: @adamwathan

The same thing happens by the way by just importing multiple CSS files in JS. Don't do that — import them all into one CSS file and load that one file in JS instead.


Source: @adamwathan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.