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

互联网   发布时间:2009-04-02 19:33:30   作者:佚名   我要评论
网页制作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.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;}

相关文章

  • 该不该使用ID选择器?浅谈对CSS的ID选择器的使用建议

    在不嵌套单独使用ID选择器的情况下,它是比较快的,然而各种各样的原因使开发者们抛弃对ID选择器的使用.究竟该不该使用ID选择器?这里我们就来浅谈对CSS的ID选择器的使用建议
    2016-07-11
  • 简要讲解CSS中的类型选择器、ID选择器、类选择器

    这篇文章主要介绍了CSS中的类型选择器和ID选择器以及类选择器,选择器时CSS入门学习中的基础知识,需要的朋友可以参考下
    2016-02-18
  • IE7对css选择器的改进-CSS教程-网页制作-网页教学网

    1、对伪类的支持。IE6和更低的版本对于伪类的支持仅限于a标签,诸如“:hover”、“:active”、“:focus”之类的伪类理论上是应该适用于所有
    2008-10-17
  • CSS属性选择器的四种格式-CSS教程-网页制作-网页教学网

      属性选择器(AttributeSelectors),或许你不应该对属性选择器感到陌生,从本质上说,id跟类选择器其实就是属性选择器,只不过是选择了id或者类的值(value)而已。  
    2008-10-17
  • 最常用的五类CSS选择器-CSS教程-网页制作-网页教学网

     一些新手朋友对选择器一知半解,不知道在什么情况下运用什么样的选择器,这是一个比较头疼的问题,针对新手朋友,对CSS选择器作一些简单的说明,希望能对大家的学习工作
    2008-10-17
  • 四种css 伪类选择器

    伪类,用于给元素的片段添加样式,这如何理解呢?比如你要让一个段落的第一行的文字加粗,那么这个选择器是不二之选
    2012-04-18
  • CSS3 选择器 伪类选择器介绍

    前面花了两节内容分别在《CSS3选择器——基本选择器》和《CSS3选择器——属性选择器》介绍了CSS3选择器中的基本选择器和属性选择器使用方法,今天要和大家一起学习CSS3选择
    2012-01-21
  • CSS3 选择器 属性选择器介绍

    上一切在《CSS3选择器——基本选择器》中主要介绍了CSS3选择器的第一部分,这节主要和大家一起来学习CSS3选择器的第二部分——属性选择器
    2012-01-21
  • CSS3 选择器 基本选择器介绍

    CSS的选择器,我想大家并不会陌生吧,因为天天在使用,但对于CSS3的选择器,要运用的灵活到位,我想对很多朋友还是一定的难度,特别是CSS3中的:nth选择器
    2012-01-21
  • 你不可不知的CSS选择器

    *通配符选择器,经常用于css reset(样式重置),清理标签的默认样式,但现在一般不提倡直接使用*了,主要是*会匹配所有标签,相当耗资源。*在css的优先级中是最低的。
    2011-07-27

最新评论