英文教程:五種CSS選擇器類型
互聯(lián)網(wǎng) 發(fā)布時間:2009-04-02 19:33:30 作者:佚名
我要評論

網(wǎng)頁制作Webjx文章簡介:英文教程:五種CSS選擇器類型.
CSScommandsareusuallygroupedinthecurlybracestomakeasetofrules.FollowingarethevariouswaysavailabletoattachthesesetofruleswithHTMLcode.
Selector(insimplewords)mea
英文教程:五種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.chabaoo.cn</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.chabaoo.cn</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>chabaoo.cn</h3>
Intheaboveexampleemisdescendentofh3element.Abovecssrulewillautomaticallybeappliedonallemelementsinsideh3elementintheHTMLcode.Descendentselectorsarewellsupportedacrossstandards-compliantbrowsers.
5GROUPINGSelectORS
Youcanalsospecifythesamesetofrulesformorethanoneselctor,likethis:
p,h1,h2{text-align:left;}
Justplaceacommabetweeneachone.
Youcanevengetmorecomplex,andgroupmultipleclassandidselectors:
p.navigation,h1#title{font-weight:bold;}
相關(guān)文章
- 在不嵌套單獨使用ID選擇器的情況下,它是比較快的,然而各種各樣的原因使開發(fā)者們拋棄對ID選擇器的使用.究竟該不該使用ID選擇器?這里我們就來淺談對CSS的ID選擇器的使用建議2016-07-11
- 這篇文章主要介紹了CSS中的類型選擇器和ID選擇器以及類選擇器,選擇器時CSS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-02-18
IE7對css選擇器的改進-CSS教程-網(wǎng)頁制作-網(wǎng)頁教學(xué)網(wǎng)
1、對偽類的支持。IE6和更低的版本對于偽類的支持僅限于a標(biāo)簽,諸如“:hover”、“:active”、“:focus”之類的偽類理論上是應(yīng)該適用于所有2008-10-17CSS屬性選擇器的四種格式-CSS教程-網(wǎng)頁制作-網(wǎng)頁教學(xué)網(wǎng)
屬性選擇器(AttributeSelectors),或許你不應(yīng)該對屬性選擇器感到陌生,從本質(zhì)上說,id跟類選擇器其實就是屬性選擇器,只不過是選擇了id或者類的值(value)而已。2008-10-17最常用的五類CSS選擇器-CSS教程-網(wǎng)頁制作-網(wǎng)頁教學(xué)網(wǎng)
一些新手朋友對選擇器一知半解,不知道在什么情況下運用什么樣的選擇器,這是一個比較頭疼的問題,針對新手朋友,對CSS選擇器作一些簡單的說明,希望能對大家的學(xué)習(xí)工作2008-10-17- 偽類,用于給元素的片段添加樣式,這如何理解呢?比如你要讓一個段落的第一行的文字加粗,那么這個選擇器是不二之選2012-04-18
- 前面花了兩節(jié)內(nèi)容分別在《CSS3選擇器——基本選擇器》和《CSS3選擇器——屬性選擇器》介紹了CSS3選擇器中的基本選擇器和屬性選擇器使用方法,今天要和大家一起學(xué)習(xí)CSS3選擇2012-01-21
- 上一切在《CSS3選擇器——基本選擇器》中主要介紹了CSS3選擇器的第一部分,這節(jié)主要和大家一起來學(xué)習(xí)CSS3選擇器的第二部分——屬性選擇器2012-01-21
- CSS的選擇器,我想大家并不會陌生吧,因為天天在使用,但對于CSS3的選擇器,要運用的靈活到位,我想對很多朋友還是一定的難度,特別是CSS3中的:nth選擇器2012-01-21
- *通配符選擇器,經(jīng)常用于css reset(樣式重置),清理標(biāo)簽的默認(rèn)樣式,但現(xiàn)在一般不提倡直接使用*了,主要是*會匹配所有標(biāo)簽,相當(dāng)耗資源。*在css的優(yōu)先級中是最低的。2011-07-27