Skip to Main Content

Remove unused CSS from Elementor website manually

In this step-by-step tutorial, we will learn how to manually remove unused CSS from the Elementor website without breaking the website.

On this Page:

Many people are unaware of the amount of unused CSS that is loaded or injected onto their pages and posts by their themes and plugins. However, a quick test of your site using Google Page Speed Insights (PSI) or any other lab testing tool will reveal that a large amount of CSS is being loaded.

Removing unused CSS in WordPress and Elementor is a tedious process and it can end up breaking your layout and messing up your design if it isn’t done right.

Performance test

According to Google PSI reports, this page can potentially save over 60 KB which is huge, Your’s site may differ. The bigger your site is the better you can save.

Before the optimization (No optimization)

Before we remove unused CSS Page Speed Insights suggest , we have over 70KB of CSS present in the page

After the optimization (with the Asset CleanUp plugin)

Picture showing after removing unused CSS Problem with Asset clean up and saw 20% improvement

Reduce page weight by 20% after unloading stylesheets with Asset CleanUp

After the optimization ( Chrome Extension with Asset CleanUp Plugin)

After removing unused CSS with the help of Asset clean up plugin and used CSS chrome extension

After removing unused CSS with CSS used chrome extension

This question always comes in Facebook groups.

How do we remove unused CSS from the Elementor website?

Before I discovered this method, I was using Remove unused CSS from the web.dev article to identify unused CSS it was great for smaller stylesheets. But if your website is built with Elementor or any page builders things can be complicated and the size of stylesheets is getting bigger.

Am I right?

I think the answer is true.

That is why I wrote this tutorial to help you solve these problems that are recommended by Google PageSpeed Insights (PSI).

Before you start implementing it on your site, you might be asking yourselves, what are the benefits of removing it?

I want the same answer too so that is why I asked the question a while ago on the WP Speed Matters Facebook Group and this is what I got.

Benefits of Removing Unused CSS

The second point might be incorrect because removing unused CSS wouldn’t fix Rendering Blocking resources, it will surely reduce page weight & HTTP requests.

The Benefit of removing unused CSS

Answers are still there and If you want to learn and improve your site performance, I highly recommend joining WP Speed Matters Facebook Community. You will be going to love it, just by browsing & you will learn tons of advanced stuff from the archive.

This step-by-step article is complicated and long to read

People are lazy and do not want to follow all the processes listed below and want to do it automatically (convenient), So here are some of the 3rd parties plugins you can test on your website.  

If you have another plugin you want me to include. Please don’t hesitate to do it.

Seven plugins will automatically remove your unused CSS from the Website

When you enable the features, make sure you test them carefully even dynamic injected CSS rules because it tends to break style and sometimes missing styles.

Remove unused CSS wp rocket
WP Rocket

WP Rocket removes the BETA-mentioned enhancement in version 3.12. Get more details in the changelog.

Swift unused CSS tab
Swift Performance Pro

Swift Performance allows you to remove unused CSS automatically under advanced settings

Right now Swift Performance (free and Pro) has the option to remove unused CSS automatically from the website because the last time I activated the feature all my hover effects, and pseudo-elements were broken. ( You need to test it thoroughly).

To access this feature. Follow these steps-

  1. Enable Advance view otherwise, it wouldn't show up by default.
  2. Go to Optimization - Styles - Enable Disable Full CSS
Remove unused CSS automatically with FlyingPress
FlyingPress

Ability to generate Critical CSS and reduce Page weight by remove unused CSS.

Picture credit to Gijo for providing the picture.

Rapid Load plugin
RapidLoad Power-Up plugin for Autoptimize

It is an add-on for the Autoptimize WordPress plugin. 

Unused CSS removal by NitroPack
NitroPack

Picture credit to NitroPack

Perfmatters - Remove Unused CSS removal out of beta
Perfmatters: Remove Unused CSS

In Version 1.9.2, Perfmatters Remove the Unused CSS BETA tag, and a Huge thank you to Massimo Villa for the update and for providing me the image. 

