CSS Audit Tool

Analyze CSS files for duplicates, conflicts, unused rules, and structural issues across Framework components.

Purpose

The CSS Audit tool scans CSS files to identify problems that cause styling conflicts in the Hydrowise dashboard:

  • Duplicate selectors with conflicting values
  • Unused CSS rules (selectors not in HTML)
  • Specificity conflicts between component CSS
  • !important overuse
  • Missing responsive breakpoints

Running the Audit

Command Line

python css_audit.py --path /path/to/css --output report.html

Options

FlagDescription
--pathDirectory containing CSS files to scan
--outputOutput file for report (HTML or JSON)
--htmlPath to HTML files for unused rule detection
--ignoreComma-separated list of files to skip
--strictTreat warnings as errors

Audit Categories

1. Duplicate Detection

Finds selectors defined multiple times with different values:

Problem: Same selector, different values
/* In dashboard.css */
.card { background: #fff; }

/* In components.css */
.card { background: #f0f0f0; }
Solution: Consolidate into single definition or use more specific selectors
.dashboard-card { background: #fff; }
.sidebar-card { background: #f0f0f0; }

2. Specificity Analysis

Calculates specificity scores and flags conflicts:

Selector TypeSpecificityExample
Element0,0,1div
Class0,1,0.card
ID1,0,0#header
Inline1,0,0,0style=""
Warning: When lower specificity rules use !important to override higher specificity, it creates maintenance problems.

3. Unused Rule Detection

Cross-references CSS selectors against HTML to find orphaned rules:

# Output shows unused selectors
UNUSED: .legacy-button (dashboard.css:145)
UNUSED: .old-header (theme.css:89)
UNUSED: #deprecated-modal (components.css:234)

4. !important Audit

Flags overuse of !important declarations:

CountSeverityAction
0-5OKAcceptable for edge cases
6-15WarningReview and reduce
16+ErrorRefactor CSS architecture

5. Responsive Coverage

Checks for required breakpoints in component CSS:

Required breakpoints:
  @media (max-width: 1200px)  /* Desktop */
  @media (max-width: 992px)   /* Tablet */
  @media (max-width: 768px)   /* Mobile landscape */
  @media (max-width: 576px)   /* Mobile portrait */

Report Output

Summary Section

CSS AUDIT REPORT
================
Files scanned: 12
Total rules: 847
Duplicates found: 23
Unused rules: 45
!important count: 8
Missing breakpoints: 3 files

ERRORS: 2
WARNINGS: 5

Detailed Findings

DUPLICATE: .btn-primary
  Location 1: buttons.css:45 (background: #007bff)
  Location 2: theme.css:123 (background: #0066cc)
  Recommendation: Use single source, remove duplicate

UNUSED: .legacy-modal-backdrop
  Location: modals.css:234
  Recommendation: Remove if no longer needed

IMPORTANT: 8 declarations found
  dashboard.css:89 - .sidebar { width: 250px !important }
  Consider: Use more specific selector instead

Best Practices

CSS Encapsulation Rule: Components handle ONLY their own CSS. Parents NEVER set CSS on children. This enables embedding anything in anything without style conflicts.