2022.2

Table Of Contents
WhenusedasaCSSselector,theHTMLtagforatableistable.Forarow,itistrandforacell,td.A
stylerulethatusesoneofthese,however,wouldapplytoalltables,rows,orcells.Foraruletobe
morespecificyouneedtoaddanID(forauniqueelement)oraclass(forasetofsimilarelements)to
thetable,roworcell,andusethatasthestylerule'sselector.
Adding an ID or class to a table, row or cell
BeforeyoucanaddanIDorclasstoatable,roworcell,youhavetoselectthattable,roworcell(see
"Selectingatable,roworcell"onpage278).Afterselectingthecell,rowortable,typetheIDorclassin
therespectivefieldontheAttributespane.
InCSS,refertothetable,roworcellwith#ID(whereIDshouldbereplacedwiththeactualID)orwith
.class(whereclassshouldbereplacedwiththeactualclass).
Styling the first, last and nth rows
TheCSSpseudo-classes:first-child,:last-childand:nth-child()areveryusefulforstyl-
ingtablerows.
ACSSpseudo-classfollowsaselectortospecifyaspecialstateofthatselector.Italwaysstartswitha
colon.
Thepseudo-classes:first-child,:last-childand:nth-child()selectanelementonlyifit
isthefirst,lastornthchildelementrespectively.(InHTMLandCSS,thewordchildreferstoanele-
mentinsideanotherelement.)
ThefollowingCSSstyleruleselectsthetablerow(tr)thatcomesfirst(:first-child)initsparent(which
naturallyisatable),andcolorsitsbackgroundred:
tr:first-child {
background: red;
}
Tip: InaDynamicTable,dataareinthebodyofthetable(selector:tbody)andsubtotalsarein
thefooter(selector:tfoot).
Selecting a specific row, odd or even rows, or every nth row
Thepseudo-class:nth-child()letsyouselectaspecificrow,alloddorevenrows,oreverynthrow.
Betweentheroundbracketsin:nth-child()youcanfillinanumber,oddoreven,oraformula:
an+b.Intheformula,arepresentsacyclesize(every...),nisacounter(forthechildelements),andb
isanoffsetvalue('startatb').Thefollowingexampleswillmakethisclear.
:nth-child(3)matchesjustoneelement:thethirdchildelement.
Page 280