当 CSS 规则在 Chrome 的元素检查器中显示为灰色时,这意味着什么?
我正在使用 Google Chrome 的元素检查器检查网页上的 h2
元素,并且一些似乎已应用的 CSS 规则呈灰色显示。看起来删除线表示规则被覆盖,但是当样式变灰时意味着什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对我来说,当前的答案并没有充分解释这个问题,所以我添加了这个答案,希望对其他人有用。
灰色/变暗的文本可能意味着
继承
注意事项:Chrome 开发工具的“样式”面板将显示一个规则集,因为该集中的一个或多个规则正在应用于当前选定的 DOM 节点。
我想,为了完整起见,开发工具显示了该集中的所有规则,无论它们是否应用。
如果由于继承而将规则应用于当前选定的元素(即规则应用于祖先,并且选定的元素继承了它),chrome 将再次显示整个规则集。
应用于当前选定元素的规则以普通文本显示。
如果该规则集中存在某个规则,但由于它是不可继承的属性(例如背景颜色)而未应用,则该规则将显示为灰色/变暗的文本。
这是一篇给出了很好解释的文章 - (注意:相关项目位于文章底部 - 图 21 - 不幸的是相关部分没有标题) -http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art033
文章摘录
For me the current answers didn't explain the issue fully enough, so I am adding this answer which hopefully might be useful to others.
Greyed/dimmed out text, can mean either
Inheritance
Note: Chrome dev tools "style" panel will display a rule set, because one or more rules from the set are being applied to the currently selected DOM node.
I guess, for the sake of completeness, dev tools shows all the rules from that set, whether they are applied or not.
In the case where a rule is applied to the currently selected element due to inheritance (i.e. the rule was applied to an ancestor, and the selected element inherited it), chrome will again display the entire ruleset.
The rules which are applied to the currently selected element appear in normal text.
If a rule exists in that set but is not applied because it's a non-inheritable property (e.g. background color), it will appear as greyed/dimmed text.
here is an article that give a good explanation - (Note: the relevant item is at the bottom of the article - figure 21 - unfortunately the relevant section doesn't have a heading) -http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art033
Excerpt from the article
迈克尔科尔曼有正确的答案。我只想添加一个简单的图像来配合它。他包含的链接有一个简单的示例: http://commandlinefanatic.com/art033ex4.html
HTML /DOM 看起来像这样...
当您选择 p 元素时,Chrome 中的样式窗格如下所示...
如您所见,p 元素继承自其祖先(div) 。那么为什么样式
background-color: blue
是灰色的呢?因为它是至少具有一种可继承样式的规则集的一部分。可继承的样式是text-indent: 1em
background-color:blue
不可继承,但它是包含text-indent: 1em< 的规则集的一部分/code> 是不可继承的,Chrome 的开发者希望在显示规则集时是完整的。但是,为了区分规则集中可继承的样式和不可继承的样式,不可继承的样式将显示为灰色。
Michael Coleman has the right answer. I just want to add a simple image to go along with it. The link that he included has this simple example: http://commandlinefanatic.com/art033ex4.html
The HTML/DOM looks like this...
The Styles Pane in Chrome looks like this when you select the p element...
As you can see, the p element inherits from its ancestors (the divs). So why is the style
background-color: blue
grayed out? Because it's part of a rule-set that has at least one style that is inheritable. That inheritable style istext-indent: 1em
background-color:blue
is not inheritable but it's part of the rule-set that containstext-indent: 1em
which is inhertiable and the developers of Chrome wanted to be complete when displaying rule-sets. However, to distinguish between styles in the rule-set that are inheritable from styles that are not, the styles that are not inhertable are grayed out.如果您通过检查器添加样式,但该新样式不适用于您选择的元素,也会发生这种情况。通常显示的样式只是所选元素的样式,因此灰色表示您刚刚添加的样式没有选择 DOM 导航器中具有焦点的元素。
This also occurs if you add a style through the inspector, but that new style doesn't apply to the element you have selected. Usually the styles shown are only those for the element selected, so the grey indicates that the style you just added doesn't select the element which has focus in the DOM navigator.
这意味着该规则已被另一个具有更高优先级的规则替换。例如样式表具有:
检查器将显示规则
color:red;
灰显和color:blue;
正常。阅读 CSS 继承,了解哪些规则被继承/具有更高的优先级。
编辑:
混合:灰色的规则是未设置的规则,它们使用应用于所有元素的特殊默认样式表(使元素可渲染的规则,因为所有样式必须有一个值)。
It means the rule has been replaced with another rule with higher priority. For example stylesheets with:
The inspector will show the rule
color:red;
grayed out andcolor:blue;
normally.Read up on CSS inheritance to learn which rules are inherited/have higher priority.
EDIT:
Mixup: the grayed out rules are the unset rules, which use a special default stylesheet that is applied to all elements(the rules that make the element renderable, because all styles have to have a value).
使用 webpack 时,当页面重建后重新加载时,源代码中已更改的任何 css 规则或属性都会变灰。这真的很烦人,迫使我每次都重新加载页面。
When using webpack, any css rule or property that has been changed in the source code is grayed out when the page reloads after a rebuild. This is really annoying and forced me to reload the page every time.
新版本的chrome开发者显示了它继承自哪里。
The new version of chrome developer shows where it is inherited from.
我在这个问题已经有很多正确答案之后很久才回答,因为我遇到了另一种情况,即在 Chome DevTools 中 CSS 代码块变灰且不可编辑:有问题的块包含 U+200B 零宽度空格字符。一旦我找到这些并删除它们,我就可以再次在 Chrome DevTools 中编辑该块。想必这也可能发生在其他非 ASCII 字符上,我还没有试图弄清楚这一点。
I'm answering long after the question already has many correct answers because I encountered a different case of having a block of CSS code greyed out and uneditable in Chome DevTools: The block in question contained U+200B ZERO WIDTH SPACE characters. Once I found those and removed them, I could edit the block in Chrome DevTools again. Presumably this might happen with other non-ascii characters as well, I haven't tried to figure that out.