Html Visual Studio Code CSS 错误“不要使用空规则集”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32464170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:11:21  来源:igfitidea点击:

Visual Studio Code CSS error "Do not use empty rulesets"

htmlcssvisual-studio-codevisual-studio-2017

提问by Wyredain

I have been using Visual Studio Code for HTML and CSS. I get an error that does not impair the website, but I would like to know more about it. When I hover over the styling for an ID in HTML, a box pops up and says. "Do not use empty rulesets".

我一直在将 Visual Studio Code 用于 HTML 和 CSS。我收到一个不会影响网站的错误,但我想了解更多信息。当我将鼠标悬停在 HTML 中 ID 的样式上时,会弹出一个框并显示。“不要使用空规则集”。

Can anyone provide more information on "empty rulesets" and explain why Visual Studio Code (or any other editor) would warn about an empty ruleset?

任何人都可以提供有关“空规则集”的更多信息并解释为什么 Visual Studio Code(或任何其他编辑器)会警告空规则集?

回答by BoltClock

In CSS, a rulesetis one of the basic building blocks that make up a stylesheet:

在 CSS 中,规则集是构成样式表的基本构建块之一:

.example {
    font-size: 1.25rem;
    color: red;
}

An empty ruleset is one that doesn't have any property declarations, just a selector:

空规则集是没有任何属性声明的规则集,只有一个选择器:

#id {
}

As you note, these rules don't have any effect on the rendering of a document, but some browsers will consume themwhen evaluating CSS only to find nothing there. Performance junkies detestany unnecessary overhead of this sort, so it's better to scrub them for the sake of cleanliness. In fact, CSS Lint has a rule specifically against empty rulesets, and Code is simply linting your CSS on-the-fly with the same set of rules.

正如您所注意到的,这些规则对文档的呈现没有任何影响,但是一些浏览器在评估 CSS 时会使用它们,却在那里找不到任何东西。性能迷们讨厌此类任何不必要的开销,因此为了清洁起见,最好擦洗它们。事实上,CSS Lint 有一个专门针对空规则集的规则,而 Code 只是使用相同的规则集即时对您的 CSS 进行 lint。

However, empty rulesets can be useful in working around certain browser bugs such as this one. In such situations, removing the empty ruleset actually has adverse effects on page rendering, so they should be left there, ideally with a comment documenting their purpose.

但是,空规则集在解决某些浏览器错误(例如错误)时非常有用。在这种情况下,删除空规则集实际上会对页面呈现产生不利影响,因此应该将它们保留在那里,最好带有记录其用途的注释。

You can disable linting by setting css.validateto false in your settings.json file. See the documentationfor how to do this.

您可以通过css.validate在 settings.json 文件中设置为 false来禁用 linting 。请参阅文档以了解如何执行此操作。