LiteSpeed Cache unused CSS removal
LiteSpeed Cache (LS Cache) - Generate UCSS (Unique CSS)

If you're using LS Cache or LiteSpeed Cache, you can achieve the same thing but it is much slower than other premium plugins. Just make sure you exclude Quic Cloud IP otherwise it will not work.

Best suited with LiteSpeed Server.

Process of Removing Unused CSS (manually) from the website:

Step 1: Get a baseline Performance score

To optimize our website, first, we need to get a performance score on this page, later, we can compare both the picture.

In this process, we are going to optimize the singer template, that I got from Envato Elements Plugins.

Page Speed Insights recommends in our website, we have over 100KB of CSS from there more than 70KB is unused CSS

Accordingly, to Google PageSpeed Insights, I can save over 60 KB of CSS, but your CSS size may differ from mine.

Step 2: Open Chrome Developer tools & start capturing coverage Tab

For example, if you combine or merge CSS in your performance plugins then it is not easy to identify which CSS is in use or not, there are 2 ways to solve this issue –

  1. Remove page or post from caching this is one of the most common and the safest way. After you optimized the post or the page was re-added again. or if you’re using HTTP/2 Protocol, do not combine your stylesheets in the first place.
  2. By adding ?nocache or any string to the end of your URL to bypass cache. (This might serve uncached version and old data or it might not serve either) (Not all the plugin supports this)

Step 2(A): Right-click anywhere to open Inspect Elements

Watch the video properly, it covers tills #3 Identify Unused CSS problems from the Coverage tab.

One of the easiest ways to open Developer tools is by right-clicking on any place with your mouse and clicking Inspect to open the Dev tools Inspect Elements panel. 

Right-click on any place and click the Inspect elements to open the elements panel

Step 2(B): Click the Customize and Open DevsTools Menu (3 dots) in Chrome Dev Tools

Click the 3 dots (Customize and Control settings) to open its settings

Step 2(C): Coverage Report

You can access the “Coverage Report” by clicking or hovering over the “More Tools” option.

Dev tools Coverage report is access by hovering over the more tools

  1. Click the 3 dots (Customize and Dev tools)
  2. More tools
  3. Coverage report to start capturing network activities

Then, click the reload button and start capturing coverage.

After you open Coverage reports you need to click the Reload button to Capture any dead code (unused CSS) ship by your theme or plugins

I have tested this page without enabling Improved CSS Loading Elementor’s features in the settings tab. That is why the CSS size is huge. This is specific to Elementor’s users only.

Disabled:

A code coverage report.

UPDATE: Improved Asset & CSS loading features are stable and you can enable them on every website. If it breaks follow my article on why Elementor Menu is broken.

After I Enabled the features the page Size relatively reduced a lot compared to the disabled but there are still lots of unused CSS that can be removed (see the Red color).

Picture showing difference between after enabling and disabling Elementor's experiments

Step 3: Identify Unused CSS problems from the Coverage tab

Click the reload button to start capturing coverage. Next to the URL Filter on the right side, there is an option to choose from, from there choose filter coverage by type to CSS.

So it will show only CSS files.

Choose CSS as filter type from dropdown lists

The red color of the bar indicates unused bytes and the greenish color indicates used bytes.

The red border with 100% unused bytes are safe to remove which means CSS is not used anywhere

You will get all the information like shortcuts, tips everything from Chrome Dev tools Coverage.

Step 4: Test before you start removing it

Testing is crucial, We figure out which stylesheets are not in use from the usage visualization panel above but we can’t be 100% sure whether the file we’re removing is not going to break our style.

To safely remove with confidence, we have to head over to the Chrome Dev tools – Network Panel and then right-click on the stylesheet you want to remove (100% one), then click the Block request URL this will open the request blocking tab (shown on the picture below) which lets you block requests you don’t want to load and then refresh the page and see if it doesn’t break. You do this process over and over.

TIPS: The request Blocking feature is also available in the Webpagetest and it is currently in Beta Stage.

Request Blocking in action

Block Request URL features in Chrome Dev tools

Request Blocking in action

Request blocking panel showing which CSS or JS will be blocked when the next page load

Install the Asset CleanUp plugin and start unloading stylesheets

To remove unused CSS, we have to install the Asset clean-up plugin from WP Repository.

We have already figured out which stylesheets can be removed 100% safely with request blocking and coverage reports in Chrome Dev tools. Now we need to install this plugin so we can remove unused CSS and JavaScript code from the entire website or per page/post basis. You can do the same thing with other asset management plugins too.

Now go to the page/post you’re removing and toggle the “Unload on this page button” to unload CSS from loading or “unload site-wide” (be careful though this will remove the stylesheet everywhere).

Asset clean up removal process

Removing unused JS

This CSS method can be applied in removing unused JS too but it is tricky and it can break your site functionalities because of dependencies.

For some reason, If you’re removing JS I would suggest you enable the ‘Ignore dependency rule and keep the “children” loaded’ input box (See the screenshot below) whenever needed. Otherwise, all the child files (frontend-modules and elementor-waypoints) will not load. or If you do not want to load any JS at all, unload jQuery. (Do it at your own risk)

Watch out for Dependencies

After Unloading stylesheets & JavaScript

Results after unloading CSS with Asset clean up plugin ( picture show – Before and after results)

Before we remove unused CSS
Before
Picture showing after removing unused CSS Problem with Asset clean up and saw 20% improvement
after
We have saved more than 20 KB of transfer size just by unloading stylesheets with the help of the asset clean-up plugin. If you’re using lots of 3rd parties plugins or add-ons that add styling to your page, then you may be able to reduce more.

The drawback of removing unused CSS manually:

  1. It is a time-consuming process.
  2. Removing unused CSS is not worth it if you constantly updating your pages. If the style is updated, you must redo everything again from the start unless you use the same CSS class or an ID.
  3. If JavaScript dynamically injects styles or CSS classes are present, then this Chrome extension will fail to extract the used CSS. (I will explain how to include them later)
  4. Great for smaller websites like single-page websites and landing pages but not for larger websites such as News websites.

(Advanced Method) How to reduce unused CSS completely

In the next step, we are going to learn how to remove unused CSS completely in WordPress with the help of the Chrome extension.
This doesn’t include the inline style added by the theme, author, and plugins.

Step 1: Install the CSS Used Chrome Extension from the Chrome Web Store

To remove unused CSS problems completely, we have to install CSS Used Chrome Extension from the Chrome extension Store. After we saved more than 20 KB of file sizes just by unloading stylesheets with the help of the Asset CleanUp plugin, we still have lots of unused CSS remains that can be removed completely. With the help of the Used CSS Chrome extension, we will remove all the unused CSS, even if there is dynamic CSS injected by JavaScript. While I was researching how to remove unused CSS from Elementor myself, I stumbled upon StackOverflow channels (If I found the URL I will list it here). The Chrome Extension works 100% on static pages but lacks CSS classes that are dynamically injected by JavaScript like the Laura Brehm Hamburger’s menu pop-up triggering by interacting with it.

Step 2: Extract Used CSS using Used CSS Chrome Extension

