I am trying to reuse a set of classes set inside a media query as mixins to use throughout a website instead of embedding un-semantic class names in html.
Here is an example of my problem.
@media (min-width: 1000px) {
.foo{width:120px;}
.foo-2{width:150px;}
}
@media (max-width: 999px) {
.foo{width:110px;}
.foo-2{width:120px;}
}
.bar{.foo;}
.bar-2{.foo;}
.bar-3{.foo-2;}
the .bar-X will never get any styles applied. I can guess that this is happening because LESS doesn't create .bar-X inside media queries, so nothing will ever be applied.
Is this a bug in LESS, or something I can never achieve? A workaround to this would be ideal.
Mixins are a group of CSS properties that allow you to use properties of one class for another class and includes class name as its properties. In LESS, you can declare a mixin in the same way as CSS style using class or id selector. It can store multiple values and can be reused in the code whenever necessary.
Media queries are used to apply some CSS instructions only when certain conditions are met. The conditions usually refer to the width of the viewport, since that's the main factor that determines how much can be displayed on a website.
Media queries always begin with @media and consist of two parts: The first part, only screen, determines the media type where a rule should apply—in this case, it will only show the rule if we're viewing content on screen; content viewed when printed can easily be different.
The @media rule is used in media queries to apply different styles for different media types/devices. Media queries can be used to check many things, such as: width and height of the viewport. width and height of the device.
Your problem is a common misconception. LESS does not process the @media
query, the browser does after LESS has done its work. LESS can only create the CSS code that the browser is going to read. So the @media
is "meaningless" to LESS, it is just like any other selector (.someClass div table
, etc.), it only processes what the @media
is going to serve to the browser.
So that means you need to put all your code that changes for the @media
in the @media
block. But you also don't want a bunch of repeated code. So instead, create a master mixin to set your @media
code, and then call that mixin from the media queries:
.makeFooGroup(@w1, @w2) {
.foo {width: @w1}
.foo-2{width: @w2}
.bar{.foo}
.bar-2{.foo}
.bar-3{.foo-2}
}
@media (min-width: 1000px) {
.makeFooGroup(120px, 150px);
}
@media (max-width: 999px) {
.makeFooGroup(110px, 120px);
}
Produces this css:
@media (min-width: 1000px) {
.foo {width: 120px;}
.foo-2 {width: 150px;}
.bar {width: 120px;}
.bar-2 {width: 120px;}
.bar-3 {width: 150px;}
}
@media (max-width: 999px) {
.foo {width: 110px;}
.foo-2 {width: 120px;}
.bar {width: 110px;}
.bar-2 {width: 110px;}
.bar-3 {width: 120px;}
}
For some further info I've given on LESS and @media
related to this, see:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With