如何将@tailwind CSS规则添加到css检查器

Ste*_*Lee 17 visual-studio-code tailwind-css

Tailwind@tailwind在规则中添加了css,标记为未知.我怎样才能避免这个错误?

例如 styles.css

@tailwind preflight;

@tailwind utilities;

Fas*_*ani 128

如果您使用 VS Code,您可以使用以下插件:https : //marketplace.visualstudio.com/items? itemName =csstools.postcss

  • 对于像我这样想要在 `.scss` 文件(在我的例子中是 Angular 应用程序)上使用它的人,请将其添加到您的 `settings.json` 中: `"files.associations": { "*.scss": "postcss" }` (67认同)
  • 虽然这消除了我的警告,但它也禁用了 CSS 的所有智能感知。我只能假设我做错了什么或忘记了一个步骤,但很难说当只有 1 个步骤时会忘记哪一步...... (13认同)
  • 似乎不适用于 VueJS、Volar 和 .vue 文件 (12认同)
  • 一个简单的解决方案,只需一次安装,我喜欢这个!谢谢 :) (5认同)
  • 这是船长。一键点击,完成。谢谢! (4认同)

dav*_*ell 93

Visual Studio Code 允许您为 CSS 语言服务定义自定义数据

例如,在您的工作区中,.vscode/settings.json您可以添加:

{
  "css.customData": [".vscode/css_custom_data.json"]
}
Run Code Online (Sandbox Code Playgroud)

然后.vscode/css_custom_data.json添加:

