A Node.js tool that removes unused CSS from stylesheets by analyzing HTML files and executing JavaScript.
UnCSS is a Node.js tool that removes unused CSS from stylesheets to reduce file size and improve website performance. It works by analyzing HTML files, executing JavaScript, and identifying which CSS selectors are actually used on the page. The tool helps developers clean up their CSS by eliminating dead code that accumulates from frameworks and libraries.
Frontend developers and build engineers working on web projects who need to optimize CSS delivery and reduce page load times. It's particularly useful for projects using CSS frameworks like Bootstrap where only a subset of styles is utilized.
Developers choose UnCSS because it automates the tedious process of manually identifying unused CSS, integrates seamlessly into build pipelines, and supports JavaScript-rendered content, making it more accurate than static analysis tools.
Remove unused styles from CSS
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses jsdom to simulate a browser and execute JavaScript on page load, capturing injected CSS from frameworks like React or Angular, making it more accurate for modern web apps.
Offers plugins for Grunt, Gulp, and PostCSS, allowing seamless automation in CI/CD pipelines, as evidenced by the detailed usage examples in the README.
Provides ignore options and CSS comments (e.g., /* uncss:ignore */) to preserve selectors for dynamic styles, helping avoid false positives from user interactions.
Processes stylesheets with specific media queries beyond default ones, ensuring responsive design rules are properly analyzed and retained during optimization.
Cannot handle CSS added after page load by JavaScript (e.g., on hover or click), requiring manual ignore lists which the README admits can lead to configuration errors.
Requires generating static HTML files from templates or running a local dev server, adding extra steps compared to tools that work directly on source code.
Using jsdom to execute JavaScript can be slow for large websites, increasing build times significantly, which is a trade-off for accuracy.
If HTML files don't cover all usage scenarios, it may incorrectly remove used CSS, leading to styling bugs that require thorough testing to catch.