The Right Way to Remove Unused CSS in WordPress
July 30. 2021
Having a lot of unused code can considerably slow down your website. And one of the usual suspects in WordPress is unnecessary CSS code. Fortunately, there are a few ways to prevent the poor performance of your website and stop ruining the user experience. While one way to do it is a manual method, it requires a significant developing experience. Another, the more beginner-friendly option, is to focus on existing tools and remove unused CSS in WordPress, added by themes and plugins, with just a few clicks.
How to successfully remove unused CSS in WordPress
As you are probably aware, WordPress is using many plugins, themes, and other third-party libraries. They all bring numerous features to improve your website functionality. But, more often than not, the majority come with loads of functions you don’t actually use. Nevertheless, if you don’t specifically use WordPress professionals to maintain and optimize your website, it will load everything for every visitor. This results in transferring large amounts of unused CSS and slowing the website itself. Consequently, poor performance will reduce your website traffic and lower your search rankings.
In many cases, removing 100% of unused code is quite hard, almost impossible. But even removing the slightest amount or a conflicting script, will improve your website performance. Aside from a typical “minimization” of code, there there are several ways to reduce the impact of unused CSS code without breaking your website.
Why the unused CSS is even there?
Because CSS is responsible for styling the appearance of your WordPress website, every theme you can find includes it. And not only themes but a lot of plugins come with their own styling and customization options. Not to mention all kinds of website builders, libraries, and even individual elements, where you are using just a small portion of it. In general, a few plugins won’t really affect the website so much. But, if have plenty, the cumulative effect can really slow your website.
Tools for removing unused CSS
While there are many tools that can help you remove unused CSS, the majority are not appliable for every situation. Of course, for static websites, most of them will be quite helpful. However, dynamic websites often load and style elements by injecting CSS classes with JavaScript. And this is the harder part since it’s hard to detect these classes. For example, eCommerce websites that dynamically style products and cart pages.
Think about it during development
One solution to get rid of unused CSS from the start is to think about it during website development. You can basically split your CSS code into different files, each serving a specific purpose. However, most people prefer to use WordPress themes to avoid building a website from scratch. And this is where you either have to be very skillful or find WordPress experts to do it for you.
Using UnusedCSS online solution
Another way is to use premium solutions like UnusedCSS to easily find and clean up your CSS issues. The advantage of this tool is that it’s able to scan your JavaScript files and search for injections. Some of its most notable features are:
- Auto finding unused CSS rules
- Provide you with clean CSS for downloading
- Exploring responsive design and media queries rules
- Checking website for changes
- Preview of changes
- and more
PurgeCSS for removing extra CSS
This is another tool quite helpful during development. Those who are working with Bootstrap, Foundation, and similar CSS frameworks find it very useful. Since you are probably using only a fraction of CSS rules, this tool can help you filter out all the unused styles. Basically, this is a great way to make your CSS file considerably smaller.
Autoptimize plugin
The next in line is the very popular WordPress plugin. With it, you can easily minify, combine, and cache styles. It injects necessary rules in the page head, inline critical CSS, minifies HTML and moves scripts to the footer. In essence, it can be used to implement the “lazy-load” for images, optimize fonts, and async non-combined JavaScript. Every website can benefit from this extensive plugin.
WP Rocket
One of the most beginner-friendly options you can find. WP Rocked offers effective solutions for your page and speed optimization. Not only it will get you satisfying results on speed tests, it will visibly improve user experience to visitors. When you want to remove unused CSS, simply activate it, turn on the “File Optimization” and proceed to the “Optimize CSS delivery” option. It will provide you with a CSS file with only the necessary CSS rules, which will load before others. In addition, WP Rocket will automatically clear the cache and will allow you to minify and aggregate CSS files.
Remove unused CSS with Asset CleanUp
You can use Asset CleanUp to unload unused files from themes and plugins. It allows you to complete this task for each page separately. While this is a bit complex and time-consuming way, it’s a very effective method to use. Just like some other plugins, it has the “Test Mode” option to prevent undesired effects on your live website. However, Asset CleanUp can also help you remove unused JavaScript files as well. Finally, when you test what your visitors see, you can simply choose to unload everything that is not necessary.
Optimize your CSS with Perfmatters
The last, but not least important on this list is one of the premium performance plugins. Perfmatters will help you remove both unused CSS and JavaScript. After registration, installation, and activation, you will be able to access its script manager. This, among other options, can help you modify each of your pages or posts with ease. In essence, it provides you with a simple dashboard menu you can use to remove any unused CSS and JavaScript on separate pages or for the entire website. Just like for CSS, you can use it to combine or minify JavaScript files for better performance.
Additional considerations to improve the website speed
Consider using CDN to deliver CSS files – to considerably reduce the loading speed of your CSS or any other files.
Minify and combine most of your CSS files regularly – to remove unnecessary rules, classes, and extra characters from your CSS in WordPress. However, it’s arguable if this is helpful for all large websites due to the complexity of code, size of such files, and additional later optimization.
Removing absolutely every portion of unused CSS in WordPress is almost impossible. But, you can use the above methods to speed up your website quite fairly. It will depend on the number of cases, but if your WordPress website really has plenty of unused code, the difference will be quite noticeable. On the other side, sometimes even a small change can mean everything. While the goal is not to have the fastest website in the world, slight changes matter. But, other efforts should be focused on content and usefulness to users, rather than winning top speed positions in search engine tests.