CSS 中的逗号,多个选择器使用相同的 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4013604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comma in CSS, multiple selectors using the same CSS
提问by nothrow
.Resource table.Tbl td
{ /* some css*/ }
.Resource table.Tbl td.num
{ /* some css 2*/ }
.Resource table.Tbl td.num span.icon
{ /* some css 3*/ }
.Resource table.Tbl2 td
{ /* some css*/ }
.Resource table.Tbl2 td.num
{ /* some css 2*/ }
.Resource table.Tbl2 td.num span.icon
{ /* some css 3*/ }
where the CSS for Tbl and Tbl2 should be the same.
Tbl 和 Tbl2 的 CSS 应该相同。
.Resource table.Tbl, table.Tbl2 td { /* some css*/ }
doesn't work.
不起作用。
How can I achieve this, without duplicating whole line?
如何在不复制整行的情况下实现这一目标?
回答by Alin Purcaru
.Resource table.Tbl td, .Resource table.Tbl2 td { /* some css*/ }
You should add the full ancestor path for both rules. Not just where you see differences.
您应该为这两个规则添加完整的祖先路径。不仅仅是你看到差异的地方。
回答by kennytm
.Resource table.Tbl td, .Resource table.Tbl2 td { /* some css */ }
You have to duplicate the stuff before and after table.TblX
, because there's no way to group the ,
operator to have higher precedence than the descendent selector ?
.
您必须在 之前和之后复制内容table.TblX
,因为无法将,
运算符分组以使其具有比后代选择器更高的优先级?
。
回答by alex
You can't (well not on every browser, read on).
你不能(当然不是在每个浏览器上,请继续阅读)。
Each selector is independent, unfortunately.
不幸的是,每个选择器都是独立的。
It is one of CSS's annoying issues.
There is :any()
, which can do what you wish, but browser support is limited.
有:any()
,它可以做你想做的,但浏览器支持是有限的。
You can do it anyway you like and pre-process it with a server side language, so it outputs independent selectors.
您可以随心所欲地执行此操作,并使用服务器端语言对其进行预处理,因此它会输出独立的选择器。
LESSis popular.
LESS很受欢迎。