2022.2

Table Of Contents
l
Amorespecific,andthereforemoreimportantrule,isencounteredforthesameelement.See
"UsingamorespecificCSSrule"belowtolearnmoreaboutthespecificityofstylerules.
l
Arulewiththesameimportanceisreadafterthefirstrule.Notonlyistheorderoftherulesina
CSSfileimportant,butalsotheorderinwhichthestylesheetsareread.Thestylesheetsthatare
includedwithasectionarereadinthespecifiedorder;see"Applyingastylesheettoasection"
onpage434.
UsingamorespecificCSSrule
Bydefault,manyCSSpropertiesofanHTMLelementalsoapplytotheelementsinsidethatelement.
Forexample,aCSSrulethatspecifiesacertainfont-typeforaboxalsoappliestoparagraphsinthat
box.Inthisexampletheboxisthe'parent'elementandtheparagraphsarethe'child'elementsthat
inheritthefont-typepropertyofthebox.
Note: Althoughthebackgroundcolorpropertyseemstobeinherited,itisn't.Mostelementsare
transparent;thereforethebackgroundcoloroftheparentelementshinesthrough.
Toreplaceinheritedstyleproperties,youneedtoaddamorespecificCSSruleforthat(typeof)ele-
ment.Incaseofaconflictbetweenageneralruleandamorespecificrule,themorespecificrulewillbe
applied.
Thefollowingdiagramshowstheorderofspecificity.
RulesforHTMLelements(p,table,lietc.)aregeneralrules.Rulesforclasses,pseudoclasses,andele-
mentswithacertainattribute(.class,:hover,[target])aremorespecific.Rulesforelementswithacer-
tainIDareevenmorespecific.Themostspecificareinlinestyles.
Example
AssumingthatatablehastheCSSproperty"color:red"(whichcolorstextinthecellsred),amorespe-
cificruleforcellsinthattablecouldbe,forexample:
Page 680