Make CSS changes go live immediately

Browsers will not always apply CSS changes right away. Sometimes the end user will have to refresh the page to force the browser to dump and rebuild the cache. This isn’t a massive problem during development, but once the site goes into production it can become problematic. Here are two solutions I use to make sure end-users see CSS changes right away without any page refreshing.

The first example is simple and can be used with any site. The second example will be for a WordPress theme.

Example One

Chances are the call to your CSS file looks something similar to this:

If you want browsers to dump the old CSS file and load up the new/modified CSS file, simply add a version to the CSS file. It should look something like this:

So, every time you make a change, just up the version. This will force the browser to redownload the CSS file and apply all changes. Very easy, and no more “please refresh your page” conversations with clients.

Example Two – For a WordPress Theme

The top of your style.css file in your theme folder must start with a comment like this:

You can learn more about this required comment at the WordPress Codex. You will notice that there is already a theme version located in this file. When you make updates to your theme, you should always update the version number.

So what we want to do is automatically pull the version number from your style.css into the CSS call located in the <head> of your site. So, change the code from:

To:

This will ensure whenever you update the Version line in style.css the version will automatically change in the CSS call. This will cause the all CSS files stored locally to recache. Your audience will always see CSS changes right away without refreshing the page or clearing the browser cache.

 

2 thoughts on “Make CSS changes go live immediately

Leave a Reply

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