css - 如何使用 SASS 来模拟 :matches(a, b) + :matches(a, b) :nth-child(2)? 的使用

标签 css sass css-selectors

在回答“CSS/SCSS Adjacent sibling selector between certain types”这个问题时,如果父元素的第二个子元素和前面的兄弟元素是各种类型的元素,OP 希望对它进行样式化。

我提出了一个answer使用 matches() 选择器(尽管 Firefox 和 Webkit 浏览器——在撰写本文时——使用 :any() 伪类的供应商前缀实现) .

有点像 Internet Explorer 和 Edge 都无法实现任何版本的选择器的时代传统,无论是 :any() 还是 :matches(),尽管这是一项实验性功能,但我不能为该决定或“失败”负责。

但是,为了兼容性,我想问一下:是否有一种方法可以使用 SASS 来形成适当的选择器来可靠地设置以下样式:

a::before {
  content: 'link';
}
span::before {
  content: 'span';
}
b::before {
  content: 'b';
}
em::before {
  content: 'em';
}
:-webkit-any(a, b, span) + :-webkit-any(a, b, span):nth-child(2) {
  color: #f90;
}
:-moz-any(a, b, span) + :-moz-any(a, b, span):nth-child(2) {
  color: #f90;
}
:matches(a, b, span) + :matches(a, b, span):nth-child(2) {
  color: #f90;
}
<div>
  <h2>The second element child of each of these following &lt;div&gt; elements should be styled</h2>
  <div>
    <span></span>
    <a href="#"></a>
  </div>
  <div>
    <a href="#"></a>
    <span></span>
  </div>
  <div>
    <a href="#"></a>
    <span></span>
    <span></span>
  </div>
</div>
<div>
  <h2>The second element child of each of these following &lt;div&gt; elements should <em>not</em> be styled</h2>
  <div>
    <a href="#"></a>
    <em></em>
  </div>
  <div>
    <em></em>
    <a href="#"></a>
  </div>
</div>

对于 SASS,我希望,或者至少想象,应该从以下形式扩展分组(虽然我不确定,因此这个问题是关于如何 模拟 :matches() 语法或 SASS 语法可能是什么):

:matches(a, b, span) + :matches(a, b, span):nth-child(2) {
  color: #f90;
}

进入显式选择器,例如:

a + a:nth-child(2),
a + b:nth-child(2),
a + span:nth-child(2),
b + a:nth-child(2),
b + b:nth-child(2),
b + span:nth-child(2),
span + a:nth-child(2),
span + b:nth-child(2),
span + span:nth-child(2) {
  color: #f90;
}

a::before {
  content: 'link';
}
span::before {
  content: 'span';
}
b::before {
  content: 'b';
}
em::before {
  content: 'em';
}
a + a:nth-child(2),
a + b:nth-child(2),
a + span:nth-child(2),
b + a:nth-child(2),
b + b:nth-child(2),
b + span:nth-child(2),
span + a:nth-child(2),
span + b:nth-child(2),
span + span:nth-child(2) {
  color: #f90;
}
<div>
  <h2>The second element child of each of these following &lt;div&gt; elements should be styled</h2>
  <div>
    <span></span>
    <a href="#"></a>
  </div>
  <div>
    <a href="#"></a>
    <span></span>
  </div>
  <div>
    <a href="#"></a>
    <span></span>
    <span></span>
  </div>
</div>
<div>
  <h2>The second element child of each of these following &lt;div&gt; elements should <em>not</em> be styled</h2>
  <div>
    <a href="#"></a>
    <em></em>
  </div>
  <div>
    <em></em>
    <a href="#"></a>
  </div>
</div>

引用资料:

最佳答案

这可能是一个解决方案,虽然不如使用 :matches 简洁,但也差不多了:

a, b, span {
    & + a, & + b, & + span {
        &:nth-child(2) {
            color: #f90;
        }
   }
}

另一种方法是显式执行扩展循环:

$tags: a, b, span;
@each $a in $tags {
   @each $b in $tags {
        #{$a} + #{$b}:nth-child(2) {
            color: #f90;
        }
   }
}

(也可以使用 mixin 编写,但这不会使它看起来更好......)

但实际上在这一点上,我会在上游工作以确保有一个类而不是那 3 个标签。

关于css - 如何使用 SASS 来模拟 :matches(a, b) + :matches(a, b) :nth-child(2)? 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41445375/

相关文章:

php - 如何在 Jquery : 48 function calls (96 lines each) for HTML class variable 中管理空间

jquery - Bootstrap - 附近的图像 slider |在下方添加文字

vim - Sass 的语法高亮在 Vim 中不起作用

css - 在 CSS 中,是否可以选择换行前的最后一个元素?

css - 如何在未知的多级<ul>中选择最后一个<li>

jquery - 如何让css影响jquery加载的新内容?

jquery - 单击显示/隐藏按钮,从左到右切换幻灯片,平滑移动

Django + Sass + Compressor : loading cached css files, 无法更新或删除

vue.js - 编译Vue项目时的冲突顺序

css - 为复选框定义 css