Understanding Specificity
When multiple CSS rules target the same element, specificity determines which rule wins.
What is Specificity?
The text is red because ID selectors have the highest specificity!
The Specificity Hierarchy
From lowest to highest:
- Element selectors (p, div, span) - lowest
- Class selectors (.class, [attr], :pseudo) - medium
- ID selectors (#id) - high
- Inline styles - highest
- !important - overrides all
Combining Selectors
More specific selectors win:
Equal Specificity = Last Wins
When specificity is equal, the last rule in the CSS wins.
Class vs Multiple Classes
The !important Override
Use !important sparingly - it makes CSS harder to maintain!
Inheritance vs Specificity
Direct selectors always beat inherited styles, regardless of specificity.
Exercise: Override a Style
Override the ID selector color using a class with !important:
التالي · الدرس 14 من 27
Calculating Specificity
قراءة 3 دقيقة
أنهيت 13 من 27 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

