https://github.com/jandc/css-from-html-extractor
PHP library which determines which css is used from html snippets.
https://github.com/jandc/css-from-html-extractor
css html-extractor php-library
Last synced: 11 months ago
JSON representation
PHP library which determines which css is used from html snippets.
- Host: GitHub
- URL: https://github.com/jandc/css-from-html-extractor
- Owner: JanDC
- License: isc
- Created: 2016-10-16T13:40:26.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T12:17:29.000Z (over 6 years ago)
- Last Synced: 2025-07-10T20:48:00.992Z (12 months ago)
- Topics: css, html-extractor, php-library
- Language: PHP
- Homepage:
- Size: 42 KB
- Stars: 9
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CSS from HTML extractor
Php library which determines which css is used from html snippets.
It is used in jandc/critical-css to automatically and dynamically determine critical css on a per page basis.
### Installation
``
composer require jandc/css-from-html-extractor
``
### Usage
#### With Twig
###### Register Extension
```php
use CSSFromHTMLExtractor\Twig\Extension as ExtractorExtension;
$extension = new ExtractorExtension()
$extension->addBaseRules('path/to/css');
/** @var Twig_Environment $twig */
$twig->addExtension($extension);
```
###### Mark the regions of your templates with the provided blocks
```twig
{% fold %}
...
{% endfold %}
```
###### Retrieve the resulting css from the extension
```php
$extension = $twigEnvironment->getExtension(ExtractorExtension::class);
$extension->buildCriticalCssFromSnippets();
```
#### Handling raw HTML
```php
$cssFromHTMLExtractor = new CssFromHTMLExtractor();
$cssFromHTMLExtractor->addBaseRules('path/to/css');
$cssFromHTMLExtractor->addHtmlToStore($rawHtml);
$extractedCss = $cssFromHTMLExtractor->buildExtractedRuleSet();
```