Css, Change Less Variable with @Media

css, change less variable with @media

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:

  • For the basic idea of LESS media queries: How can I use media queries more efficiently with LESS?
  • For discussion of getting media queries grouped and less repetition of selector code, see: Media Query grouping instead of multiple scattered media queries that match

LESS CSS set variables in media query?

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;
}
}

Changing variable value based on media query

This is impossible, variables are assigned values when the Sass is compiled to CSS.

what you can do is this:

$avatar_size: 200px;

$avatar_tablet: 150px;

$avatar_mobile: 100px;

@media (#{$tablet_size}) {

img {

width: $avatar_tablet;

height: $avatar_tablet;

}

}

@media (#{$mobile_size}) {

img {

width: $avatar_mobile;

height: $avatar_mobile;

}

}

Media queries in less with variables-need global variables

You can sort of achieve this by using list arrays for each property and screen-width (like the below sample):

@BWInputHeight: '20px','40px','60px'; // Height of the button for min-width=320 and min-width=768 respectively
@minwidths: '320px','768px','1024px'; // The widths for which you need the media queries to be created

.loop-column(@index) when (@index > 0) { // Loop to iterate through each value in @minwidths and form the corresponding output
.loop-column(@index - 1);
@width: extract(@minwidths, @index); // extracts width based on array index
@media (min-width: e(@width)){
.dataTables_filter input{
height: e(extract(@BWInputHeight,@index)); // extracts button height for the corresponding screen width
max-width: 135px;
display: inline-block;
padding: 1px 6px;
margin-right: 15px;
}
}
}

.loop-column(length(@minwidths)); // calling the function

Demo in Code-pen - Modify output area width to see difference and click the eye icon in CSS tab to see compiled CSS.

Note: As per this Stack Overflow thread, both dotless and less.js should be 99% compatible and hence I have given this answer. In case this doesn't work for you, I will happily have this answer removed.

Pass Less.js variable into media query

As seven-phases-max stated in the comments above, strict math is implied within @media queries.

LESS - Strict Math

With strict math on, only maths that is inside un-necessary parenthesis will be processed.

Therefore you need to wrap the variables that you want to be evaluated within parenthesis:

@browser-main-and-sidebar: (@main-width + @main-spacer + @sidebar-width + @outer-padding * 2);

..or:

@media (min-width: (@browser-main-and-sidebar)) {
max-width: @browser-main-and-sidebar;
}

Both of which will compile to the desired result of:

@media (min-width: 940px) {
max-width: 940px;
}


@outer-padding: 20px;
@main-width: 600px;
@main-spacer: 10px;
@sidebar-width: 290px;
@browser-main-and-sidebar: @main-width + @main-spacer + @sidebar-width + @outer-padding * 2;

@media (min-width: (@browser-main-and-sidebar)) {
max-width: @browser-main-and-sidebar;
}

CSS pre-processor with a possibility to define variables in a @media query

Let me answer more directly the question:

"Is it possible to achieve this in any other CSS pre-processor, or we
are doomed to override the .myElement within each media query?"

The answer actually resides in the question. Because LESS (and other similar tools) is a "pre-processor," the @media means nothing to it at compilation time, because it is not looking at the media state of the browser for its preprocessing. The @media state is only relevant after the preprocessing, at which point any @someVariable has already been calculated. This is why what you are trying to do cannot work. Your @myVar can only be output as a single value in a CSS style sheet, and that output occurs before the browser state is ever evaluated by @media.

So it does not have to do with the "global" or "local" scope of a media query, but the fact that LESS compiles the code into CSS utilizing the variables, and it is the CSS (not LESS) that pays attention to the @media query information.

For some further discussion on building media queries with LESS in such a way that they are all together and not scattered throughout the code, see this question.

Variables in media query

You forgot to wrap --ratio-alt in var() when assigning it to --ratio, inside your @media query:

:root {
--font-size: 100%;
--ratio: 1.333;
--ratio-alt: 1.68;
--h4: calc(var(--font-size) * var(--ratio));
--h3: calc(var(--h4) * var(--ratio));
--h2: calc(var(--h3) * var(--ratio));
--h1: calc(var(--h2) * var(--ratio));
}

@media (min-width: 40em) {
:root {
--ratio: var(--ratio-alt);
}
}

How can I use media queries more efficiently with LESS?

I think you can nest media queries and LESS (>1.3.0) will 'bubble' them to the root of your stylesheet during compilation.

.sponsors
{
li
{
.thumbnail
{
padding-top:20px;
padding-bottom:15px;

@media (min-width: 768px) {
padding-bottom:0px!important;
}

img
{
display:block;
margin:0 auto;
}
}
}
}


Related Topics



Leave a reply



Submit