LESS, Media Queries, and Performance - css

I recently got started with LessCSS, and I've been having quite a bit of success in regards to how clean and readable my CSS has become. However, I don't think I'm using Less to its fullest advantage.
I'm now in the process of coding up my first fully-responsive site using Less, and I'm concerned with performance and speed. One thing to note is that I don't stick to a "breakpoint" methodology - I scale things up and down until they break, then I write CSS to fix them; which usually results in anywhere from 20 - 100 media queries.
I'd like to start using Less to nest the media queries inside my elements, such as the example below:
.class-one {
//styles here
#media (max-width: 768px) {
//styles for this width
}
}
.class-two {
//styles here
#media (max-width: 768px) {
//styles for this width
}
}
Through my initial testing, I have found that when reviewing the compiled CSS output - this methodology results in multiple (many!) instances of #media (max-width: ___px). I have scoured the internet, and haven't found anything that explicitly explains the performance implications of nesting/bubbling media queries.
Update 1: I realize that more CSS information results in a larger CSS file to download - but I'm not as worried about site load time as a sole metric. I'm more interested in the browser's handling of memory and performance after the DOM is ready.
Update 2 & Semi-Solution: I found this article which discusses the four main categories of CSS selectors and their efficiencies. I highly recommend the read which helped me sort out how best to tackle my over-media-query'd CSS.
So my question is this: after the DOM is ready, will having this many media queries in my compiled stylesheet affect load times & performance?

Its almost impossible to give you a totally accurate answer.
When asking about performance the typical answer is to profile it and see what you get. Fix the slowest part first, then repeat.
You can profile CSS selectors in the Chrome Developer tools:
Ultimately I don't think the media queries will have anywhere near as much impact on performance compared to even a slightly complicated selector.
You can see more information about writing efficient CSS here:
https://developers.google.com/speed/docs/best-practices/rendering
Also with regards to file size and repeated CSS, gzip almost completely nullifies this as the gzip algorithm works best with repeated data.

> also u can write like this :-
// breakpoints
#mobile: ~"(max-width: 767px)";
#tablet: ~"only screen and (min-width: 768px) and (max-width: 991px)";
#desktop: ~"only screen and (min-width: 992px) and (max-width: 1199px)";
#desktop-xl: ~"only screen and (min-width: 1200px) and (max-width: 1350px)";
body{
background:black;
#media #mobile {
background:red;
}
}

Related

CSS media queries - many vs few

Suppose 1 css file, and 3 #media formats, say:
#media (min-width:1280px)
#media (min-width:640px) and (max-width:1279px)
#media (max-width:639px)
Is there a significant performance difference between using each #media format once and cramming all appropriate CSS rules inside, such as:
#media (min-width:1280px) {
/* all css for this format */
}
#media (min-width:640px) and (max-width:1279px) {
/* all css for this format */
}
#media (max-width:639px) {
/* all css for this format */
}
VS
Using as many #media declarations as necessary (about as many as you have CSS rules/sections)... such as:
div.some-class-name {
/* styles */
}
#media (min-width:1280px) {
div.some-class-name {
/* styles for this format */
}
}
#media (min-width:640px) and (max-width:1279px) {
div.some-class-name {
/* styles for this format */
}
}
#media (max-width:639px) {
div.some-class-name {
/* styles for this format */
}
}
I personally prefer version 2 (though maybe not so excessive), as it's much easier to track a specific pair of styles for each #media query.
thx for feedback!
I'm not an expert on this, but I found a decent article addressing this:
https://helloanselm.com/2014/web-performance-one-or-thousand-media-queries/
It doesn’t matter if you use one big or multiple media-queries in your code assuming your values are mostly the same. It shouldn’t matter if your values are different for each(!) of your hundreds of media-queries but could affect performance (found no way to track this down). The only thing I could add here is that using many inline media queries often is that they’re increasing the size of your CSS (unless your gzipping it) and that affects performance as it’s loaded blocking.
Makes sense to me. I personally like to group element styles rather than grouping media queries (version 2 in your question). In my mind, CSS's goal is to style your elements and styling them based on screen size is a secondary goal.
In practise no there shouldn't be a significant performance difference, there shouldn't be one at all. Browsers generally serialise and strip out duplicate media queries.
The only potential issue is if the extra MQs add bloat to your css file and thus it takes longer to download, delaying the render. It is pretty unlikely for this to happen.
You can read a little more about it here
The performance difference issues in your examples are extremely small. The only thing I can tell you about performance is this: If you use stylesheets, ALL stylesheets will be loaded by the browser. Only the one that matches the media query will be applied.
Basically, just use whichever method is easier to change, organize, etc. . . my guess is stylesheets.
About the NUMBER of queries, there will be no performance changes. Most browsers automatically calculate the width, height, screen, type etc., even without media queries.
Hope that helps!
Your version 1 is very similar to v2. The difference I see is you wrote "all CSS" versus "styles for this format". In the case of writing all of the CSS you will have a much larger file than V2. Loading a larger file takes extra time than a smaller file, so version 2 will be more efficient.
In any case, use these 2 rules for performance:
1) Shorten your CSS as much as possible. Avoid redundancy wherever you can. If in the top media query you use "h2 {padding: 0 }, then don't use that in the other queries as that would be completely unnecessary.
2) Look at the number of characters or file size of your CSS. The smaller file will always load faster.

