I want to build some CSS along these lines:
h1,h2,h3,h4,h5,h6 {some rule}
h1 a,h2 a,h3 a,h4 a,h5 a,h6 a {color: inherit;}
h1 span,h2 span,h3 span,h4 span,h5 span,h6 span {another rule;}
It would be useful if I could create a variable like this:
@headings: h1,h2,h3,h4,h5,h6;
and then maybe do something like this:
@{headings} {
& a {color: inherit;}
}
Unfortunately this gives me:
h1, h2, h3, h4, h5, h6 a {
color: inherit;
}
Is what I want possible? This is a simple version of what I want to do but I would also find useful for working with HTML input types and other instances of multiple selectors that often appear together.
#1 Just yet one more solution in addition to @helderdarocha's answer and those given in https://stackoverflow.com/a/23954580/2712740. Maybe be this one could look a bit more clear:
The limitation of this solution is that
h1, h2, h3 ... {}
and.headings
should be defined at the same level. Additionally, it's important to keep in mind that all these styles will output to CSS at the point ofh1, h2, h3 ... {}
definition not at the point of.headings
definitions, so it may break your cascading overrides if you have some).#2 The alt. solution I'm copy-pasting from https://stackoverflow.com/a/23954580/2712740 #3, basicaly it's the same as #1 but w/o its limitations (just having more special scary symbols):
Use a Ruleset
If you define your heading group as a ruleset with a mixin call to set properties with, then you can do this:
I've isolated the whole thing inside
&
just so the.setProps()
can be localized (it would work without it, but it would be setting the.setProps()
globally. Also, the nested& {}
bracketing is not necessary, but I find that it helps show what the "default" for the@headings
is going to be.This can be used sequentially, if desired, like so:
Both will output like so:
If you have these variables and selectors:
You can use this mixin to generate the code you want:
Calling:
will generate: