What can be done to improve the performance of a very large CSS (384KB)

Asked

Viewed 159 times

12

My site has a style sheet, already minified, with a total size of 384KB.

If possible, what can I do to improve performance?

I thought of compressing with GZIP, which would leave the sheet with 59KB, and even divide this sheet into 2 sheets of 29.5 KB each. This helps?

  • You’ve already taken a look at the tab Audits Developer Tools in Chrome? A series of items is displayed that can be done to improve the performance of your page

  • 2

    Probably organizing the styles is the best way. Great chance of the problem being the way the sheet itself was made. There may even be a real reason for a leaf to have all this, but I would bet more on not being that case. And gzip has to do is the server, automatically in the negotiation of the protocol, if it is properly configured.

  • 4

    If Gzip has reduced the size so much, it’s because there’s something repeated that probably didn’t need to be (after all, that’s how compressors work...). It’s kind of hard to answer so "in the air" what can be done, but if you post your sheet on Pastebin or another site of this type we could take a look and, who knows, help identify some anti-patterns... P.S. Splitting the sheet into two or more does not help, on the contrary, hinders, and I agree with Bacco on the issue of gzip.

  • 2

    I make my words those of Bacco and mgibsonbr. Probably the best way is to fix your leaf, if possible, of course.

  • It’s just that I’ve gathered several leaves into one. That’s why it has this whole size. (ps: the site is responsive, so it increases the sheet size more because it has more lines). The sheet is correct, that’s not the problem. The size is due only to multiple unified sheets. About gzip, I already know it’s done by the server, and I already use ._.

  • I only know that gzipado gets 59KB because I did tests via server.

  • I think if you joined several sheets ctza there are several components in common can unify in css, but if you do this depending can lose readability.

  • Look at this, maybe you’ve seen it, http://browserdiet.com/pt/

Show 3 more comments

1 answer

11


A number of styles like this should fit into one or more of the following categories...

Attribute redundancy

There may be many repeated attributes in several classes. For example, imagine that you are formatting the titles like this:

h1 { font-weight: bold; font-size: 24px; ... }
h2 { font-weight: bold; font-size: 20px; ... }
h3 { font-weight: bold; font-size: 16px; ... }
...

In this case, you could group the common attributes like this:

h1, h2, h3 { font-weight: bold; ... } 
h1 { font-size: 24px; ... }
h2 { font-size: 20px; ... }
h3 { font-size: 16px; ... }

Very specific styles

Another common problem is to always use very specific classes. I’ve seen some systems with a class for each element using the id as selector.

Imagine something like this:

#link1 { A; B; C; D }
#link2 { A; B; C; D }
#link3 { A; B; C; D; E }

(Being A, B, C, D and E any styles)

If you have multiple CSS classes with similar styles, separate what is equal and put into a new class, then use style composition in the attribute class page elements separating class names by whitespace.

Example:

.link { A; B; C; D }
.link-maior { E }
<a href="#" class="link link-maior">Meu Link</a>

In the example above, the class .link could contain the general attributes of links, while the class .link-maior would have a specific format for larger links.

Lack of pattern

A large CSS can also originate in lack of standardization, where each component of each screen is slightly different from the others.

In this case the ideal is to refactor the screens, standardizing them in a set of unified styles.

Generation tool

Another possibility is that some tool is generating styles with too much junk, and several of the above scenarios may occur simultaneously.

I know a tool of this type where all the components of the screen are positioned absolutely. As the positions and styles are unique to each page element, the system generates a CSS file with a class for each element with its position and formatting.

The ideal would be to throw away this tool and make the screens the right way. If not possible, at least divide the styles of each screen into separate files.

Really large and complex system

Finally, it may be that the system is really big enough to justify such a great style.

In this case, the best strategy would be to split the file so that it is not loaded as a whole.

Place the main classes used on all screens in a main file, then divide the rest of the styles into different files following some criteria related to system use, such as groups of screens that are usually used at the same time, user types (admin, normal, ...) or with an individual CSS for each screen.

The overall size of the styles will not decrease, but the loading of the Csss will be diluted between several requests, improving a little the user experience when accessing the system.

Don’t forget to compress

The Questioner (AP) stated that the file is already compressed or "minified", which consists of removing all unnecessary characters such as whitespace and line breaks.

But to make the answer complete, we must not forget that applying the "minification" is an elementary step towards optimizing the response time of any non-trivial application.

  • +1. Apart from the last case (given the size of the CSS), I think there is a bit of everything messing up the sheet in question.

  • Missing mention blank spaces and comments that are unnecessary and can be removed automatically by tools like YUI Compressor. There’s even a online version his.

  • @Brunoaugusto True. But I did not speak because the AP said that his is already compressed.

  • Yes, yes, I said more to make the answer more complete even for future reference.

  • Yes @utluiz is a very complex site, has enough elements, pages... Is it normal for a site to have at least 59KB of CSS throughout the site? Or is it still a lot? Asd

Browser other questions tagged

You are not signed in. Login or sign up in order to post.