Comma in CSS, multiple selectors using the same CS

2019-01-15 05:36发布

问题:

.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.

.Resource table.Tbl, table.Tbl2 td { /* some css*/ }

doesn't work.

How can I achieve this, without duplicating whole line?

回答1:

.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.



回答2:

.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  .



回答3:

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.

You can do it anyway you like and pre-process it with a server side language, so it outputs independent selectors.

LESS is popular.