{
  "atDirectives": [
    {
      "name": "@tailwind",
      "description": "Use the @tailwind directive to insert Tailwind’s `base`, `components`, `utilities`, and `screens` styles into your CSS.",
      "references": [
        {
          "name": "Tailwind’s “Functions & Directives” documentation",
          "url": "https://tailwindcss.com/docs/functions-and-directives/#tailwind"
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,您必须重新加载 VS Code 窗口才能获取更改。

这是 的副本.vscode/css_custom_data.json,其中包含所有带有简短用法片段的指令(反过来在 vs 代码中突出显示语法):

{
  "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": "@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"
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是结果的预览:

<code>@screen</code> 指令的预览

唯一缺少的指令是@apply,因为它是在 CSS 属性级别声明的。在CSS语言服务可能不希望atRules在性能水平,并不会拿起这样的指令。

  • 感谢您提供不需要任何项目或包更改的解决方案。恕我直言,这应该是主要答案。❤️ (15认同)
  • 在 __settings.json__ 文件中,添加 `"files.associations": { "*.css": "tailwindcss" }` 或者您可以使用 __File: Associations__ 设置 GUI: 1. 单击 __Add Item__;2. 在 _Item_ 中输入“*.css”,在 _Value_ 中输入“tailwindcss”。 (12认同)
  • &gt; 请注意,您必须重新加载 VS Code 窗口才能使更改生效。~~~ 它立即为我加载(VSCode 1.46.1 Win10)。感谢您提供所有指令列表。 (4认同)
  • 此外,要安装扩展,您还需要按照“推荐的 VS Code 设置”部分中所述设置“files.associations”设置:https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss (4认同)
  • 这是一个部分解决方案,与 /sf/answers/4396084241/ 中建议的安装 PostCSS 语言支持不同。 (2认同)
  • 我真的更喜欢这个解决方案,而不是安装不必要的包。 (2认同)
  • @MagnusBull,当您使用它时,它似乎可以提供智能感知,但是您是否也摆脱了“unknownAtRules”的花体?当我使用“@apply”时,我会弹出文档,但仍然继续出现下划线错误消息。 (2认同)
  • 您可以编辑此回复以包含此信息吗^ (2认同)

Jor*_*cia 51

当前推荐的 Visual Studio Code 设置(更新于 2022 年 12 月 15 日)

官方 Tailwind CSS IntelliSense Visual Studio 代码文档

使用files.associations设置告诉 Visual Studio Code 始终在 Tailwind CSS 模式下打开 .css 文件:

    "files.associations": {
      "*.css": "tailwindcss"
    }
Run Code Online (Sandbox Code Playgroud)

提示:在 Visual Studio Code 中按Ctrl+,打开“设置”;然后,输入“文件关联”

默认情况下,在编辑“字符串”内容(例如在 JSX 属性值中)时,Visual Studio Code 不会触发完成。更新editor.quickSuggestions设置可能会改善您的体验:

    "editor.quickSuggestions": {
      "strings": true
    }
Run Code Online (Sandbox Code Playgroud)

将两者结合起来,您的settings.json文件(如果是新的)将类似于以下内容:

    {
        "files.associations": {
          "*.css": "tailwindcss"
        },
        "editor.quickSuggestions": {
            "strings": true
        }
    }
Run Code Online (Sandbox Code Playgroud)

来源:Tailwind CSS IntelliSense


以前推荐的 Visual Studio Code 设置(已弃用)

Visual Studio Code 具有内置 CSS 验证,在使用 Tailwind CSS 特定语法(例如 @apply)时可能会显示错误。您可以使用css.validate设置禁用此功能:

"css.validate": false
Run Code Online (Sandbox Code Playgroud)

“editor.quickSuggestions”设置建议当时和现在保持不变。将两者结合起来,您的settings.json文件(如果是新的)将类似于以下内容:

{
    "css.validate": false,
    "editor.quickSuggestions": {
        "strings": true
      }
}
Run Code Online (Sandbox Code Playgroud)

  • 这就是答案,不要忽略 CSS&gt; Lint:Unknown At Rules inside VS Code,欢呼 Jorge! (4认同)

Bio*_*unt 25

我是新来的,但解决此问题的最简单答案是按照以下步骤操作:

\n
    \n
  1. 打开导入 Tailwind CSS 的 CSS 文件

    \n
  2. \n
  3. 按 Ctrl + Shift + P 并搜索 \xe2\x80\x9c更改语言模式\xe2\x80\x9d

    \n
  4. \n
  5. 在搜索栏中,输入 \xe2\x80\x9ctailwindcss\xe2\x80\x9d 并选择它。

    \n
  6. \n
  7. 现在,您的 CSS 文件与 Tailwind CSS 而不是常规 css 关联,并且警告应该消失。

    \n
  8. \n
\n

致谢:https://batchnepal.com/topic/fix-unknown-at-rule-warning-in-vscode

\n

这样您就不需要安装任何第三个插件或忽略任何错误。谢谢阅读!

\n


小智 24

这是由vscode的内置列表提供的规则不可知的规则.

为了摆脱它,你需要做以下事情:

1.安装stylelint扩展code --install-extension shinnn.stylelint

2.安装stylelint推荐配置npm install stylelint-config-recommended --save-dev

3.在vscode USER SETTINGS中添加这两行

"css.validate": false, // Disable css built-in lint
"stylelint.enable": true, // Enable sytlelint
Run Code Online (Sandbox Code Playgroud)

4.将这些行粘贴到.stylelintrc项目根目录中调用的文件中,如果它不存在则创建它.有关stylelint配置的更多信息,请访问以下链接:https://stylelint.io/user-guide/

{
  "extends": "stylelint-config-recommended",
  "rules": {
    "at-rule-no-unknown": [ true, {
      "ignoreAtRules": [
        "extends",
        "tailwind"
      ]
    }],
    "block-no-empty": null,
    "unit-whitelist": ["em", "rem", "s"]
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我能够通过仅安装`stylelint` 扩展并禁用内置`css`、`scss` 和`less` lint 并启用stylelint 扩展(没有进一步的步骤)来隐藏警告。 (4认同)
  • 安装非常简单。正是如图所示的那 4 个步骤。 (2认同)

小智 21

根据tailwindcss-intellisense GitHub 存储库:

推荐的 Visual Studio 代码设置

files.associations

使用该files.associations设置告诉 Visual Studio Code 始终.css以 Tailwind CSS 模式打开文件:

"files.associations": {
   "*.css": "tailwindcss"
}
Run Code Online (Sandbox Code Playgroud)

它修复了@tailwind警告,并添加了 Tailwind CSS 指令和智能感知功能。

有关 Tailwind CSS intellisense 扩展的更多信息可以在GitHub 存储库中找到。


小智 16

1.只需转到设置(ctrl + ,)的快捷方式。
2.在搜索栏中搜索 CSS。
3.寻找( CSS> Lint:Unknown At Rules )
4.从下拉选项中选择“忽略”。
就这样

  • 问题是如何添加顺风支持。忽略它并跳过语法意识是一个糟糕的答案。 (4认同)
  • 这是一个很棒而简单的答案!它不需要安装危险的插件来解决简单的问题。谢谢! (4认同)
  • 切换到使用 tailwindcss 进行文件关联是一种更好的方法,因为它实际上解决了这个特定问题,而不是忽略所有警告。 (2认同)

Jig*_*sar 14

只需在settings.json文件中添加三行:

"css.lint.unknownAtRules": "ignore",
"css.validate": true,
"scss.validate": true,
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用……它只会关闭所有智能。啊! (10认同)

agm*_*984 10

社会保障局

如果您将 SASS 与 Tailwind 一起使用,您仍然会在.scss使用这些早期对此问题的答案的文件中看到错误。

要正确 lint SASS,您可以添加到您的 VS Code 设置中:

"scss.validate": false,
Run Code Online (Sandbox Code Playgroud)

按照@hasusuf 的说明操作,但关闭默认的 VS Code 验证器:

添加以下 3 个设置:

"css.validate": false,
"scss.validate": false,
"stylelint.enable": true,
Run Code Online (Sandbox Code Playgroud)


小智 8

.vscode/settings.json通过添加来编辑我的文件"css.validate": false,似乎无需安装外部库即可为我工作。

https://github.com/Microsoft/vscode/issues/14999#issuecomment-258635188


Ace*_*e.C 6

我的建议是安装postCSS语言支持,然后重命名tailwind.css为,tailwind.pcss然后将package.json脚本(或用于顺风的任何构建脚本)中的引用更改为tailwind.pcssfrom,tailwind.css并且一切正常。

@apply规则与postCSS兼容:https : //github.com/tailwindcss/tailwindcss/issues/325

  • PostCSS 语言支持扩展现在也绑定到 .css 文件,因此无需更改扩展名 (5认同)

小智 6

经过多次测试:

PostCSS 和 STYLUS 语法荧光笔删除了警告,但 CSS Intellisence 并不完整。它不显示第一个实用程序类 Tailwind。

我在 Visual Studio Code 中安装了“语言手写笔”插件。

设置 > 用户设置:

"files.associations": {
   "* .css": "stylus"
 },
Run Code Online (Sandbox Code Playgroud)

CSS 智能不完整


Jon*_*jie 6

当我处理 CSS 和 SCSS 时,这对我有用。在您的settings.json文件中,添加/编辑以下代码。

"files.associations": {
    "*.css": "tailwindcss",
    "*.scss": "tailwindcss"
}
Run Code Online (Sandbox Code Playgroud)