Critical CSS

What is Critical CSS?

To make your web pages pretty, you (or your theme developer) uses CSS. These are "style rules" that are applied to various bits of content on your page. The "critical" CSS is the set of rules that apply to the portion of the page that is visible without scrolling (or any other user interaction).

Why do I need Critical CSS?

By default, CSS files (style sheets) are render-blocking. In other words, the browser won't display the page until all the CSS is loaded, because it needs that CSS to know how to display the content. That is usually bad for performance, but when you defer CSS (making it asynchronous), it can cause a FOUC. What’s a FOUC??? It’s the dreaded “Flash Of Unstyled Content”. When CSS is deferred, your page will render un-styled initially, and then style rules will be applied as the CSS files load.

While that might be interesting to see (at first), it's not great for your visitors because it causes "layout shifting"--ever heard of CLS/Cumulative Layout Shift?. Things will move around as the style rules are applied, and this can lead to visitors clicking the wrong thing, getting frustrated, and possibly leaving your site entirely.

So to avoid FOUC and layout shifting, we inline our Critical CSS right in the page, and that way the page can be displayed (correctly) super fast, and then the browser can go download the files needed to display the rest of the page.

How do I find the Critical CSS?

Ah, now we're getting somewhere! While there are some free tools that can do this for individual pages, we built Critical CSS generation directly into SWIS, and it allows you to get page-specific critical CSS with the click of a button.

To get started, enter your API key in the SWIS settings (requires SWIS Performance 2.3+). Once you save the settings, you'll see a button in the sidebar to Generate Critical CSS. SWIS will then scan your site and start generating the critical CSS. Once it's complete, it will purge the page cache automatically to start using the critical CSS. Of course, if you use a third-party caching plugin or server-based cache, be sure to clear it when the process is complete.

Manage Critical CSS

You can browse to any page on your site to mange the Critical CSS. Look for the SWIS menu on the admin bar, and click Manage JS/CSS. You'll see the Critical CSS section right at the top. If you're using the Critical CSS API, you should see something like this:

This allows you to regenerate the CSS if needed, edit the CSS, and also use the Test CSS button to see what the page looks like with only the inlined critical CSS (no external CSS will be loaded on the test URL).

SWIS will not generate critical CSS for every web page. For example, it will only generate CSS for a single blog post, and then use that on all blog posts. The same applies to custom post types, and date/category/tag archives. But if you want SWIS to generate page-specific critical CSS, you can do that easily:

You can also add/edit page-specific critical CSS manually (like if you're using one of the online generators linked above):

Have more questions? Let us know!

Still need help? Contact Us Contact Us