After we have installed the Chrome extension. If you’re in Dev tools, make sure you refresh the page. Now open Elements Panel [Follow Step#2(A)] and this will open the DOM tree from there click the HTML tag, this will grab everything, and click the CSS Used tab on the right side of the panel. (see the attachment below for reference) After you click the CSS used tab, it will go through all the processes and extract the used CSS from the page but the problem with this method is that it misses the Dynamic CSS that is injected by the JavaScript. To fix these problems, we have to interact with the layout in this example it is by interacting with the Hamburger menu and opening the menu (See it in the next step). Extracting CSS with used CSS chrome extension
  1. Open Elements Panel
  2. Click the HTML tag.
  3. Click the CSS used Tab next to the Accessibility tag
  4. Click the Preview button to open in the New tab or click the copy button to copy CSS and upload it to your server.

Step 3: Include Dynamic style, CSS Classes, and IDs to the extracted CSS

To Detect dynamically injected CSS classes or IDs by ‘Used CSS chrome extension“, we need to first interact with the layout itself, in my case, it will be by interacting with the hamburger menu.

Caution: (If you have 1000 posts this method will take you more than a week or more to do which is not a great experience for you who use the Automatic process listed above)

To capture all the CSS, make sure you interact with the page and open it like the opening menu (See the screenshot below) and this is what I did:
  1. I interact with the Hamburger menu this way Elementor Dialog JS will inject the CSS.
  2. Click the HTML tag and then click the CSS Used tab to generate used CSS this will include the CSS injected by JS.
Detect dynamically injected CSS classes with Used CSS chrome extension. First, click the Elements panel then select HTML from DOM CSS used will automatically generate CSS
  1. Click the Hamburger Menu (icon)
  2. On the right side, click the Elements panels
  3. Click the <html tag in the DOM tree.
  4. Click the CSS Used, it will scan and generate all the used CSS
  5. Now, you can copy the code and further optimize it or see the design by clicking the preview button or viewing your website on CodePen.

Step 4: Copy the minified code to your Code editor and save it

By default, the Used CSS Chrome extension will generate the minified code, this will include inline style generated by your theme, plugins, or hardcoded CSS/ author style. These CSS can safely ignore it unless you have the pro version of the Asset CleanUp plugin which has the feature to remove it. You can remove the inline style generated by your theme or plugins because we can’t remove it. Next, un-minify the code with the online free tools or use the prettier Visual Studio code and then save the minified file as a .css file format. You can upload the file or copy the code to your server with the help of FTP or by accessing the control panel (cPanel). Choose your preferred Code editor and past the code and save it as .css file
Tips: If you’re using Visual Studio (VS) code, you can use a prettier extension to minify by entering F1 on your keyboards using Online tools to minify it, or using the Beautify VS Code extension to un-minify the code.

Log in to your cPanel and upload the file

I use Runcloud as my control panel so my process of accessing the control panel will be different. But our goal is the same, upload the stylesheet to the wp-content folder. In this step, we are going to upload a file or create a .css file in the wp_content folder in the file manager or upload it using the FTP client.

Step 1: Log in to your control panel (cPanel)

Runcloud Login

Step 2: Select your Server

Select your server

Step 3: Head over to Web Application and choose your root folder

Now head over to the Web Application area and choose your root. i.e., Domain Choose Domain as root folder

Step 4: Open File Manager

Choose File Manager

Step 5: Scroll down and find and open the wp-content folder

After you open File Manager, you need to scroll down to the end and find the wp-content folder. This folder contains everything like your media, Plugins, Themes, cache, etc Find and Choose WP Content

Step 6: Create a new folder or use the existing folder

After we open the wp-content folder there will be an existing folder, if you’re using any Performance plugins like Swift or wp-rocket then there is a chance it will create a folder automatically for you called cache. I am just using it for demonstration purposes only (change it to another location and create a folder instead). Click the Cache folder and inside the cache folder, create your folder (combine-assets), this is the folder where you will add all your stylesheets or JavaScript. It is up to you to choose the folder path, you can also choose the WP Content ‘assets folder‘ rather than the ‘cache folder‘. Click the Cache folder to create a new folder inside the folder Inside the Cache folder, there are many existing folders and we have to create another folder of our own.

Step 7: Create a .css file inside the combine-assets (change if needed) folder

Unfortunately, Runcloud does not allow us to upload assets directly through their Control Panel but it is possible through FTP clients like FileZilla. Since the size of the CSS is small. So for me, it is to create a file and copy & paste the minified code. After you have created a folder ( example – combine-assets), you need to create a file-name.css file that will store our extracted CSS from number #9 Select the file from the Dropdown menu and Create new file, we can later add our CSS Give a file name (for example – file.min.css) and save it. Give a file name to your Stylesheets file

Step 8: Open the file and paste the extracted CSS from your code editor

Open the file and copy all the CSS ( Ctrl + A and Ctrl + C) from your code editor paste it inside the File editor (laura-brehm.css file) and save it. Paste your CSS inside the file Or you can install the File Manager plugin from WP Repository or use FileZilla to upload your file. After you upload your file copy and paste the code as I did. We need to install the Code Snippets plugin available in the WordPress repository or use a child theme.

Install Code Snippets plugins and enqueue the stylesheet

We have uploaded the stylesheets to our wp-content folder, Now we need to load the stylesheet by using the wp_enqueue style function.

Step 1: Install the Code Snippets plugin-

Now install the Code Snippet Plugin from the WP Repository and activate the plugin or use the Child Theme function.php file. Download The Code snippets plugin from WP Repository

Step 2: Click the Add new Snippet and enqueue the stylesheet-

Now use the wp_enqueue_style function to enqueue (load) your stylesheet. picture showing how to load or enqueue the stylesheets to the specific post using PHP

/*
  * I use Code Snippet plugin so opening php tag and closing is not needed
  * Change the post ID with respected post ID
  * later if you're updating the code, simply update the version from 1.1 to 1.2 
*/

function laura_brehm() {
  /* 1978 is the post type referring to the template page like your-domain.com/wp-admin/post.php?post=1978*/
  if( is_page ( 1978 )) {
   /*Enqueue Stylesheets properly */
   wp_enqueue_style('laura-brehm','/wp-content/cache/combine-assets/laurabrehm.min.css', array(), '1.0', 'all');
  }
}
add_action('wp_enqueue_scripts', 'laura_brehm');
    
  

Unload all the stylesheets using the Asset CleanUp plugin

After you enqueue the stylesheet with the Code Snippet plugin. Make sure you check it using the Asset CleanUp options and then click the “Unload on this page” toggle on all the stylesheets to remove/dequeue/unload it.

If you want to exclude some stylesheets like global stuff make sure you click the Ignore dependency rule and keep the “childrenThis also applies to stylesheets.

To completely remove all the stylesheets, we have to go to post or page and click the unload on this page button except the stylesheets we enqueue

  1. WP Speed Matters
  2. web.dev – Fast Load Times

14 Responses

  1. Best article I´ve found that explain how to unload unused CSS from WordPress websites and, to be honest, I´ve found and read over 40 articles about this topic!

    Great job!!

  2. Good video…nice and clear.
    I read the kinsta article, any recommendations specifically for elementor?
    The reasoning behind moving js to the header was to only move what was used as some of the large elementor js scripts only have 5% coverage.

  3. Good article…I’ve been using Dev Tools and Asset Cleanup to remove unwanted CSS/ js but wanted to take it a step further as there are lots of css/ js files which are more than 90% unused. Can you use the same method to move js code into the header?
    I also made a big impact by creating my own icon subset file, preloading it in local fonts in Asset Cleanup and then disabling Font Awesome, Google Fonts & Eicons in code snippets – although you need to override the hamburger menu icon.

  4. That is actually my main concern, JQuery. Assuming my only use of JQuery was in a mobile toggle, wont it be faster to use vanilla JS as opposed to loading the whole JQuery library?

    1. A great point to Disqus but Elementor frontend.min.js and other JS (free and Pro) are depending on JQuery which make it hard to remove.

      If you’re using Asset clean up plugin that I recommended then there are a handful of JavaScript can be removed i.e. font-awesome-4-shim ( FontAwesome 4 backwards compatibility), share-link ( Social share on Lightbox), swiper ( Elementor slider), elementor-sticky ( for the sticky header) etc [To properly disable those JavaScript You need to enable Ignore dependency rule and keep the “children” loaded (recommended)]

Leave a Reply

Your email address will not be published. Required fields are marked *