英文教程:五种CSS选择器类型
英文教程:五种CSS选择器类型
发布时间:2016-12-27 来源:查字典编辑
摘要:英文教程:五种CSS选择器类型.CSScommandsareusuallygroupedinthecurlybracestomakeaset...

英文教程:五种CSS选择器类型. CSScommandsareusuallygroupedinthecurlybracestomakeasetofrules.FollowingarethevariouswaysavailabletoattachthesesetofruleswithHTMLcode.

Selector(insimplewords)meanshowyounamethesesetofrules.

1CLASSSelectORS

ClassselectorsisthesimplestformofselectorswhereyouassignyourownmeaningfulnametothesetofCSSrules.Tocreateaclassselectoryousimplyneedtowritenameoftheclassfollowedbyaperiod.

(Aclassnamecannotstartwithanumberorasymbolasitisnotsupportedbyvariousbrowsers.)

Forexample,

p.big{font-weight:bold;font-size:12px;}

.center{text-align:center;}

AndthisHTML:

<pclass="big">Thisparagraphwillberenderedbold.www.jb51.net</p>

Youcanapplymorethanoneclasstoagivenelement.

AndthisHTML:

<pclass="centerbig">Thisparagraphwillberenderedbold.</p>

Intheaboveexample.bigand.centerarenameofCSSclassesandtheseclassesareappliedtoPtaginHTML.

IfclassnameisfollowedbyHTMLelementinyourCSScodelikep.biginaboveexampleitmeansthatthisclasswillworkonPtagonly.

OtherwiseyoucanapplytheCSSclassonanyelement.

It’sagoodpracticetoaddHTMLelementbeforeclassnameinCSSifyouarewritingCSSrulesforaparticularelement(ItaddsmoreclaritytoCSScode.

2IDSelectORS

IDselectorsworklikeclassselectors,exceptthattheycanonlybeusedononeelementperpagebecausetheyworkwithIDofthehtmlelement.TheidselectorisdefinedasidoftheHTMLelementfollowedbya#symbol.

Forexample,

p#navigation{width:12em;color:#666;font-weight:bold;}

AndthisHTML:

<pid="navigation">Thisparagraphwillberenderedbold.www.jb51.net</p>

AsagoodpracticeIDselectorsmustbeusedifyouarewritingtheCSScodeforasingleHTMLelementonly.IDselectorsarewellsupportedacrossstandards-compliantbrowsers.

3TAGSelectOR

TagselectorisanothersimplemethodofCSSrulesimplementation.YoucanusethisselectortoredefinetherulesforaparticularHTMLelement.

Forexample:

p{color:#999;font-weight:bold;}

IntheaboveexampleCSScodewillbeautomaticallyappliedoneveryptag.

4DESCENDENTSelectORS

Descendentselectorsspecifythatstylesshouldonlybeappliedwhentheelementinquestionisadescendent(forexample,achild,oragrandchild)ofanotherelement.

Forexample,

h3em{color:white;background-color:black;}

AndthisHTML:

<h3>Thisis<em>emphasized</em>www.jb51.net</h3>

Intheaboveexampleemisdescendentofh3element.Abovecssrulewillautomaticallybeappliedonallemelementsinsideh3elementintheHTMLcode.Descendentselectorsarewellsupportedacrossstandards-compliantbrowsers.

5GROUPINGSelectORS

Youcanalsospecifythesamesetofrulesformorethanoneselctor,likethis:

p,h1,h2{text-align:left;}

Justplaceacommabetweeneachone.

Youcanevengetmorecomplex,andgroupmultipleclassandidselectors:

p.navigation,h1#title{font-weight:bold;}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新 Div+Css教程学习
热门 Div+Css教程学习
网页设计子分类