I have a few Less utilities that I've used as extends - here's a typical scenario.
.clearfix
{
&:before,
&:after {
content:"";
display:table;
}
&:after {
clear:both;
}
}
However, I now find myself using media queries and the extend doesn't, well, extend, to all these scenarios.
What I would like to do is to declare the code once and reuse it. I came up with this pattern which works and allows me to use the utility inside media queries, or wherever it suits.
The question is I am doing it wrong, and the extend should work inside a media query, or is there a better of tackling this.
.clearfix
{
@clearfix();
}
@clearfix :
{
&:before,
&:after {
content:"";
display:table;
}
&:after {
clear:both;
}
};
Thanks in advance.
Extend
inside media queries can extend only styles defined in the same media query block. The reason is obvious: since the extending selector is appended to the list of extended style selectors,extend
of the globally defined style by a media query limited selector would mean leaking of that media specific selector to the global scope thus violating the purpose of the media query block.In other words, if you have something like:
And want to get the following CSS:
You'll need to specify your intentions explicitly by moving the extending part to the global scope as well, e.g.:
Or alternatively:
@seven-phases-max does answer your question about the extend in media queries, but does not answer your question about the used solution.
I think that your are using a detached ruleset where you should use a mixin. You could wonder why you should prefer a mixin? I think using a mixin will make your code more reusable (and extendable).
When i import the following code in my project:
When i want to set
p4:3
for every@set()
call i need to define due to the last declaration wins rule for variables:The above repeat the first three properties (which possible can change in future).
Doing the same with mixins, from my library:
In my project which imports the above mixins i will have to define only:
But when using media queries, detached ruleset indeed can help you to make your code more DRY (define your media queries once on a single place). Example:
The above compiles into CSS as follows: