乐闻世界logo
搜索文章和话题

How to add a tailwind css rule to css checker

7 个月前提问
3 个月前修改
浏览次数51

6个答案

1
2
3
4
5
6

在使用Tailwind CSS时,通常不需要在CSS检查器(如浏览器开发者工具中的样式编辑器)中直接添加自定义规则,因为Tailwind是一个工具类优先的框架,您可以通过在HTML标签中直接添加类名来应用样式。然而,如果您需要在项目中使用Tailwind CSS以外的自定义CSS样式或者需要对现有的Tailwind样式进行调整,通常有几种方法可以做到这一点:

  1. 使用Tailwind CSS的配置文件:这是扩展或自定义Tailwind样式的首选方法。在tailwind.config.js文件中,您可以扩展现有的样式或添加新的自定义样式。例如,如果您想要添加一个新的间距规则,可以在配置文件中这样做:
js
// tailwind.config.js module.exports = { theme: { extend: { spacing: { '72': '18rem', '84': '21rem', '96': '24rem', } } } }

这将添加新的间距类,例如mt-72, mt-84, 和 mt-96,可在项目中直接使用。

  1. 使用@apply指令:在您的CSS文件中,您可以使用Tailwind提供的@apply指令来应用工具类的样式到自定义的CSS类中。这允许您将Tailwind的实用程序类应用到CSS中,然后在HTML中使用这个自定义类。例如:
css
/* custom.css */ .btn-custom { @apply text-white bg-blue-500 hover:bg-blue-700; }

然后,在HTML中,您可以使用<button class="btn-custom">Click me</button>来应用这些样式。

  1. 直接在CSS中编写:如果你更愿意直接操作CSS或需要添加一些Tailwind没有覆盖的复杂样式,你可以在CSS文件中直接添加它们。例如:
css
/* custom.css */ .custom-rule { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }

然后在HTML中用class="custom-rule"来使用这个规则。

  1. 浏览器的CSS检查器:如果您只是想在开发过程中暂时测试一些样式更改,可以使用浏览器的CSS检查器。右键单击您想要调整的元素,选择“检查”(Inspect),然后在元素的样式面板中添加或修改样式。但是,这种方法的变化只存在于您当前的浏览器会话中,刷新页面后更改会丢失,因此,最终您还是需要将更改反映到您的源代码中。

尽管如此,最佳实践是尽量利用Tailwind CSS的配置和类系统来管理您的样式,因为这有助于保持项目的一致性和可维护性。直接在开发者工具中添加或调整样式通常是用于快速调试或实验的临时手段,并不适合长期的代码维护。

2024年6月29日 12:07 回复

If you use Visual Studio Code, you can use the PostCSS Language Support plugin.

Make sure you associate your scss files with PostCSS by adding the following to your settings.json file: "files.associations": { "*.scss": "postcss" }.

2024年6月29日 12:07 回复

2022-05 Update

The official Tailwind CSS IntelliSense extension now extends the built-in CSS language mode to fix these lint warnings, without losing any of VS Code’s default IntelliSense features.

Review the Recommended VS Code Settings section to enable this for all CSS files in your workspace.

I leave my original answer intact below if you don’t want to install an additional extension, but since v0.8.0 it no longer conflicts with the built-in CSS support so that would be my recommended approach.


Old Answer Without Extension

Visual Studio Code allows you to define Custom Data for CSS Language Service.

For example, in your workspace’s .vscode/settings.json you can add:

shell
{ "css.customData": [".vscode/css_custom_data.json"] }

And then in .vscode/css_custom_data.json add:

shell
{ "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" } ] } ] }

Note that you may have to reload the VS Code window for the change to be picked up.

Here is a copy of .vscode/css_custom_data.json, which contains all directives with short usage snippets (which in turn get syntax highlighted in vs code):

shell
{ "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" } ] } ] }

Here's a preview of the result:

preview of @screen directive

The only directive missing is @apply, because it's declared at the CSS property level. The CSS Language Service probably doesn't expect atRules at the property level and won't pick up such directives.

2024年6月29日 12:07 回复

This is the at-rule-no-unknown rule provided by Visual Studio Code's built-in list.

In order to get rid of it you need to do the following:

1. Install the Stylelint extension code --install-extension stylelint.vscode-stylelint

2. Install the Stylelint recommended configuration npm install stylelint-config-recommended --save-dev

3. Add these two lines in your Visual Studio Code USER SETTINGS

shell
"css.validate": false, // Disable default CSS built-in lint "stylelint.enable": true, // Enable Stylelint "scss.validate": false, // Disable SCSS lint (optional if using scss)

4. Paste these lines into a file called .stylelintrc in your project's root directory; create it if it does not exist. More information about Stylelint's configuration follow this link: https://stylelint.io/user-guide/

shell
{ "extends": "stylelint-config-recommended", "rules": { "at-rule-no-unknown": [ true, { "ignoreAtRules": [ "extends", "tailwind" ] }], "block-no-empty": null, "unit-allowed-list": ["em", "rem", "s"] } }
2024年6月29日 12:07 回复

Official Tailwind CSS IntelliSense Visual Studio Code Documentation

Use the files.associations setting to tell Visual Studio Code to always open .css files in Tailwind CSS mode:

shell
"files.associations": { "*.css": "tailwindcss" }

Tip: Press Ctrl + , from Visual Studio Code to open Settings; then, type "Files Associations".

By default Visual Studio Code will not trigger completions when editing "string" content, for example within JSX attribute values. Updating the editor.quickSuggestions setting may improve your experience:

shell
"editor.quickSuggestions": { "strings": true }

Combining both, your settings.json file (if new) will look similar to this:

shell
{ "files.associations": { "*.css": "tailwindcss" }, "editor.quickSuggestions": { "strings": true } }

Source: Tailwind CSS IntelliSense


Visual Studio Code has built-in CSS validation which may display errors when using Tailwind CSS-specific syntax, such as @apply. You can disable this with the css.validate setting:

shell
"css.validate": false

The "editor.quickSuggestions" setting recommendation remains the same then and now. Combining both, your settings.json file (if new) would look similar to this:

shell
{ "css.validate": false, "editor.quickSuggestions": { "strings": true } }
2024年6月29日 12:07 回复

1. Just go to settings (Ctrl + , for a shortcut).

2. Search for CSS in the search bar.

3. look for the "CSS > Lint: Unknown At Rules"

4. Select "ignore" from the dropdown options.

This ignores the warning. If you're OK with it.

2024年6月29日 12:07 回复

你的答案