Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS CSS set variables in media query?

I'm working on a iPad-specific website. To make my website work on both the retina display iPad and older versions of iPads, I want to set a variable in LESS CSS in media query like:

@media all and (max-width: 768px) {     @ratio: 1; }  @media all and (min-width: 769px) {     @ratio: 2;   } 

So when you are setting anything in pixels, you can do

width: 100px * @ratio; 

But I got a compile error saying @ratio is not defined. Is it possible to have a workaround to make it work? Thanks.

like image 648
Sean Avatar asked Jun 05 '12 22:06

Sean


People also ask

Can I use CSS variables in media query?

If you're like me, you've probably been stretching CSS variables / custom properties to their limits while building your own design systems. But this "silver bullet" can lead to a nasty roadblock: you can't use them in media query declarations.

Can I use CSS variables in less?

As we know, less function can't work with css variables.

How do I use media query in less?

As you can see, you can have multiple conditions for the same media query. In order to test this, you need to resize your window so that it has a lower width. This can be done by pressing F12 in your browser. If you are using Chrome, a new area should appear, either on the side, bottom, or as a separate window.

How do you use variables in media queries?

Tip: Media Queries are about defining different style rules for different devices (screens, tablets, mobile phones, etc.). You can learn more Media Queries in our Media Queries Chapter. Here, we first declare a new local variable named --fontsize for the . container class.


2 Answers

It would be nice, but this is impossible to do like this.

LESS compiles your media queries to actual media queries, so at compile time there is no indication of which media query will be relevant to the browser.

After compile time you just have CSS not less so you can't have variables anymore.

You can do this instead but it isn't as elegant:

@base_width: 100px;  @media all and (max-width: 768px) {     .something {         width: @base_width;     } }  @media all and (min-width: 769px) {     .something {         width: @base_width * 2;     } } 
like image 108
Paul Avatar answered Sep 20 '22 15:09

Paul


I know I'm late with my answer but someone may find this useful.

You can move your styles to a separate file

// styles.less .foo {   width: 100px * @ratio; } 

And then import the file multiple times after changing variables' values

// main.less @ratio: 1; // initial value  @media all and (max-width: 768px) {   @ratio: 1;   @import (multiple) "styles"; }  @media all and (min-width: 769px) {   @ratio: 2;   @import (multiple) "styles"; } 

Note that (multiple) is important here

Compiled code will look like this

// main.css @media all and (max-width: 768px) {   .foo {     width: 100px;   } } @media all and (min-width: 769px) {   .foo {     width: 200px;   } } 
like image 29
Peter Dvukhrechensky Avatar answered Sep 17 '22 15:09

Peter Dvukhrechensky