Skip to content

Variables not being replaced in media queries declaration #122

Closed

Description

@franciscolourenco
No description provided.

Activity

  1. dylan commented on Jan 19, 2011

    @dylan

    I am having this problem as well.

  2. mishunov commented on Apr 2, 2011

    @mishunov
    Contributor

    This is still the issue to me. Has there been any action on this?
    Both variables and @import rules within @media queries are not converted and are just output as they are not converted at all. Let's asume I have core.less file that has some variables and CSS in it. Then I have another .less file, that dispatches different stylesheets based on @media queries. So to serve core.less to a device I do the following:

    @media only screen and (min-device-width : 769px) {
        @import "core";    
    }
    

    In resulting CSS it turns into

    @media only screen and (min-device-width : 769px) {
    
    }
    

    If I want to work around this and instead of importing core.less will import compiled core.css file like:

    @media only screen and (min-device-width : 769px) {
        @import "core.css";    
    }    
    

    it compiles without any change to:

    @media only screen and (min-device-width : 769px) {
      @import "core.css";
    }
    
  3. vincentbernat commented on May 23, 2011

    @vincentbernat

    I also would like to get variables replaced inside media query. Something like this :

    @media screen and (max-width: @page-width) {
     ...
    }
    

    The CSS generated from this still contains @page-width instead of its value.

  4. maranomynet commented on Aug 12, 2011

    @maranomynet
    Contributor

    +1 for this issue!

  5. davetayls commented on Aug 28, 2011

    @davetayls

    Is this being looked at? I would like to use it in the same way as @vincentbernat

  6. PaulAdamDavis commented on Sep 5, 2011

    @PaulAdamDavis

    +1 on this too. I want to use it the same way as @mishunov

  7. dfreerksen commented on Sep 7, 2011

    @dfreerksen

    The only solutions I have found to this is to do something like this:

    1. Create a file named 768.less
    2. In this file create a parametric mixin with no parameters such as:
      .max-width-768() {
      body {
      background-color: #FF00FF;
      }
      }
    3. In your styles.less (or whatever you are calling it)
      @import "768.less";
      @media (max-width: 768px) {
      .max-width-768();
      }

    The other option I have found is:

    1. Create a file named 768.less
    2. In your styles.less (or whatever you are calling it)
      @import "768.less";
    3. In your 768.less file you do something like:
      @media (max-width: 768px) {
      body {
      background-color: #FF00FF;
      }
      }
  8. jayf commented on Oct 5, 2011

    @jayf

    Likewise, I'd would like to use it in the same way as @vincentbernat, e.g.,

    @media screen and (min-width: @bigass) {

    I see in parser.js where it grabs the media rule, but I'm still too new to the code to see the exact changes needed...

  9. ttfkam commented on Oct 5, 2011

    @ttfkam
    Contributor

    With regard to everything I've seen from the draft CSS 3 specs, @import statements are not allowed within @media directives. This, however, is allowed by the spec:

     @import "foo.css" screen and (min-width: 768px);
    

    I haven't tried this with Less, but if there is going to be compatibility with CSS, perhaps this should be the target instead of nesting import statements. http://www.w3.org/TR/css3-mediaqueries/

  10. maranomynet commented on Oct 6, 2011

    @maranomynet
    Contributor

    @importing LESS files should be possible inside @media blocks IMO, even though CSS @import (linking) is invalid.

    However, the original issue being reported is that LESS currently doesn't allow variables or other such niceties following @media and @import

  11. loa commented on Nov 21, 2011

    @loa

    +1 Need same thing as @vincentbernat

  12. matthew-dean commented on Nov 22, 2011

    @matthew-dean
    Member

    Ah, looks like I stumbled upon the same bug. My statement:

    @import "desktop/nav";

    This imports perfectly well OUTSIDE a media query, but vanishes with no error inside a media query.

  13. razialx commented on Dec 1, 2011

    @razialx

    I have a patch for this I am getting ready to submit. Haven't looked at the importing, just the variable replacement.

  14. razialx commented on Dec 1, 2011

    @razialx

    So, I messed up my pull request apparently. The patch for this is in this request: #481

    Should have been in its own. It is the second commit. Can find the original commit on my branch here:
    razialx@1f97c27

  15. added a commit that references this issue on Dec 2, 2011
  16. 36 remaining items

  17. brgrz commented on Feb 11, 2013

    @brgrz

    My issue is imports inside the media blocks, the same as mishunov and matthewdl. I guess that's a different issue than the variables replacements? but it seems related to this issue as other's talked about it here..or is there another issue opened on that?

  18. brgrz commented on Feb 11, 2013

    @brgrz

    Maybe it will be easier to understand what I'd like with this workaround that I use now:

    `<link href="@Url.Content("~/public/less/styles.978.gs.less")" rel="stylesheet" type="text/css" media="only screen and (min-width: 768px) and (max-width: 1139px)"/>

    `

    I have different grids that are computed off different values for variables and I use those grids for different resolutions. The grids are based on Semantic.gs where I have a couple of mixins like .column(number-of-columns).

  19. kamranayub commented on Feb 11, 2013

    @kamranayub

    This doesn't work already @mhrovatic?

    Because we're doing this in our projects already.

  20. brgrz commented on Feb 11, 2013

    @brgrz

    What exactly are you doing? please post a sample code

  21. kamranayub commented on Feb 11, 2013

    @kamranayub

    We're doing more than you're asking ;)

    @media (max-width: @breakpointLarge) {
      @import "modules/_header";
      @import "modules/_footer";
      ...
    }
    
    @media only screen and (min-width: 700px), only screen and (max-width: 800px) {
     ...
    }

    No issues here using Mindscape.

  22. brgrz commented on Feb 11, 2013

    @brgrz

    Is the "modules/_header" an uncompiled LESS file that includes variable declarations?

    For instance can you do this
    '@import "978grid_vars"; // defines and initializes some variables
    @import "grid"; // uses those variables'

    This doesn't seem to work for me with dotless, says variable is undefined.

  23. lukeapage commented on Feb 12, 2013

    @lukeapage
    Member

    if it is dotless you are using, raise the issue with https://github.com/dotless/dotless - it is a port, it doesn't directly use less.js

  24. lukeapage commented on Feb 16, 2013

    @lukeapage
    Member

    this is implemented in master

  25. adam-lynch commented on Dec 15, 2013

    @adam-lynch

    This is a frustrating one :/. Really limits what I want to do with variables but I need to compile on the fly in the browser too.

    Wanted to do stuff like:

    @media(min-width: @feedMaxWidth + @gutter_base){
        //...
    }

    Or even:

    @extraRoomAtThisWidth: @feedMaxWidth + @gutter_base;
    @media(min-width: @extraRoomAtThisWidth){
        //...
    }
  26. Soviut commented on Dec 15, 2013

    @Soviut

    @adam-lynch Both examples should work fine by now. What version of LESS are you using?

  27. adam-lynch commented on Dec 15, 2013

    @adam-lynch

    @Soviut 1.5.0

  28. Soviut commented on Dec 15, 2013

    @Soviut

    If you have the strictMath option turned on, you'll need to wrap your math operations in parenthesis (@feedMaxWidth + @gutter_base)

  29. adam-lynch commented on Dec 15, 2013

    @adam-lynch

    @Soviut, parenthesis sorted it.

    Strangely though, I don't have strictMath set to true. Even when I set it to false I was still getting it.

    My config:

    {
            env: "production", // or "production"
            async: false,       // load imports async
            fileAsync: false,   // load imports async when in a page under
            // a file protocol
            poll: 1000,         // when in watch mode, time in ms between polls
            functions: {},      // user functions, keyed by name
            dumpLineNumbers: "comments", // or "mediaQuery" or "all"
            relativeUrls: false,
            strictMath: false// whether to adjust url's to be relative
            // if false, url's are already relative to the
            // entry less file
            //rootpath: ":/a.com/"// a path to add on to the start of every url
            //resource
        }
  30. seven-phases-max commented on Dec 15, 2013

    @seven-phases-max
    Member

    The trick is that math operations are not evaluated without parenthesis inside media feature expressions regardless of strictMath settings. E.g.:

    @feedMaxWidth: 11;
    @gutter_base:  22;
    
    @extraRoomAtThisWidth: @feedMaxWidth + @gutter_base;
    @media (min-width: @extraRoomAtThisWidth) {
        - {value: @extraRoomAtThisWidth}
    }
    
    @media (min-width: @feedMaxWidth + @gutter_base) {
        - {value: @feedMaxWidth + @gutter_base}
    }
    
    @media (min-width: (@feedMaxWidth + @gutter_base)) {
        - {value: (@feedMaxWidth + @gutter_base)}
    }
    

    result (strictMath: on):

    @media (min-width: 11 + 22) {
      - {
        value: 11 + 22;
      }
    }
    @media (min-width: 11 + 22) {
      - {
        value: 11 + 22;
      }
    }
    @media (min-width: 33) {
      - {
        value: 33;
      }
    }
    

    result (strictMath: off):

    @media (min-width: 11 + 22) {
      - {
        value: 33;
      }
    }
    @media (min-width: 11 + 22) {
      - {
        value: 33;
      }
    }
    @media (min-width: 33) {
      - {
        value: 33;
      }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions