微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

选择器超出选择器最大特异性

如何解决选择器超出选择器最大特异性

这是我拥有的 CSS:

.tab {
  border: 0;
  flex: 1 1 0;
  min-height: 48px;
  opacity: 1;
  text-align: center;
  z-index: 0;
}
.tab:not([data-selected]) {
  border-radius: 0;
}
.tab[data-selected] {
  background-color: $white;
  border: 1px solid #eee;
  z-index: 2;
}
.tab[data-selected]:first-of-type {
  margin-right: -8px;
}
.tab[data-selected]:last-of-type {
  margin-left: -8px;
}
.tab[data-selected]:not(:first-of-type):not(:last-of-type) {
  margin-left: -8px;
  margin-right: -8px;
}
.tab:first-of-type {
  border-bottom-left-radius: 12px;
  border-top-left-radius: 12px;
}
.tab:last-of-type {
  border-bottom-right-radius: 12px;
  border-top-right-radius: 12px;
}

这里有点问题:

.tab[data-selected]:not(:first-of-type):not(:last-of-type) {
  margin-left: -8px;
  margin-right: -8px;
}

我正在尝试选择中间孩子。我的 stylelint 是这样抱怨的:

Expected "`.tab[data-selected]:not(:first-of-type):not(:last-of-type)`" to have a specificity no more than "0,3,2"

我该如何更好地解决这个问题?

解决方法

选择器的总体特异性

.tab[data-selected]:not(:first-of-type):not(:last-of-type)

是 (0,4,0),即超过 (0,3,2) 的 1。

您可以增加 selector-max-specificity 以适应此选择器,因为差异很小。

或者,如果您不想这样做,您可以像这样重构您的 &[data-selected] CSS 规则。默认情况下应用两个负边距,然后有选择地从 &:first-of-type&:last-of-type 中删除负边距,从而消除双重否定的需要:

    &[data-selected] {
        background-color: $white;
        margin-left: -8px;
        margin-right: -8px;
        border: 1px solid #eee;
        z-index: 2;

        &:first-of-type {
            margin-left: 0; /* Preserve negative margin-right */
        }

        &:last-of-type {
            margin-right: 0; /* Preserve negative margin-left */
        }
    }

请注意,两个嵌套规则中的边距方向交换了,因为我们试图保留现在存在的负边距,而不是将它们添加到以前不存在的位置。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?