PurgeCSS
PurgeCSS is a tool to remove unused CSS from your stylesheets. It analyzes your content and CSS files to determine which selectors are actually used, then removes the unused ones to reduce file size.
Usage
PurgeCSS is a CSS processor that can be enabled from the style editor menu, or configured via the processors property of the configuration object.
When enabled, PurgeCSS analyzes the compiled HTML and JavaScript code in your project to identify used CSS selectors, then removes any unused styles from the CSS output.
PurgeCSS can be used alongside other processors. Multiple processors are applied in the order they appear in the style editor menu.
Demo
show code
- JS
- TS
- React
- Vue
- Svelte
- Solid
- Preact
- Web Components
import { createPlayground } from 'livecodes';
const options = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
return (<LiveCodes {...options} />);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import LiveCodes from 'livecodes/svelte';
const options = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
</script>
<LiveCodes {...options} />
import LiveCodes from 'livecodes/solid';
export default function App() {
const options = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
return (<LiveCodes {...options} />);
}
import LiveCodes from 'livecodes/preact';
export default function App() {
const options = {
"config": {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
}
};
return (<LiveCodes {...options} />);
}
<live-codes></live-codes>
<script type="module">
import "livecodes/web-components";
const playground = document.querySelector("live-codes");
playground.config = {
"activeEditor": "style",
"markup": {
"language": "html",
"content": "<div class=\"container\">\n <h1>PurgeCSS Demo</h1>\n <p>Only the <strong>used</strong> styles are kept. Check the <strong>Compiled</strong> tab to see the purged output.</p>\n <button>Primary Button</button>\n</div>\n"
},
"style": {
"language": "css",
"content": "/* used in HTML */\n.container {\n font-family: sans-serif;\n max-width: 600px;\n margin: 2em auto;\n}\n\n/* used in JS */\n.btn-primary {\n background: #4a90d9;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n\n/* not used */\n.btn-secondary {\n background: #6c757d;\n color: white;\n border: none;\n padding: 10px 20px;\n border-radius: 4px;\n}\n.unused-class {\n color: red;\n font-size: 2em;\n}\n"
},
"script": {
"language": "js",
"content": "const btn = document.querySelector('button');\nbtn.classList.add('btn-primary');\n"
},
"processors": [
"purgecss"
],
"tools": {
"active": "compiled",
"status": "open"
}
};
</script>
Processor Info
Name
purgecss
Editor
style
Processor
Version
@fullhuman/postcss-purgecss: v7.0.2
Custom Settings
Custom settings added to the property purgecss are passed as options to PurgeCSS. Please check the documentation for full reference.
Please note that custom settings should be valid JSON (i.e. functions are not allowed).
Example:
{
"purgecss": {
"safelist": ["unused-class"]
}
}
Links
- PurgeCSS
- PurgeCSS on GitHub
- CSS Processors in LiveCodes