惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
博客园_首页
J
Java Code Geeks
C
Check Point Blog
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
D
Docker
U
Unit 42
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
GbyAI
GbyAI
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Cut Your CSS File Size by 40% Without Losing Any S...
Kui Luo · 2026-05-27 · via DEV Community

Kui Luo

Most websites ship CSS that's 2-3x larger than necessary. After auditing over 50 production sites, I found the same patterns wasting kilobytes on every page load. Here's what actually works to trim the fat.

The Numbers That Matter

Optimization Avg Savings Difficulty Impact on Layout
Remove unused selectors 25-35% Low None
Consolidate similar rules 8-15% Medium None
Replace magic numbers with variables 5-10% Low None
Flatten nested selectors 3-8% Low Potential shifts
Switch to logical properties 2-5% Low None

1. Find What You Actually Use

Open Chrome DevTools → Coverage tab → Start capturing → reload your page. The red bars show unused bytes. On most sites, 40-60% of CSS never matches any element.

Copy the unused selectors list. Most of them fall into three buckets:

  • Dead component styles — you removed a component but forgot its CSS
  • Responsive overrides — media queries for breakpoints you no longer use
  • Utility class bloat — you imported an entire framework but use 12 classes

Delete them. Run your site. If nothing breaks, you're golden.

2. Merge Duplicate Declarations

This is the biggest hidden waste. Search your codebase for repeated property-value pairs:

/* Before: 3 rules, 78 bytes */
.card { padding: 16px; border-radius: 8px; }
.modal { padding: 16px; border-radius: 8px; }
.toast { padding: 16px; border-radius: 8px; }

/* After: 1 rule, 56 bytes (28% smaller) */
.card, .modal, .toast { padding: 16px; border-radius: 8px; }

On one project this single technique cut 14KB from a 89KB stylesheet.

3. Stop Nesting More Than 2 Levels Deep

Deep nesting creates specificity problems AND bloated output:

/* Avoid this */
.nav .nav-item .nav-link .icon { color: blue; }

/* Use this instead */
.nav-icon { color: blue; }

The compiled CSS from nesting nav > ul > li > a > span generates selectors that are harder to override, forcing you to write even more specific rules later. It's a vicious cycle that makes your stylesheet grow with every feature.

4. Use Custom Properties Instead of Copy-Paste

If you have the same color, spacing, or font-size repeated 30+ times, you're doing it wrong:

:root {
  --space-sm: 8px;
  --space-md: 16px;
  --radius: 8px;
}

/* One change updates everything */

This doesn't just save bytes. It makes theme changes a single-line edit instead of a 47-file find-and-replace.

5. Audit Before You Optimize

Before changing anything, measure:

  1. Total CSS bytes transferred (check Network tab)
  2. Parse time (Performance tab → look for "Parse Author StyleSheet")
  3. Unused percentage (Coverage tab)

After each optimization, measure again. Some "optimizations" actually increase size after gzip because they reduce repetition patterns. The Coverage tab is your source of truth, not your gut feeling.

The Quick Win Checklist

  • [ ] Run Chrome Coverage tab, delete unused selectors
  • [ ] Search for duplicate property blocks, merge them
  • [ ] Flatten selectors deeper than 3 levels
  • [ ] Replace repeated values with custom properties
  • [ ] Remove media queries for breakpoints you dropped
  • [ ] Check the gzip size before and after — that's your real number

Most teams can complete this checklist in under 2 hours and see a 30-40% reduction in CSS payload. The initial page render gets noticeably faster because the browser has less to parse before it can paint.