This module automatically scans your Drupal site and collects all CSS classes used in database-driven content. It generates a list of actually used CSS classes, which is useful for CSS optimization workflows that require accurate safelists to prevent the removal of dynamic or backend-generated classes.
Remove unused CSS from your theme, without breaking the pages your editors built.
Tools like PurgeCSS shrink big CSS files (Bootstrap, Tailwind, a design system) by keeping only the classes your theme uses. They find those classes by reading your templates and JavaScript. But they cannot see the classes your editors type into content: an alert alert-info box in a body field, a Bootstrap grid in a paragraph, a button class in a custom block. Those styles get purged, and content breaks in production.
CSS Class Collector fixes that. It keeps a list of every CSS class used in your Drupal content and exports it as a CSV safelist, so those classes are always kept.
All collected classes in one place, under Reports » Collected CSS classes, ready to export.
Why use it
- Smaller CSS, faster pages. Purge hundreds of kilobytes of unused framework CSS with confidence, and improve your Core Web Vitals.
- No broken content. Classes that only exist in the database are on the safelist, so editors' layouts keep their styles.
- Always up to date. Content is collected every time it is saved; you never maintain the safelist by hand.
- Works with any purge tool. The export is a plain CSV: use it with PurgeCSS, Tailwind's safelist, or your own build step.
What it collects
- Content: the long text fields (formatted and plain) of nodes, paragraphs and custom blocks, in every language, each time they are saved.
- Views: the output of each view display on the front end (at most once a day per display, so pages are not slowed down).
- Blocks rendered with markup on the front end.
- Everything that already exists, with one click on Run full scan: all content plus every enabled view display, rendered with your front-end theme.
Admin pages are ignored, so admin theme classes stay out of the list, and classes generated for a single render (such as js-view-dom-id-…) are skipped.
Run the full scan once after installing the module, or after a content import.
How to use it
- Install:
composer require drupal/css_class_collectoranddrush en css_class_collector. - Give the Administer CSS Class Collector permission to your site builders.
- Go to Reports » Collected CSS classes and click Run full scan.
- Click Export CSV to download
css-classes.csv. - Use the CSV as the safelist of your CSS purge step.
Example: purge Bootstrap with PurgeCSS
The module includes help-file/build-css.js, an example for a Bootstrap theme (for example a bootstrap_barrio subtheme):
- Copy
build-css.jsinto your theme, next to yourbootstrap.min.css, and put the exportedcss-classes.csvnext to it. - Install the tools:
npm install purgecss glob - Run
node build-css.js
It reads your Twig templates and JavaScript, keeps every class from the CSV plus a few dynamic Bootstrap patterns (col-*, btn-*, modal-*…), and writes bootstrap-purge.min.css. Load that file in your theme instead of the full Bootstrap. Adjust the paths and patterns to your theme.
Requirements
- Drupal 10.3, 11 or 12, with Views.
- Paragraphs and Custom Block are optional: they are scanned when they are enabled.
Questions and ideas are welcome in the issue queue.
Ask your assistant about Css Class Collector (Purge CSS)
Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works
Depends on
Dependencies of the latest stable release
- views Drupal core
Required by
Tracked projects that depend on this one
No tracked projects depend on this one yet.