【问题标题】:CSS selector when an item has no class [duplicate]项目没有类时的CSS选择器[重复]
【发布时间】:2019-02-01 05:34:50
【问题描述】:

我正在尝试定位 h 标记的所有实例,但仅当它没有类时。

例如:

<h1 class="class1">First</h1>
<h1 class="class2">Second</h1>
<h1>Third</h1>

在这种情况下,我只想定位第三而不是其他 - 我找到了一个答案 (Is there a CSS selector for element without any class?),它引用了 [class=""] 选择器,但似乎只适用于:

<h1 class="">Third</h1>

遍历并列出所有类的实例然后使用 :not 引用它们是不切实际的,那么还有其他方法吗?

我承认在某些 h1 类的当前 CSS 中使用 !important 等会使情况变得更糟,但我继承了这一点,我没有能力从头开始。

【问题讨论】:

  • :not([class]), [class='']???
  • 如果您在现有的 css 中有 !important 并且无法摆脱它,您别无选择,只能将 !important 也放入您自己的 css 中,并确保您的 css样式表包含在现有样式表之后。
  • 如果你仔细看,那里的答案显示了当根本没有类时它是如何工作的,以及当类属性为空时需要什么。

标签: css


【解决方案1】:
if you use h1: not ([class]) this tag search all. 

如果有很多 !important 你应该在新的 css 中使用它,这个新 css 将它放在所有 .css 的末尾

Example:

<h1 class="class1">First</h1>
<h1>Second</h1>
<h1>Third</h1>

h1:not([class]) { 
  color: red;
}

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 2016-01-13
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    相关资源
    最近更新 更多