Skip to main content

Minify CSS and JavaScript

Minification removes unnecessary characters (whitespace, comments) from CSS and JavaScript files without changing functionality, reducing file sizes by 20-50%.

Why Minify?

Smaller files = faster downloads:

  • Typical CSS reduction: 20-40%
  • Typical JS reduction: 20-50%
  • Typical HTML reduction: 10-20%

Example:

// Original (220 bytes)
function calculateTotal(items) {
// Loop through items and sum prices
let total = 0;
for (let i = 0; i < items.length; i++) {
total += items[i].price;
}
return total;
}

// Minified (110 bytes)
function calculateTotal(items){let total=0;for(let i=0;i<items.length;i++)total+=items[i].price;return total;}

Types of Minification

Minify Inline CSS

Compresses <style> blocks embedded in HTML pages.

  • Enable in: File Optimization → Minify inline CSS
  • Best for: Pages with significant inline styles
  • Risk: Very low; CSS is only removed whitespace and comments

Minify Inline JavaScript

Compresses <script> blocks embedded in HTML pages.

  • Enable in: File Optimization → Minify inline JS
  • Best for: Pages with embedded scripts
  • Risk: Low; uses safe minification (variable names stay readable for inline code)

Minify HTML

Removes unnecessary whitespace and safe HTML comments from page HTML.

  • Enable in: File Optimization → Minify HTML
  • Best for: All sites
  • Risk: Very low; only whitespace removed, structure intact

Minify External CSS Files

Caches minified versions of enqueued stylesheets and serves those instead.

  • Enable in: File Optimization → Minify external CSS files
  • Best for: Sites with many stylesheets or large CSS files
  • Benefit: Permanent cache, avoid repeated minification
  • Risk: Medium; requires testing for CSS issues (use exclusions if needed)

Minify External JavaScript Files

Caches minified versions of enqueued JavaScript files.

  • Enable in: File Optimization → Minify external JS files
  • Best for: Sites with many scripts or large JS files
  • Benefit: Permanent cache
  • Risk: Medium; requires testing (exclude critical scripts if breaking)

Configuration

Setting Up Minification

  1. Go to Performance ToolkitFile Optimization
  2. Enable desired minification options:
    • ✅ Minify HTML output
    • ✅ Minify inline CSS
    • ✅ Minify inline JavaScript
    • ✅ Minify external CSS files
    • ✅ Minify external JavaScript files
  3. Click Save changes
  4. Test your site thoroughly

Exclusions for External Files

If external file minification causes issues, exclude specific files:

CSS Exclusions

Add one per line (by handle, filename, path, or wildcard):

woocommerce-layout
bootstrap.css
/wp-content/themes/*/css/critical.css

JavaScript Exclusions

jquery-core
analytics.js
/wp-content/plugins/gravity-forms/*

Testing Minification

After enabling minification:

  1. Test critical pages:

    • Home page
    • Blog/post pages
    • Product pages (if WooCommerce)
    • Checkout (if applicable)
    • Account/login pages
  2. Check console for errors:

    • Right-click → Inspect → Console tab
    • Look for red error messages
    • Repeat on different pages
  3. Test all interactions:

    • Buttons and forms
    • Dropdowns and modals
    • Filters and search
    • Shopping cart operations
  4. Monitor performance:

    • Google PageSpeed Insights
    • WebPageTest
    • Browser Network tab (file sizes)

Troubleshooting

CSS/JS broke after enabling minification

This is rare but can happen if:

  • Code relies on whitespace-dependent behavior (very unusual)
  • Plugin has inline CSS/JS with syntax errors

Solution:

  1. Disable the problematic minification type
  2. Add the specific file/handle to exclusions
  3. Re-enable with exclusion in place

File size didn't decrease much

  • File may already be minified by the plugin that created it
  • Check if your theme/plugins already minify assets
  • You still benefit from combining with other optimizations

Performance didn't improve as expected

Minification is usually 10-20% of total performance gains. Combine with:

  • Page caching (biggest impact)
  • Lazy loading images
  • CDN integration
  • Defer scripts
  • Combine CSS/JS (HTTP/1.1 only)

Best Practices

  • ✅ Enable all minification types on first deployment
  • ✅ Always test after enabling
  • ✅ Keep exclusion list small (only add if breaking)
  • ✅ Combine with file combination (HTTP/1.1 sites)
  • ✅ Monitor for third-party plugin CSS/JS issues
  • ✅ Verify on different browsers/devices

Performance Impact

Typical results from enabling all minification:

MetricImprovement
File sizes20-50% smaller
Page load time5-15% faster
Bandwidth10-30% reduced
PageSpeed score2-8 points higher

Combine minification with caching and lazy loading for best results.