Skip to content

Group media queries #950

Closed
Closed

Description

@Soviut

While the media query bubbling is great:

header {
    color: green;

    @media only screen (max-width: 500px) { color: red; }
}

footer {
    color: green;

    @media only screen (max-width: 500px) { color: red; }
}

Less generates fairly bloated code because it repeats the mediaquery selector each time its declared in the less file:

header {
  color: green;
}
@media only screen (max-width: 500px) {
  header {
    color: red;
  }
}
footer {
  color: green;
}
@media only screen (max-width: 500px) {
  footer {
    color: red;
  }
}

It would be nice if media queries could be grouped if they're identical:

header {
  color: green;
}
footer {
  color: green;
}

@media only screen (max-width: 500px) {
  header {
    color: red;
  }
  footer {
    color: red;
  }
}

Activity

  1. lukeapage commented on Sep 11, 2012

    @lukeapage
    Member

    How do you do that without potentially altering the meaning by having the statements re-organised?

  2. Soviut commented on Sep 11, 2012

    @Soviut
    Author

    I don't think the meanings could be altered. This would be exactly the same as collapsing normal redundant tags. For example:

    body {
        background: white;
    }
    
    body {
        padding: 0;
        margin: 0;
    }

    Would be collapsed to:

    body {
        background: white;
        padding: 0;
        margin: 0;
    }

    This is even more the case with media queries because they're special top level selectors that act as an control layer on top of normal element selectors. Basically, they only have a single meaning and aren't affected by the rest of the css within the file.

    Further, less already maintains the order of the bubbled media queries, it just creates a lot of redundant selectors for the exact same query. If they could be buffered and written to a single query at the end of processing it would be much neater and producer smaller css output.

  3. lukeapage commented on Sep 11, 2012

    @lukeapage
    Member

    What are the rules around selector complexity inside media queries? Do the
    queries bump complexity up and override any ordering? Can you point at any
    specs?

  4. Soviut commented on Sep 11, 2012

    @Soviut
    Author

    Essentially, yes. Media selectors are like IF statements that wrap around normal style rules and only apply if the condition of the query is met. For example, if the width of the browser is less than a certain number of pixels then the rules within the query are turned on and override the existing rules.

    So having lots of identical queries with a single style each would be functionally identical to one query with all the styles inside it. As long as the query is the same.

  5. lukeapage commented on Sep 11, 2012

    @lukeapage
    Member

    what I mean is in this example.. the div goes red - meaning re-ordering the media queries (both for screen) would change the meaning of the css

    @media screen {
        div {
            background-color: green;
        }
    }
    
    div {
         background-color: red;
    }
    
    @media screen {
        div.pink {
            background-color: pink;
        }
    }
    
  6. cloudhead commented on Sep 11, 2012

    @cloudhead
    Member

    It should only combine if the rulesets follow each other directly.

  7. lukeapage commented on Sep 11, 2012

    @lukeapage
    Member

    which they don't in @Soviut original example, making this feature request of limited use in IMO

  8. Soviut commented on Sep 11, 2012

    @Soviut
    Author

    I agree, but I don't see how this would apply to bubbled media queries? Remember, bubbled queries are a bit of syntactic sugar; You can't normally embed a media query inside another selector. So it could safely be assumed that any time you encounter a bubbling query to group it with identical bubbling queries in the order they arrive in.

  9. lukeapage commented on Oct 18, 2012

    @lukeapage
    Member

    If you have two bubbled media queries next to each other that can be combined I think it would be very obvious to do that in the less.. can you give an actual example where it would be safe to combine the media queries and it makes sense to keep them separate in the less?

  10. kamranayub commented on Oct 23, 2012

    @kamranayub

    When dealing with retina images, we've wrapped the complex media query inside a mixin and created sprite mixins, so we have this all over the place... it increases the output CSS but it's more maintainable.

    For example, here's our sprite mixin:

    .sprite(@spritePath, @hdpiPath, @x, @y, @size: auto) {
        background-image: url(@spritePath);  
        background-repeat: no-repeat;
        background-position: -1 * @x -1 * @y; // Negativize the value
        .background-size(@size);
        @media @mediaRetina {
            background-image: url(@hdpiPath);
        }
    }

    Where @mediaRetina is:

    @mediaRetina: ~"only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 4/3), only screen and (min-device-pixel-ratio: 1.3), only screen and (min-resolution: 124dpi), only screen and (min-resolution: 1.3dppx)";
    

    Then below, we create more mixins to wrap each sprite element like this:

    #sprite
    {
        .header-logo()
        {
            .sprite(@globalSpritePath, @global2XSpritePath, 22px, 0, 384px 288px);
            width: 94px;
            height: 59px;
        }
    }
    

    And use it in other LESS file(s) like this:

    h1 {
        width: 94px;
        height: 59px;
    
        a {
            #sprite > .header-logo();
        }
    
    }
    

    In this case, the generated CSS looks like:

    h1 a {
      background-image: url("/images/sprite-global.png");
      background-repeat: no-repeat;
      background-position: -22px 0;
      -webkit-background-size: 384px 288px;
      -moz-background-size: 384px 288px;
      -o-background-size: 384px 288px;
      background-size: 384px 288px;
      width: 94px;
      height: 59px;
    }
    @media only screen and (-webkit-min-device-pixel-ratio: 1.3), 
           only screen and (min--moz-device-pixel-ratio: 1.3), 
           only screen and (-o-min-device-pixel-ratio: 4/3), 
           only screen and (min-device-pixel-ratio: 1.3), 
           only screen and (min-resolution: 124dpi), 
           only screen and (min-resolution: 1.3dppx) {
      h1 a {
        background-image: url("/images/[email protected]");
      }
    }
    

    Now imagine this case being repeated many times. Without grouping, the only way to alleviate this extra weight is to move every retina style into a dedicated LESS file, which may work for small sites, but is unrealistic for a large site.

    This is where it makes sense to group them and keep them separated, especially if you have a big site with a lot of modules (like ours).

    I know what you mean about overriding styles, though, and I'm not sure you could safely implement this exact feature without potentially messing with the cascading a designer might want.

    To me this sounds more like being able to define a "section" (a placeholder) and then defining styles to be placed in it, in whatever order they're written. This is pretty common in server-side templates, where you have a "scripts" or "head" section and then your pages can inject content into them when they're loaded.

    So, I'd like to be able to do this in a LESS file essentially:

    @media { // retina query
        @renderSection("retina")
    }
    
    // in another file
    h1 {
        @section retina {
            // retina styles
        }
    }

    The @section would be replaced on compile with the current selector.

    So, my sprite mixin would just become:

    .sprite(@spritePath, @hdpiPath, @x, @y, @size: auto) {
        background-image: url(@spritePath);  
        background-repeat: no-repeat;
        background-position: -1 * @x -1 * @y; // Negativize the value
        .background-size(@size);
        @section retina {
            background-image: url(@hdpiPath);
        }
    }

    It doesn't need to be this syntax (or implementation), I'm basing it off the ASP.NET Razor syntax since that's what I'm familiar with and I like the syntax.

  11. lukeapage commented on Oct 24, 2012

    @lukeapage
    Member

    Its a good example.. and a good use-case.. You could do

    @media-section
    

    (or media-group) which would then tell less to group the media together (optionally merging it into the media query if it already existed which would allow you to pull the rules to where-ever you wanted to put them).. maybe thats what you mean?

    I'm tempted to think its a good idea (and not too difficult to implement)

  12. Soviut commented on Oct 24, 2012

    @Soviut
    Author
  13. Leegorous commented on Jan 16, 2013

    @Leegorous
  14. lukeapage commented on Mar 22, 2013

    @lukeapage
    Member

    The other option would be to have a global option for grouping true/false.

  15. 37 remaining items

  16. Soviut commented on Mar 25, 2014

    @Soviut
    Author

    @seven-phases-max gave you the answer and referred back to this issue in his answer. Very meta ;)

  17. gyopiazza commented on Mar 25, 2014

    @gyopiazza

    I definitely prefer the mixin method to merge the media queries, as opposed to post processing them. This allows for easier typing and more control over what gets merged and how.

    Here in the comments you can see the solution I use in all my projects:
    #950 (comment)

  18. matthew-dean commented on Jan 20, 2015

    @matthew-dean
    Member

    Closing since this is supported by a plugin, and isn't a priority to move into core (AFAIK - @less/admin correct if this is wrong).

  19. nfq commented on May 18, 2015

    @nfq

    @gyopiazza I have a question about #950 (comment) above: why do you need to initialise the mixin? I mean, the CSS compiles without the init. I guess I am trying to understand best practices and usage.

  20. seven-phases-max commented on May 18, 2015

    @seven-phases-max
    Member

    @nfq This is not quite an initialization but just "default" empty definition. It's necessary in case you don't provide your custom .step*() mixins (i.e. it's assumed that you may have these things in different files, e.g. "default" .step*() definitions and their rendering are in some generic utility/library code, while custom .step*() definitions are in your theme/project specific code).

  21. gyopiazza commented on May 18, 2015

    @gyopiazza

    @nfq It's actually not necessary. Edited :) Edited again, oh my.
    As @seven-phases-max mentioned, it's useful to avoid errors in case you don't use the mixins in your code, since the media-queries will call the non-existent mixin.

    Btw, the advantage of this technique is that combining media queries slows down compilation time (a bit).

  22. nfq commented on May 18, 2015

    @nfq

    @gyopiazza Thanks for the quick reply. I don't mind the compilation time, and for huge projects, I definitely prefer grouping all media queries at the bottom of the main stylesheet. I tried a few of the plugins but found your way the easiest for our use case and the most convenient. Thanks!!

  23. nfq commented on May 18, 2015

    @nfq

    @seven-phases-max Thanks, your answer makes sense. I use less a lot, but haven't yet understood the best way to achieve certain things!

  24. bassjobsen commented on May 18, 2015

    @bassjobsen
    Contributor

    Notice that also clean-css support @media merging since v3, and so does the less-plugin-clean-css

    with main.less:

    header {
        color: green;
    
        @media only screen (max-width: 500px) { color: red; }
    }
    
    footer {
        color: green;
    
        @media only screen (max-width: 500px) { color: red; }
    }

    lessc --clean-css="advanced" main.less outputs:
    footer,header{color:green}@media only screen (max-width:500px){footer,header{color:red}}

    less-plugin-clean-css sets the --skip-advanced true by default you should explicit set the advanced option for @media merging

  25. gyopiazza commented on May 18, 2015

    @gyopiazza

    @nfq With the "mixin approach" media queries are still compiled at the bottom (or anywhere you declare them).

  26. nfq commented on May 20, 2015

    @nfq

    @gyopiazza thanks, yeah. Happy with this approach!!

    @bassjobsen I'll definitely use this on a bigger project. I haven't actually started using Less plugins yet. Thanks for the tips!

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions