code-distortion/relevant-css is a PHP library that parses your css, analyses your html, and builds custom css with only the necessary definitions.
If you'd like to use this in Laravel, have a look at code-distortion/laravel-relevant-css which integrates this package neatly into Laravel and the Blade templating system.
This package was inspired by PurgeCss.
use CodeDistortion\RelCss\RelevantCss;
$outputCss = RelevantCss::new()
->cssFile('my.css') // specify a source css file
->fileNeedsCss('my.html') // an html file that needs custom styles
->output(); // generate the custom css
print $outputCss;
/*
html{line-height:1.15;-webkit-text-size-adjust:100%}
body{margin:0}
input{font-family:inherit;font-size:100%;line-height:1.15;margin:0}
… etc
*/
Install the package via composer:
composer require code-distortion/relevant-css
use CodeDistortion\RelCss\RelevantCss;
$relevantCss = new RelevantCss();
// or
$relevantCss = RelevantCss::new(); // this is neater to chain with
First specify the css definitions that can be used. More than one source can be specified.
You can specify css files:
$relevantCss->cssFile('my.css');
or pass definitions directly as a string:
$cssDefinitions = <<<CSS
html { padding: 0; color: black; }
input { border: 1px solid grey; background-color: white; }
CSS;
$relevantCss->cssDefinitions($cssDefinitions);
Then specify the html that needs styling. More than one can be specified.
You can specify html files:
$relevantCss->fileNeedsCss('my.html');
Or pass the html directly as a string:
$relevantCss->contentNeedsCss('<input type="text"/>');
You may also specify css selectors to always include, regardless of whether they appear in the html or not:
$relevantCss->alwaysAddSelectors('.success .warning .error');
Then generate the custom css as a string ready to use:
$outputCss = $relevantCss->output();
It takes time to parse the css input and so to help improve this, RelevantCss will cache the css when you specify a cache-directory:
$relevantCss = RelevantCss::new('path/to/cache/dir');
By default RelevantCSS will access the filesystem directly when loading files and storing cache data. However you can change how the filesystem is accessed by passing a filesystem object when instantiating it. It must implement \CodeDistortion\RelCss\Filesystem\FilesystemInterface
.
// DirectFilesystem is the filesystem used by default
use CodeDistortion\RelCss\Filesystem\DirectFilesystem;
$relevantCss = RelevantCss::new('path/to/cache/dir', new DirectFilesystem());
composer test
Please see CHANGELOG for more information on what has changed recently.
This library uses SemVer 2.0.0 versioning. This means that changes to X
indicate a breaking change: 0.0.X
, 0.X.y
, X.y.z
. When this library changes to version 1.0.0, 2.0.0 and so forth it doesn't indicate that it's necessarily a notable release, it simply indicates that the changes were breaking.
You're free to use this package, but if it makes it to your production environment please plant or buy a tree for the world.
It's now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you support this package and contribute to the Treeware forest you'll be creating employment for local families and restoring wildlife habitats.
You can buy trees here offset.earth/treeware
Read more about Treeware at treeware.earth
Please see CONTRIBUTING for details.
Please see CODE_OF_CONDUCT for details.
If you discover any security related issues, please email tim@code-distortion.net instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.