CSS:声明分组

标签 css css-selectors

.box1 等的声明在下面的示例中分组。

也可以对 .box1 a 等做同样的事情吗?

.box1, .box2, .box3 {
   color: red; }

.box1 a {
   text-decoration:none; }

.box2 a {
   text-decoration:none; }

.box3 a {
   text-decoration:none; }

Note: I don't mean box1 a, box2 a, box3 a, is there something simpler than that?

最佳答案

是的,使用:matches

:matches(.box1, .box2, .box3) a { text-decoration: none; }

这是 spec .这是一个 article关于:匹配。这是 MDN page ,将其称为 :any,因为目前它在供应商前缀版本 :-moz-any:-webkit-any 下可用。不过,必须写出这两种类型的内容会破坏节省击键的意义。

如果你正在喝 CSS 预处理器 Kool-Aid,那么在大多数情况下你可以做 .box1, .box2, .box3 { a { text-decoration-none; }

理论上,您也可以使用 XPath(通过 document.evaluate),使用如下表达式

//input[matches(@id, 'left\d*right')]

那是 XPath 2.0,它可能或可能在您浏览器的 document.evaluate 版本中工作。在 XPath 1.0 中,您需要一个丑陋的解决方法:

//*[starts-with(@id, 'left') and ends-with(@id, 'right') and 
  boolean(number(substring-before(substring-after(@id, "left"), "right"))]

关于CSS:声明分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653930/

相关文章:

jquery - Slick vs. Sizzle——CSS 选择器引擎的优缺点

html - 显示/折叠事件中菜单高度的 CSS 转换

css - 哪个 CSS 选择器应该设置 background-image 属性?

javascript - 滚动页面直到页脚碰到页眉

css - 如何在查询移动设备中按 ID 或类设置按钮样式

css - 是否有 CSS 父级选择器?

css - 第 n 个子选择器的 SASS 编译错误

html - 用于选择不包含表格元素的 div 元素的 css 查询

css - 如何在 li 标签的边框中添加边距?

html - "x Of y"样式css,中间分隔符,垂直居中对齐