CSS选择器的类型介绍

2020-03-25 16:00:35来源:爱站网 阅读 ()

新老客户大回馈,云服务器低至5折

在我们选项css的时候一般都会使用到选择器,但是css的选择器有很多我们要怎么选择呢?下面我们就跟爱站小编一起去看看CSS选择器的类型介绍。
英文教程:五种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.aizhan.com</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.aizhan.com</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.aizhan.com</h3>
Intheaboveexampleemisdescendentofh3element.Abovecssrulewillautomaticallybeappliedonallemelementsinsideh3elementintheHTMLcode.Descendentselectorsarewellsupportedacrossstandards-compliantbrowsers.
5GROUPINGSelectORS
Youcanalsospecifythesamesetofrulesformorethanoneselctor,likethis:
p,h1,h2{text-align:left;}
Justplaceacommabetweeneachone.
Youcanevengetmorecomplex,andgroupmultipleclassandidselectors:
p.navigation,h1#title{font-weight:bold;}

以上就是小编分享CSS选择器的类型介绍,看完后大家学习到了多少呢?我们在学习的时候也要常常温故而知新,这样才能牢固的掌握。


原文链接:https://js.aizhan.com/web_authoring/css/12317.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:CSS如何解决多列布局的问题

下一篇:学习WEB标准的技能