Hello does anyone know how to do a media query with LessCSS?
@media screen and (max-width: 600px) {
#container{
width: 480px;
}
}
gives me the following error:
! Syntax Error: on line 23: expected one of :: : . # * - [ @media @font-face , { ; got ( after:
@media screen and
Similarly, @font-face and any CSS3 query with an @ conflict with LessCSS's compiler.
Thanks!
All of the answers above are out of date.
Both less.js and lessCss/dotless support @media and @font-face.
They also now support @media with nested rules, e.g.
becomes
Please refer to the less.js documentation and dotless documentation for more information.
from A Call for LESS to Embrace Responsive Design
So yes, you can use media queries right now but you cannot write them in the nested syntax.
Check out the comments on that blog post for some proposed solutions that look like they'll be rolled in less.js sooner than later.
As far as I understand, LessCSS does not support @media, you may find open tickets for that on their github home page.
The only way I see for this to work in your case is this:
It does look like the issue has been fixed in the LessCSS releases since the accepted answer.
If you can use less.js, it works right out of the box. I tried the same example:
and it reproduced it exactly the same in the .css file. There's also an escape functionality in less.js, but it only seems to work on values, so I don't think you could use it for this.
I'm guessing that you're using the original Ruby LessCSS though? If you can't switch to the new one, the only other thing I can think of is using an @include. This might be slightly better than another link tag in the HTML, and if you compress all your CSS files, you might be able to avoid the extra HTTP request.
We use it on our project and never found it to work.
create an external css file of which is not managed by less. The less css file will be able to access the font-face.
So I added a simple @media print { .no-print {display:none;}} into the following referenced .less file:
and it wouldn't work. However, our css guru pointed out that .lesses default is to use media type of screen, so if you change your referenced less link to
Then suddenly the @media print in the less file will get picked up and start working per normal.
Hopefully this helps someone else as confused as I was.