1

I am looking for a solution where I define 1 variable globally and than overwrite it inside a media query - without putting the entire code in it (like LESS CSS set variables in media query?).

I thought something like that(defining):

@media (min-width: 768px) {
     @BWInputHeight: 40px;
}

@media (max-width: 768px) {
    //responsive screens
    @BWInputHeight: 20px;
}

And using it like that:

.dataTables_filter input {
    .form-control;
    max-width: 135px;
    display: inline-block;
    height: @BWInputHeight;
    padding: 1px 6px;
    margin-right: 15px;
}

The problem here, "@BWInputHeight" is a undeclared variable. How can I solve this with LESS ?

Community
  • 1
  • 1
user254197
  • 883
  • 2
  • 9
  • 31
  • 2
    by the way I use dotless – user254197 Jul 01 '14 at 09:23
  • How about using list arrays like [this](http://codepen.io/hari_shanx/pen/GmzCD)? Click on the eye icon in the CSS tab to see the compiled output. Or is this exactly what you didn't want to do? – Harry Jul 01 '14 at 09:30
  • Yes, this it not what I wanted. I need something like media (min-width: 768px) { BWInputHeight: 40px;//here i define only varibles, which will be used depending on the resolution,so I have for example .dataTables_filter input {} only in one place, because if I add some new properties may be I forget to add it also on different media queries } Thank you for helping me :) – user254197 Jul 01 '14 at 09:45
  • I have updated the same pen, check if this is anywhere closer :) – Harry Jul 01 '14 at 09:48
  • oh sorry, I didn't note that. This is less.js. I have never tried with Visual Studio. – Harry Jul 01 '14 at 10:03

1 Answers1

1

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.

Community
  • 1
  • 1
Harry
  • 87,580
  • 25
  • 202
  • 214
  • Thanks for helping again. I use dotless 1.4.0.0 and installed Less.js 1.5.1 over vs nuget manager, but as soon as I excecute this code there is no ".dataTables_filter input" class- so from my point of view it means, there is some problem with rendering that code ?!. What can I do to find that error, may there is something left to configure before using less.js ?! – user254197 Jul 01 '14 at 11:30
  • Sorry mate, I have no clues either. I am trying with less.js 1.5.1 and it seems to work fine. – Harry Jul 01 '14 at 11:34
  • I left out the "@" signs, this is the output from different inspectors from your presvious code: "@BWInputHeight: '200px','40px'; min-width: ~"(min-width: 768px)"; max-width: ~"(max-width: 768px)"; .dataTables_filter input { max-width: 135px; display: inline-block; media @min-width{ height: e(extract(@BWInputHeight,1)) } media @max-width{ height: e(extract(@BWInputHeight,2)) } padding: 1px 6px; margin-right: 15px; }" but the current code does not show any output... – user254197 Jul 01 '14 at 12:03
  • No idea mate. From whatever I read on the net, they should work fine but not sure why it is not :( Will let you know if I find any solution. But leaving out the @ symbol won't help because it is required for variable identification. – Harry Jul 01 '14 at 12:07
  • ok thanks, i left out "@" because stackoverflow gives me an error when I use more than 2 of "@" – user254197 Jul 01 '14 at 12:11
  • Oh ok, I thought you had left them out from the code :D By the way, check if [link 1](http://jordanwallwork.co.uk/2012/08/debugging-less-compilation-errors/) or [link 2](http://tedgustaf.com/blog/2010/11/setup-dotless-in-aspnet-project/) has any helpful information for you. – Harry Jul 01 '14 at 12:28
  • @user254197: Did it get solved? Were those links of any help? – Harry Jul 02 '14 at 09:28
  • actually it did not work by now, I guess because the links are for old versions for example, I added in the web.config " " and I got that error: "Parser Error Message: Sections must only appear once per config file. See the help topic for exceptions." – user254197 Jul 02 '14 at 10:15
  • and the web.config(...mvc\Web.config) looks like "
    "
    – user254197 Jul 02 '14 at 10:17
  • 1
    now I changed to web essentials 2013 and it works :) – user254197 Jul 07 '14 at 13:41
  • 1
    Great. Happy to be of some help :) – Harry Jul 07 '14 at 14:34
  • Hi, again. How shall I change the current code if I only have 2 media queries like "@media (min-width: 768px)" and "@media (max-width: 768px)" and not "@minwidths: '320px','768px','1024px';" only min-width ? Or if I only want to have 320px defined as max-width from "@minwidths: '320px','768px','1024px';" ? – user254197 Jul 09 '14 at 08:31
  • If you have only two then writing them separately could be a bit more easier than a loop. However if you still want have a look at [this](http://codepen.io/hari_shanx/pen/GmzCD). It is a very quickish sample just to show you how it can be done. Please optimize before using. – Harry Jul 09 '14 at 10:19