Since I don't seem to be getting a lot of support for closing this as a duplicate of this question, I'll essentially repeat my answer (with some variety).
Remember @media
is a CSS query, not a LESS query
The @media
query is a CSS capability that allows the currently loaded CSS to respond to the media it is being presented on (typically some type of browser, but could be print, etc.).
LESS is a CSS preprocessor that creates CSS before it is loaded into the browser, and thus before the media is ever being checked (which is done in the CSS itself, after it has been generated by LESS).
So the proper method for LESS is to have the same type of output as straight CSS, which means you have to repeat the .menu
selector in the @media
query so that its value changes for the media type.
Final CSS Output Should Be Something Like
@media only screen and (max-width: 400px) {
.menu {
width: 100px;
}
}
.menu {
width: 300px;
}
There are various ways to generate something like that with LESS. Strictly taking your basic format above, there is this:
@menu-width: 300px; // default size
@media only screen and (max-width: 400px) {
.menu {
width: @menu-width - 200px; /* assuming you want it dynamic to default */
}
}
.menu {
width: @menu-width;
}
But also look at:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…