Media Queries issue: Seems to be igorning main styles on desktop

Got some weird stuff going on. Trying to fix up an old WordPress theme which was never designed for mobile and I found issues once I added media queries. They seem to be what I want overall on mobile devices but once I hit desktop, everything looks messed up as if it's adapting to the mobile media queries and I'm really confused as to why. Am I supposed to add something to the desktop styles to make this work overall? Here's my site in question: http://destinationbeershow.com/
If you have
<body class="mobile">
at your mobile version and you specify the .mobile in all your rules affecting only mobile, then I guess you will be out of the woods.
Actually, i just solved it. I had min-width for those elements when I meant to use max-width. Duh! I think I'm out of the woods.
You might want to clarify with at least one or two examples of the specific problems you're encountering, but just looking at one or two elements, remember some basic CSS rules.
When using media queries, any rules meeting the conditions will be triggered.
Rules overwrite each other top to bottom, which means whatever is listed last will be the property used.
If you're encountering problems when your rules look different, remember that whether CSS rules overwrite each other depends on a rule's specificity. (This is more of a side note, but important to remember. See this article on calculating CSS specificity if this is a problem you're encountering.)
For example:
#media (min-width: 768px) {
#content {
width: 656px;
}
}
#media (min-width: 480px) {
#content {
width: 100%;
}
}
Once the viewport (browser window size) is 480px your element with id="content" will be 100% width. Then, when your viewport is 768px, it will still be 100% width, because the second rule is overwriting the first one since both rules are true.
If you want rules to override the smaller media query rule, then you have to make sure your larger size media query comes after. For example:
#media (min-width: 480px) {
#content {
width: 100%;
}
}
#media (min-width: 768px) {
#content {
width: 656px;
}
}
Hope that makes sense.

What is the impact of having multiple CSS media queries in one stylesheet?

I have a very large and complicated stylesheet. Because of the way I've built the organization of the styles, it would make sense to me to put in some media queries for smaller resolutions throughout the stylesheet, rather than group them all into one at the end like I normally see.
For example, here's some pseudo-code for what I mean:
.navbar { width: 300; height: 20px; background: gray; }
.navbar ul { etc etc }
#media screen and (max-width: 800px) {
.navbar ul { something to make it smaller..... }
}
.....30 some-odd lines later....
#contentblock { some styles }
#media screen and (max-width: 800px) {
#contentblock a { something that makes this one smaller and stuff }
}
^^Hope that all makes sense.
My question is: although I am allowed to do this, does it dramatically impact the load/processing time? Is it a memory hog that should be avoided by grouping all the media queries at the bottom of the sheet, or for the sake of my own convenience, I can go ahead and do it with minimal negative impact?
Negligible.
Have you not seen the big sites and their 30k+ line stylesheets? Browsers can load it very fast, it shouldn't make a difference whether or not they are grouped together.
The real difference is on your end. Make it so that it's efficient for you to develop. Personally I think it looks better for them to be next to related styles rather than on the bottom. But be mindful that newer styles override older ones, that may be where the practice of grouping media queries on the bottom came from.
Unless you're sending your code back in time to the AOL dial-up days, this will not have any noticeable impact on performance/processing time.
That being said, don't forget your order of operations. You'll have to be careful not to overwrite your media queries. Keeping them all at the end of the stylesheet helps in preventing that and ensures that your media queries are at the end of the cascade.

css media queries - is this a bad way to implement?

