การลด CSS ที่ไม่ได้ใช้งาน
ค้นหาและกำจัด CSS ที่ไม่ได้ใช้งานซึ่งทำให้สไตล์ชีตมีขนาดใหญ่เกินจำเป็น ด้วยเครื่องมือวัดความครอบคลุม การล้างโค้ด และการเขียนแบบแยกส่วน เพื่อส่งเฉพาะกฎที่แต่ละหน้าใช้จริง
การลด CSS ที่ไม่ได้ใช้งาน เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Cost of Dead CSS
Large frameworks and accumulated styles often ship rules no page uses. Unused CSS inflates download size and parse time, and because CSS is render-blocking, it directly delays first paint.
Measuring Coverage
The DevTools Coverage tab records which CSS bytes actually apply during a session. A high unused percentage signals an opportunity to trim.
Why Frameworks Bloat
Utility and component frameworks include thousands of classes for flexibility. If you use a small subset, the rest is pure dead weight unless removed at build time.
Purging Unused Rules
Tools like PurgeCSS scan your templates for class names and strip any selector not referenced, often cutting stylesheets by 90% or more.
// purgecss.config.js
module.exports = {
content: ['./src/**/*.html', './src/**/*.jsx'],
css: ['./src/styles.css']
};Tailwind's Built-In Purge
Utility frameworks like Tailwind scan your content files and generate only the classes you actually use, producing a tiny production stylesheet automatically.
// tailwind.config.js
module.exports = { content: ['./src/**/*.{html,jsx,tsx}'] };Beware Dynamic Class Names
Purgers work by string matching. Class names built dynamically can be wrongly removed. Use a safelist or full static strings to protect them.
// risky: 'bg-' + color -> may be purged
// safe: full literal
const cls = isError ? 'bg-red-500' : 'bg-green-500';Per-Route CSS
Splitting styles by route means each page loads only its own CSS instead of one global blob. Many bundlers and frameworks do this automatically with CSS code splitting.
CSS Modules and Scoping
Scoped styles via CSS Modules or single-file-component styles keep CSS tied to the component that uses it, making unused styles easier to spot and remove.
Minification
After purging, run a CSS minifier (cssnano, esbuild, lightningcss) to strip whitespace, comments, and redundant rules, and compress with Brotli or gzip in transport.
Verifying the Win
Re-run Coverage and Lighthouse after trimming. Lighthouse explicitly reports Reduce unused CSS with the estimated savings, confirming the improvement.
Workflow Summary
- Measure with the Coverage tab.
- Purge unused selectors at build.
- Safelist dynamic classes.
- Split CSS per route, then minify.
Quick Check
Your build dynamically composes class names like 'bg-' + color and they disappear in production. What is the likely cause and fix?
Recap
You learned to cut render-blocking CSS by measuring coverage, purging unused selectors, scoping styles per component and route, and minifying. Watch for dynamically built class names and safelist them so purging stays safe.
เรียนรู้ Web Performance Optimization & Lighthouse ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การลด CSS ที่ไม่ได้ใช้งาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การลด CSS ที่ไม่ได้ใช้งาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การลด CSS ที่ไม่ได้ใช้งาน”
ค้นหาและกำจัด CSS ที่ไม่ได้ใช้งานซึ่งทำให้สไตล์ชีตมีขนาดใหญ่เกินจำเป็น ด้วยเครื่องมือวัดความครอบคลุม การล้างโค้ด และการเขียนแบบแยกส่วน เพื่อส่งเฉพาะกฎที่แต่ละหน้าใช้จริง คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การลด CSS ที่ไม่ได้ใช้งาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม
ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- CSS สำคัญและการส่งมอบ CSS
- การปรับปรุงการโหลดฟอนต์
- ผลกระทบด้านประสิทธิภาพของ CSS-in-JS
- การลด CSS ที่ไม่ได้ใช้งาน