Web Performance Optimization & Lighthouse · درس

تقليل CSS غير المستخدم

اكتشف CSS غير المستخدم الذي يضخم أوراق الأنماط وتخلّص منه باستخدام أدوات التغطية والتنقية والتأليف المعياري لشحن القواعد التي تستخدمها كل صفحة فعليًا فقط.

الدرس 4 من 413 خطوة

تقليل CSS غير المستخدم درس مجاني في Web Performance Optimization & Lighthouse على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «تقليل CSS غير المستخدم» مجاني؟

نعم — نص درس «تقليل CSS غير المستخدم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web Performance Optimization & Lighthouse، انتقل إلى CoddyKit PRO. تتضمن دورة Web Performance Optimization & Lighthouse 4 دروس في المجموع.

ماذا ستتعلم في «تقليل CSS غير المستخدم»؟

اكتشف CSS غير المستخدم الذي يضخم أوراق الأنماط وتخلّص منه باستخدام أدوات التغطية والتنقية والتأليف المعياري لشحن القواعد التي تستخدمها كل صفحة فعليًا فقط. تتمرن على Web Performance Optimization & Lighthouse مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. CSS الحرج وتسليم CSS
  2. تحسين تحميل الخطوط
  3. تأثير CSS-in-JS في الأداء
  4. تقليل CSS غير المستخدم
← العودة إلى Web Performance Optimization & Lighthouse