Skip to main content
All CollectionsSupplementary GuidesWordPressGeneral
Consolidating External CSS Files in WordPress
Consolidating External CSS Files in WordPress
Rapyd Team avatar
Written by Rapyd Team
Updated over a week ago

Consolidating external CSS files in WordPress improves website performance and user experience by optimizing load times and minimizing bandwidth usage.

What are CSS Files?

CSS files, also known as Cascading Style Sheets files, are plain text files that contain code used to define the visual presentation and formatting of a website or web application. CSS files work in conjunction with HTML files to control the appearance and layout of web pages.

They specify how elements such as text, images, and other HTML components should be displayed, including aspects like colors, fonts, spacing, borders, and more. By separating the presentation layer (CSS) from the content layer (HTML), CSS files enable efficient and consistent styling across multiple web pages.

Why Consolidate External CSS Files?

1. Improved Page Load Time:

  • Combining multiple CSS files into a single file reduces the number of HTTP requests required to load a web page, resulting in faster load times.

  • The browser only needs to fetch and process one consolidated CSS file, reducing network latency and improving overall performance.

2. Reduced Bandwidth Usage:

  • By minimizing the number of separate CSS files, the total file size transferred from the server to the browser is reduced, resulting in lower bandwidth consumption.

  • This is particularly beneficial for users with limited or slow internet connections.

3. Easier Maintenance:

  • Consolidating CSS files simplifies the management and maintenance of stylesheets.

  • Instead of modifying multiple files, changes can be made in a single consolidated CSS file, making updates and troubleshooting more efficient.

4. Minimized Render Blocking:

  • When CSS files are combined, the browser can start rendering the web page without waiting for the additional CSS files to load, reducing render-blocking delays.

  • This helps improve the perceived speed and user experience of the website.

5. Enhanced Caching:

  • A single consolidated CSS file is more likely to be cached by the browser, allowing subsequent visits to the website to load faster.

  • Caching reduces the need for the browser to re-download the CSS file, improving overall performance.


Steps to Consolidate External CSS Files in WordPress Manually

Step 1: Identify the CSS Files

The first step to consolidating CSS files is to identify them. These files are typically linked in the <head> section of your site's HTML code. Open the Source Code of your website and search for .css to locate all the CSS files.

Step 2: Combine the CSS Files

Once you've identified all the CSS files, the next step is to combine them into one single file. This process requires a careful approach, as the order of the CSS rules can impact how they're applied.

Create a new CSS file, perhaps named combined.css, in your active theme's folder. Then, copy the contents of each CSS file, in the order they appear on your site, into the combined.css file.

Step 3: Enqueue the New CSS File

Now you need to tell WordPress to use your new combined.css file instead of the individual CSS files. You do this by enqueuing the CSS file in your functions.php file.

Here's how to enqueue a CSS file in WordPress:

PHP

function mytheme_enqueue_styles() {

wp_enqueue_style( 'combined-css', get_stylesheet_directory_uri() . '/combined.css', array(), '1.0.0' );

}

add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' );


The above function links the new CSS file to your WordPress site.


Step 4: Remove the Old CSS Files

The last step is to deregister or dequeue the old CSS files to stop WordPress from loading them. This process is done using the wp_dequeue_style or wp_deregister_style functions.

Here's an example of how to do this:


PHP

function mytheme_dequeue_styles() {

wp_dequeue_style( 'old-css-handle' );

wp_deregister_style( 'old-css-handle' );

}

add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_styles', 20 );

Make sure to replace 'old-css-handle' with the actual handle of the CSS file you want to remove.

Using Plugins to Consolidate CSS

Utilizing WordPress plugins is an excellent alternative to manual consolidation of CSS files, particularly for those who may not be comfortable handling code. There are several WordPress plugins designed to consolidate, minify, and compress CSS files, thus improving site performance. Here, we'll delve into three popular plugins: Autoptimize, WP Super Minify, and Fast Velocity Minify.

Autoptimize

Autoptimize is a powerful optimization plugin that can help consolidate CSS files. It not only minifies and caches scripts and styles, but also injects CSS into the page head by default and moves and defers scripts to the footer.

How to Use Autoptimize

Installation:

Install and activate the Autoptimize plugin from your WordPress dashboard by going to Plugins" > "Add New. Search for Autoptimize, install, and activate it.

Configuration:

Go to Settings > Autoptimize to configure the plugin. Tick the Optimize CSS Code? option to enable CSS consolidation.

Advanced Settings:

In the same settings page, under the "Advanced Settings" tab, you can tick the "Aggregate CSS-files?" option. This option consolidates all CSS into one file.

Click Save Changes and Empty Cache.

WP Super Minify

WP Super Minify, as the name suggests, combines, minifies, and caches inline JavaScript and CSS files on demand to speed up page loads.

How to Use WP Super Minify:

WP Super Minify, as the name suggests, combines, minifies, and caches inline JavaScript and CSS files on demand to speed up page loads.

Installation:

From your WordPress dashboard, go to Plugins > Add New. Search for "WP Super Minify", install and activate it.

Enable Minification:

Go to Settings > WP Super Minify. Check the enable the Compress CSS box.

Save: Click Save to apply the changes.

Fast Velocity Minify

Fast Velocity Minify takes your site's CSS and JS files, merges them into groups, minifies them and caches them. It helps reduce HTTP requests while simultaneously serving minified versions of your pages, thereby improving your website's load time.

How to Use Fast Velocity Minify:

Installation:

Install and activate Fast Velocity Minify by going to Plugins > Add New in your WordPress dashboard. Search for Fast Velocity Minify, install, and activate it.

Use Default Settings:

Fast Velocity Minify works well with its default settings in most cases. However, if needed, you can customize the settings by going to Settings > Fast Velocity Minify.

On the plugin's landing page, scroll to the Advanced CSS Option, mark the checkbox for Combine CSS Files and then click Save Changes at the bottom of the page.

These plugins can automatically consolidate your WordPress site's CSS files, increasing your website's speed and performance. However, always ensure to backup your website before installing a new plugin or modifying your website.

Conclusion

The consolidation of external CSS files is a valuable practice that contributes significantly to improving the performance and speed of your WordPress site. Whether you opt for manual consolidation or use automated solutions like plugins, the result is a streamlined, efficient site that offers an enhanced user experience and could boost your SEO rankings. It's important to remember to back up your website before making changes and carefully monitor the results, especially when installing new plugins, to ensure your website continues to function optimally.

Did this answer your question?