parallelogram / craft-criticalcss
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
Type:craft-plugin
pkg:composer/parallelogram/craft-criticalcss
Requires
- php: ^8.3
- ext-dom: *
- ext-mbstring: *
- craftcms/cms: ^5.6
- parallelogram/craft-kit: ^0.2 || *@dev
Requires (Dev)
- carthage-software/mago: ^1.59
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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 intostyleattributes, following the cascade:!importantfirst, then the element's ownstyle, then specificity, then source order. It understands type selectors and*,#id,.class,[attr],[attr=value],:first-childand:last-child, joined by spaces and>. Anything else, such as media queries,:hoverand 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
- Install this plugin. While Style Inliner is enabled, it leaves the tags to it, because Twig can't register a tag twice.
- Run
php craft p-migrator/style-inliner/plan(from Parallelogram Migrator) to see what will happen, thenphp craft p-migrator/style-inliner/import. It carries Style Inliner's settings over and uninstalls it. Settings inconfig/style-inliner.phpare reported, to copy intoconfig/p-criticalcss.php. 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.