CSS - all media queries in one file? or load split CSS files?

What is the best solution:

1) create one CSS file with all media queries for all supported permissions

<link rel="stylesheet" href="mobile.css" type="text/css" media="all" /> 

2) create a CSS file for each supported resolution and load it in the header

 <link rel="stylesheet" href="mobile.css" type="text/css" media="screen and (min-width:240px) and (max-width:639px)" /> <link rel="stylesheet" href="tablet.css" type="text/css" media="screen and (min-width:640px) and (max-width:1023px)" /> 

???

I mean, in attempt 2) there are more requests, but is this really important?

+5
source share
2 answers

You need to find a difficult balance . On the one hand, you want your requests to be as low as possible, since HTTP1 does not allow the use of multiple resources in a single request, and HTTP2 is not as widespread as it will be.

On the other hand, there is a parsing speed . On mobile devices, file size. It takes more time to parse (= translate your css to the actual result) compared to the desktop machine.

If you have a large file, say 500kb +, an additional request may be worth it (and if used correctly, the cache will take only one download per cache period). Try to minimize the number of files, for example. do not do one for <320, <680, <768, <1024, etc. etc. Separate two, possibly three files (unless you have good reason for this).

You can also enable it arround . Some developers like to create mobile devices first, and then scale to the size of the desktop. Instead of changing css when decreasing, you use screen and (min-width:1024px) . Then keep your mobile CSS in the main stylesheet and give the browser version a 1024px + file, which is often more efficient, so it will have less effect.

And do not forget about maintainability . You must support the code. If you split everything into files, you won’t make it easy for you. If the solutions you use are minimal, you may need to reconsider, because complex code will have the worst effect in the long run.

+2
source

Fewer requests are always beneficial due to the nature of HTTP 1.1 (for each individual request that takes time, new TCP connections are established).

I would highly recommend using multiple queries as much as possible throughout the application. However, for CSS, you can use SASS or LESS to compile multiple files into a single CSS file. The same can be achieved in different ways using JavaScript, for example, using online tools .

A media request will not stop a file request, regardless of the current screen size:

Resource extraction network requests

It's also worth noting that SPDY and HTTP 2.0 are on their way to accessibility in most major browsers.

If you want to read more , I am a co-author of an article on this subject.

+6
source

All Articles