I recently asked a question about how to use media queries while supporting fallback for older browsers that don't support them. The only answer (while it works) was to use javascript such as adapt.js to determine which stylesheet to load.
I have been tinkering around and realized an unbelieveably simple solution that worked for me in IE7 anyways, was the following:
.wrapper{width:1024px;}
#media all and (min-width: 1025px) {
.wrapper{width:1024px;}
}
#media all and (max-width: 1024px) {
.wrapper{width:1024px;}
}
#media all and (max-width: 900px){
.wrapper{width:900px;}
}
The above is just a really simple example. When I fiddling around I noticed if I specified a default value for .wrapper IE7 rendered it and ignored the media queries. In Chrome/FF/Safari it used the media queries css. This leads me to think this can be a compatible workaround to javaascript but I'm not sure of any ramifications whether browser compatibility or SEO.
Is this a bad way to implement and will it have any compatibility issues? I like the idea of having all css in one file.
Any thoughts would be appreciated, thanks!
Your ordering of max-width media queries means that by the cascade, this rule becomes totally unnecessary; you can remove it unless you intend the styles in this rule to be different than your first rule which doesn't sit in its own #media block:
#media all and (min-width: 1025px) {
.wrapper{width:1024px;}
}
Besides that, I don't see any ramifications or compatibility issues. It's pretty much how media queries with #media rules are meant to be used.

While making responsive website which CSS we should keep outside media queries? Smaller one or bigger one?

I'm making a website which has 3 breakpoint 768px, 1024px and 1900 px. Which size of CSS is good to keep outside media query containers?
Adding example
All specific styling inside media queries and all common styling outside
h1 {color:red}
#media only screen and (min-width: 480px) {
h1 {font-size:18px}
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px}
}
#media only screen and (min-width: 1024px) {
h1 {font-size:28px}
}
or
Most common used desktop first
#media only screen and (min-width: 1024x) {
h1 {font-size:28px; font-color:red}
}
#media only screen and (min-width: 480px) {
h1 {font-size:18px}
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px; }
}
or
Mobile first
#media only screen and (min-width: 480px) {
h1 {font-size:18px; font-color:red}
}
#media only screen and (min-width: 1024x) {
h1 {font-size:28px; }
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px; }
}
I believe you mean to ask what CSS should not be inside of the media query blocks, right?
If that is the case I recommend that any CSS that does not change be placed outside of the media query blocks. Any colors, font styling, etc. Any CSS that changes placement of elements, the padding, floats, inline or block display types, any structure-type CSS is what I would put in the media query blocks.
Update: To respond to the updated question, are you asking which order you should put the media blocks in? If that's the case as far as I know it doesn't really matter what order they go in. But to comment on the number of possible media queries, I would separate that CSS into different style sheets just to make it more maintainable. Your media queries would then be a part of the links to your style sheets in your HTML.
There are so many ways to approach this problem - and the decision may be different depending on the circumstances. For example, is there an existing site that you are reverse engineering to be responsive or are you starting from scratch?
STARTING FROM SCRATCH
If starting from scratch, one method is to create all of the basic styles OUTSIDE of any media query - so that these styles can be seen by any device (especially those devices that do not support media queries).
Basic styles could include just colors, and fonts etc - or it could be everything except layout.
Then, media queries are used to add the different layouts on top of the basic styles.
MIN or MIN AND MAX
The next question is how will you work your different media queries...
Will you allow them to be applied on top of one another - in which case you may start small and build up - using min-width only.
For example:
#media only screen and (min-width: 600px)
OR you may want to set them in a series of brackets - so that styes for one size do not interact with another size.
For example:
#media only screen and (min-width: 600px) and (max-width: 800px)
Again, there is no right or wrong - both have strengths and weaknesses. The first option allows you to use styles that flow through all widths. The second option allows you to fully control styles that appear in a specific width - without having to deal with the cascade.
DEALING WITH IE
There are a range of ways for dealing with older versions of IE including.
allow IE to see basic styles only
place media queries in separate CSS files and link to these files using media queries... then also link to a selection of these files (like wide screen CSS files only) via conditional comments.
Use some sort of JS solution like respond.js or others to force IE to understand the media queries.
HTH
I've read many articles recently that suggest starting with the smallest resolution first and working your way upwards using media queries. To me that also makes a lot of sense. The only problem is old browsers (IE) not understanding media queries. There are solutions to that problem though (if you Google).

Resources