当前位置: 首页 > 图文教程 > 网页制作 > CSS样式表 > 英文教程:五种CSS选择器类型

CSS样式表
验证并修复css错误内容的相关工具
分享3个比较实用的CSS页面框架布局
CSS幻灯片教程:制作高效可维护组件化的CSS代码
网页制作基础知识:html特殊符号
CSS教程:按整洁易懂的结构组织CSS样式
查看和编辑CSS中用到的颜色:CSS Prism
帮助你学习CSS的在线CSS工具网站
CSS网页设计参考:把HTML标记分类
CSS进阶:几个Reset CSS的八卦问题
WEB设计技巧:网页虚线制作方法剖析
WEBJX收集CSS格式化和造型网页高级教程
CSS3教程:在CSS中使用的颜色
学习WEB标准心得:网页重构的思路
轻易创建css导航工具:CSS Tab Designer2
总结学习web标准的十个重要理由
CSS实例教程:CSS制作星级评价的功能
CSS实例教程:纯CSS实现圆角框
多个css、js文件自动合并、压缩
网页设计教程:CSS文字排版技巧大全
Photoshop制作CSS网页制作的背景图

CSS样式表 中的 英文教程:五种CSS选择器类型


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-11   浏览: 138 ::
收藏到网摘: n/a


                         英文教程:五种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.ruanchen.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.ruanchen.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.ruanchen.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;}