Search by

parallelogram / craft-criticalcss

peptolab

Critical CSS for Craft CMS without third-party dependencies: inline a stylesheet in the <head>, load the full one without blocking, and inline CSS into emails.

Package info

github.com/parallelogram-dev/parallelogram-craft-criticalcss

Documentation

Type:craft-plugin

pkg:composer/parallelogram/craft-criticalcss

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0-rc1 2026-12-03 01:58 UTC

This package is auto-updated.

Last update: 2026-12-03 02:04:14 UTC


README

Critical CSS for Craft CMS 5 with no dependencies beyond Craft: print a stylesheet into the page's <head>, load the full one without holding the page up, and inline CSS into emails.

It replaces Style Inliner as a drop-in. Its tags, paths and settings are the same, but it doesn't pull in tijsverkoyen/css-to-inline-styles, Symfony's CSS selector or its own Guzzle and PHP ranges, which can stop Composer upgrading Craft.

Requirements

  • Craft CMS 5.6 or later
  • PHP 8.3 or later

Templates

{# In the <head>: the page's critical CSS, once per file per page #}
{% criticalcss 'asset/css/inline' %}

{# The full stylesheet, loaded without blocking rendering #}
{% deferredcss 'asset/css/app' %}

{# Where a tag can't go: the same CSS as a <style> element #}
{{ craft.criticalCss.print('asset/css/inline')|raw }}

{# Emails: CSS moved into style attributes #}
{% inlinecss %}
  <html><head><style>p { color: #333 }</style></head><body><p>Hello</p></body></html>
{% endinlinecss %}

File names start at the Where CSS files are setting (@webroot/ by default) and get .css added, so 'asset/css/inline' reads web/asset/css/inline.css.

  • {% criticalcss %} puts the file in the page's head through Craft's view. Each file is output once per page, however many templates ask for it. It's read once per change: the contents are cached under the file's modified time.
  • {% deferredcss %} writes <link rel="preload" as="style"> that becomes a stylesheet when it loads, plus a <noscript> link. The URL starts at Where full stylesheets load from (@web/ by default, or a CDN) and carries ?v= with the file's modified time. With critical CSS turned off it's an ordinary stylesheet link, so the page is never shown unstyled.
  • {% inlinecss %} moves the block's <style> CSS into style attributes, following the cascade: !important first, then the element's own style, then specificity, then source order. It understands type selectors and *, #id, .class, [attr], [attr=value], :first-child and :last-child, joined by spaces and >. Anything else, such as media queries, :hover and other selectors, stays in one <style> block in the head, which most mail clients honour.

A missing file is logged. In dev mode the page also gets an HTML comment naming it.

Settings

Setting Default
criticalCss true Off, {% criticalcss %} outputs nothing and {% deferredcss %} links stylesheets normally.
criticalPrefix @webroot/ Where file names start.
deferredBaseUrl @web/ Where {% deferredcss %} URLs start.

Set them in the control panel or in config/p-criticalcss.php, which can differ per environment. The settings page says when the file sets one.

Switching from Style Inliner

  1. Install this plugin. While Style Inliner is enabled, it leaves the tags to it, because Twig can't register a tag twice.
  2. Run php craft p-migrator/style-inliner/plan (from Parallelogram Migrator) to see what will happen, then php craft p-migrator/style-inliner/import. It carries Style Inliner's settings over and uninstalls it. Settings in config/style-inliner.php are reported, to copy into config/p-criticalcss.php.
  3. composer remove enovatedesign/craft-style-inliner.

craft.styleinliner.printcriticalcss() keeps working while templates are updated.

Development

composer check

The Craft suite runs from the sandbox project: vendor/bin/phpunit -c vendor/parallelogram/craft-criticalcss/phpunit.craft.xml, after test/Craft/install-test-db.sh.

Licence

The Craft Licence. See LICENSE.md.