Skip to content

Media queries: parentheses needed even without --strict-math=on #1480

Closed

Description

@papandreou

This example produces different output under less 1.3.3 and 1.4.2:

.foo (@dpr) {
    @dpi: @dpr * 96dpi;
    @query: ~'(min-resolution: @{dpi})';
    @media @query {
    }
}
.foo(2);

Output with less 1.3.3:

@media (min-resolution: 192dpi) {

}

Output with less 1.4.1:

@media (min-resolution: 2 * 96dpi) {

}

Adding parentheses around @dpr * 96dpi makes less 1.4.1 output the same as 1.3.3, but this was an unexpected gotcha with the upgrade to 1.4.x.

Activity

  1. lukeapage commented on Aug 15, 2013

    @lukeapage
    Member

    ah, a bit of a edge case.. we force strict maths on in media queries so that people can use fractions, thanks for raising the issue.

  2. changed the title [-]1.4.1: Parentheses needed even without --strict-math=on[/-] [+]Meida queries: parentheses needed even without --strict-math=on[/+] on Sep 5, 2014
  3. changed the title [-]Meida queries: parentheses needed even without --strict-math=on[/-] [+]Media queries: parentheses needed even without --strict-math=on[/+] on Sep 5, 2014
  4. clayzermk1 commented on Sep 18, 2015

    @clayzermk1

    👍 Also seeing this on less 2.5.1. The suggested workaround of putting parenthesis around the variable in the media query works.

    Trivial problem case:

    @foo: 10px;
    @bar: 20px;
    @baz: @foo + @bar;
    
    .test {
      @media (min-width: @baz) {
        color: red;
      }
    }

    Compiled CSS of trivial problem case (invalid):

    @media (min-width: 10px + 20px) {
      .test {
        color: red;
      }
    }

    Trivial problem case with workaround:

    @foo: 10px;
    @bar: 20px;
    @baz: @foo + @bar;
    
    .test {
      @media (min-width: (@baz)) {
        color: red;
      }
    }

    Compiled CSS of trivial problem case with workaround:

    @media (min-width: 30px) {
      .test {
        color: red;
      }
    }
  5. andrewnicols commented on Apr 8, 2016

    @andrewnicols

    We're also affected by this in Moodle as this issue affects the stock version 2.3 of bootstrap.

    In this case the variable is assigned (https://github.com/twbs/bootstrap/blob/v2.3.2/less/variables.less#L181):

    @navbarCollapseWidth:             979px;
    @navbarCollapseDesktopWidth:      @navbarCollapseWidth + 1;
    

    The media-query is (https://github.com/twbs/bootstrap/blob/v2.3.2/less/responsive-navbar.less#L181):

    @media (min-width: @navbarCollapseDesktopWidth) {
    }
    

    This leads to an output of:

    @media (min-width: 979px + 1) {
    }
    

    https://tracker.moodle.org/browse/MDL-53152

  6. es6Test commented on May 21, 2016

    @es6Test

    I tried doing something like

    @media (min-width: @navbarCollapseWidth + 1) { }

    so as to avoid creating the extra less variable, but this doesn't work, any ideas why?

  7. seven-phases-max commented on May 21, 2016

    @seven-phases-max
    Member

    so as to avoid creating the extra less variable, but this doesn't work, any ideas why?

    Currently parens are required for arithmetic expressions in @media statements, e.g.:

    @media (min-width: (@navbarCollapseWidth + 1)) { }
  8. es6Test commented on May 21, 2016

    @es6Test

    I get an error for

    @media (max-width: (@sm -1)) {
      .hidden-sm-down{  // hide anything up to small size
        display: none;
      }
    } 
    

    Error loading "app/styles/index.less!$less" from "app/app" at http://localhost:63342/foo/app/app.js Expected ')'"

  9. seven-phases-max commented on May 21, 2016

    @seven-phases-max
    Member

    I get an error for

    You're mixin up @sm -1 and @sm - 1 (the first one is not an arithmetic expression but a two value list).

  10. added a commit that references this issue on Jun 8, 2017
    faafd33
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions