-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Group media queries #950
Comments
How do you do that without potentially altering the meaning by having the statements re-organised? |
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. |
What are the rules around selector complexity inside media queries? Do the |
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. |
Here's some documentation from Mozilla |
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
|
It should only combine if the rulesets follow each other directly. |
which they don't in @Soviut original example, making this feature request of limited use in IMO |
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. |
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? |
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
Then below, we create more mixins to wrap each sprite element like this:
And use it in other LESS file(s) like this:
In this case, the generated CSS looks like:
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 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. |
Its a good example.. and a good use-case.. You could do
(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) |
+1 for @media-group |
+1 @media-group |
The other option would be to have a global option for grouping true/false. |
Hmm, this is probably a good idea. Would there be any case where selective grouping would be necessary? I think in most cases, people just want their media queries more compact so a global option for now might be the way to go. Should any one claim to need selective grouping, new keywords could be added later. +1 for a global grouping option. |
yes.. because we saw with |
I'd say add the option since it would be useful immediately and would be able to co-exist as an override with a selective import should it ever be created. |
Hello, I just saw this issue and wanted to share a little app I made when I needed to group media queries. This is not a finished app. More like some research for a future tool. So I just want to show you the idea, because I really think it's something that must be implemented. (there can be bugs and other problems) but I've tested with my last project which includes twitter bootstrap and it work properly. (no problem with the order of rules) let me know ;) |
Hello! Is someone assigned to this? It's a great feature which could be very useful to decrease css file when created with less. And in this way, It will be easier for developers to work with all these @mqs. |
@AoDev mqhelper is definitely a step in the right direction, I could see it being useful as part of a CSS linting process for now. I think it just needs the core functionality extracting from the front-end stuff in your demo. |
Yes but the goal of my app is not to be part of some "real tool". I just saw a lot of people here wondering if grouping media queries could be a problem and wanted to show that it can be done. The code is there for the devs of less if they want to have an idea and use parts of it. But I can make it a "real" module if you want :) |
I already managed it, it's a neat piece of work, thanks. |
@Soviut @lukeapage I think selective grouping makes the most sense. All or nothing wrecks havoc on trying to maintain the cascade. Should be something similar to extend, or maybe literally adding a group keyword. Like this syntax would be f'ing great:
|
There is a solution for this https://github.com/buildingblocks/grunt-combine-media-queries however it only orders by min width at the moment so is mainly useful for mobile first sites. |
IMO it makes sense to generalize the problem to scope grouping control which will provide solution for issue #930 |
Great tool @Krava ! thanks |
Excelent!!! IMO it makes all the sense implement KRAVA´s feature on LESS |
+1 |
I want to do it as a plugin. should be not too hard. too much to do though! |
I think plugins should take lower priority than having a system to auto-load plugins (options.json). But yes, a plugin makes sense as an additive feature. |
Has this option been implemented yet? Regarding slector reordering, if you use a "group" keyword to group something you are aware that it will be removed from the current logical flow and placed in a grouped area. |
http://helloanselm.com/2014/web-performance-one-or-thousand-media-queries/ |
It's not so much an issue of performance as it is the size of the resulting CSS file. Dozens of |
I have asked this question on SO and somebody gave a partial answer to this issue. |
@seven-phases-max gave you the answer and referred back to this issue in his answer. Very meta ;) |
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: |
I just did a search and found two grunt plugins that do media query grouping: https://github.com/buildingblocks/grunt-combine-media-queries |
Combine media queries is also available for gulp. |
Since v2 you can use plugins, so see https://github.com/bassjobsen/less-plugin-group-css-media-queries (and https://github.com/bassjobsen/less-plugin-pleeease) |
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). |
@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. |
@nfq This is not quite an initialization but just "default" empty definition. It's necessary in case you don't provide your custom |
Btw, the advantage of this technique is that combining media queries slows down compilation time (a bit). |
@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!! |
@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! |
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; }
}
less-plugin-clean-css sets the --skip-advanced true by default you should explicit set the |
@nfq With the "mixin approach" media queries are still compiled at the bottom (or anywhere you declare them). |
@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! |
While the media query bubbling is great:
Less generates fairly bloated code because it repeats the mediaquery selector each time its declared in the less file:
It would be nice if media queries could be grouped if they're identical:
The text was updated successfully, but these errors were encountered: