CSS层叠与继承用法手册

本文向大家描述一下CSS层叠与继承的用法,个元素可能同时被多个CSS选择器选中,每个选择器都有一些CSS规则,这就是层叠,而继承得来的规则没有特殊性。

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了莲都免费建站欢迎大家使用!

CSS层叠与继承

一、CSS层叠

  我们知道文档中的一个元素可能同时被多个CSS选择器选中,每个选择器都有一些CSS规则,这就是层叠。这些规则有可能不矛盾的,自然这些规则将会同时起效,然而有些规则是相互冲突的,例如:

ExampleSourceCode

 
 
 
  1. CSSCascade title></li> <li><styletypestyletype="text/CSS"></li> <li>h1{color:Red;} </li> <li>bodyh1{color:Blue;} </li> <li> style></li> <li> head></li> <li><body></li> <li><h1>Hellodiv-CSS.net h1></li> <li> body></li> <li> html></li> </ol></pre><p>   为此需要为每条规则制定特殊性,当发生冲突的时候必须选出一条最高特殊性的规则来应用。CSS规则的特殊性可以用4个整数来表示,例如0,0,0,0.计算规则如下:</p><p>对于规则中的每个ID选择符,特殊性加0,1,0,0</p><p>对于规则中每个类选择符和属性选择符以及伪类,特殊性加0,0,1,0</p><p>对于规则中的每个元素名或者伪元素,特殊性加0,0,0,1</p><p>对于通配符,特殊性加0,0,0,0.</p><p>对于内联规则,特殊性加1,0,0,0</p><p>  最终得到结果就是这个规则的特殊性。两个特殊性的比较类似字符串大小的比较,是从左往右依次比较,第一个数字大的规则的特殊性高。上例中两条规则的特殊性分别是0,0,0,1和0,0,0,2,显然第二条胜出,因此最终字是蓝色的。</p><p>  注意,通配符的特殊性0,0,0,0看起来没有作用,实际上不是,还有一种没有特殊性的规则,0,0,0,0要比没有特殊性更特殊,下面会介绍。</p><p>  CSS还有一个!important标签,用来改变CSS规则的特殊性。实际上,在解析CSS规则特殊性的时候,是将具有!important的规则和没有此标签的规则利用上述方法分别计算特殊性,分别选出特殊性最高的规则。最终合并的时候,具有任何特殊性的带有!important标记的规则胜出。#p#</p><p><strong>二、CSS继承</strong></p><p>  所谓继承,就是父元素的规则也会适用于子元素。比如给body设置为color:Red;那么他内部的元素如果没有其他的规则设置,也都会变成红色。继承得来的规则没有特殊性。下面看一个简单的例子:</p><p>ExampleSourceCode</p><pre> <ol> <li><htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"></li> <li><head></li> <li><title>CSSCascade title></li> <li><styletypestyletype="text/CSS"></li> <li>*{color:Blue;} </li> <li>div{color:Black;} </li> <li>.imp{color:Red!important;} </li> <li>#content{color:Green;} </li> <li> style></li> <li> head></li> <li><body></li> <li><div>Hello<span>div-CSS.net span> div></li> <li><dividdivid="content"></li> <li><pclasspclass="imp">Title p></li> <li>ContentGoesHere. </li> <li> div></li> <li> body></li> <li> html></li> <li></li> </ol></pre><p>   注意,第一行的CSS并没有继承div的黑色,这是因为通配符的缘故。通配符的特殊性虽然是全0,但是还是比继承的特殊性要高。第二行展示了!important标记的作用。<br />   另外,一些明显不应该继承的属性,比如border,margin,padding之类的是不会被继承的,具体可以参考CSS手册。</p><p><strong>三、其他</strong></p><p>  虽然有4个整数来表示一个特殊性,仍然有可能出现两条冲突的规则的特殊性完全一致的情况,此时就按照CSS规则出现的顺序来确定,在样式表中最后一个出现的规则胜出。一般不会出现这样的情况,只有一个情况例外,考虑如下样式表:</p><p>ExampleSourceCode</p><pre> <ol> <li>:active{color:Red;} </li> <li>:hover{color:Blue;} </li> <li>:visited{color:Purple;} </li> <li>:link{color:Green;} </li> <li></li> </ol></pre><p>  这样页面中的链接永远也不会显示红色和蓝色,因为一个链接要么被访问过,要么没有被访问过。而这两条规则在最后,因此总会胜出。如果改成这样:</p><p>ExampleSourceCode</p><pre> <ol> <li>:link{color:Green;} </li> <li>:visited{color:Purple;} </li> <li>:hover{color:Blue;} </li> <li>:active{color:Red;} </li> <li></li> </ol></pre><p>  就能实现鼠标悬停和点击的瞬间变色的效果。这样的顺序的首字母正好连成“LoVeHA”,这样的顺序被约定俗成的叫做LoveHa规则。特殊性规则从理论上讲比较抽象和难懂,但在实践中,只要样式表是设计良好的,并不会有太多这方面的困扰,因此本文也不再做深究,更多的技术请参考div-CSS.net的文章更新!</p><p>文章来源:Div-CSS.net设计网参考:http://www.div-CSS.net/div_CSS/topic/index.asp?id=9998</p> <p> 分享名称:<a href="http://www.mswzjz.cn/qtweb/news4/44604.html">CSS层叠与继承用法手册</a> <br> 分享URL:<a href="http://www.mswzjz.cn/qtweb/news4/44604.html">http://www.mswzjz.cn/qtweb/news4/44604.html</a> </p> <p> 攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等 </p> <p class="adpic"> <a href="https://www.cdcxhl.com/service/ad.html" target="_blank" class="ad">广告</a> <a href="" target="_blank" class="adimg"><img src=""></a> </p> <p class="copy"> 声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: <a href="http://www.mswzjz.cn/" target="_blank">贝锐智能</a> </p> </div> <div class="newsmorelb"> <p>贝锐智能技术为您推荐以下文章</p> <ul> <li> <a href="/qtweb/news3/44603.html">深入探索Redis数据量如何(查询redis有多少数据)</a> </li><li> <a href="/qtweb/news2/44602.html">Linux下进程如何优化,避免消耗过多的资源?(linux进程占用资源)</a> </li><li> <a href="/qtweb/news1/44601.html">数据访问层DAL实现过程</a> </li><li> <a href="/qtweb/news0/44600.html">mysql3升级到mysql5解决乱码心得</a> </li><li> <a href="/qtweb/news49/44599.html">简单讲解一下CloudBeaver架构</a> </li><li> <a href="/qtweb/news48/44598.html">详解nmap命令使用实例</a> </li><li> <a href="/qtweb/news47/44597.html">LinuxTCP6监听详解(linuxtcp6listen)</a> </li><li> <a href="/qtweb/news46/44596.html">idc机房机柜标准多高?(服务器机柜标准尺寸规格)</a> </li><li> <a href="/qtweb/news45/44595.html">十大黑客工具之一——Burpsuite</a> </li> </ul> </div> </div> <div class="col-lg-3 noneb"> <div class="bkright" style="margin-top: 0"> <p><a href="https://www.cdcxhl.com/news/jianshe/">网站建设知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news41/62791.html">创新互联鸿蒙OS教程:鸿蒙OSNavigableMap</a> </li><li> <a class="text_overflow" href="/qtweb/news26/223176.html">阿里云Linux操作系统简介及使用建议(阿里云linux系统)</a> </li><li> <a class="text_overflow" href="/qtweb/news19/405119.html">Redis环境测试有效解决难题(redis环境测试)</a> </li><li> <a class="text_overflow" href="/qtweb/news48/61898.html">cdn加速服务器购买怎么配置</a> </li><li> <a class="text_overflow" href="/qtweb/news49/52599.html">Redis实现简易高效的用户登录管理(redis用户登录)</a> </li><li> <a class="text_overflow" href="/qtweb/news24/4674.html">香港轻量级服务器</a> </li><li> <a class="text_overflow" href="/qtweb/news17/53417.html">云储存是干什么用的(云储存是什么意思?)</a> </li><li> <a class="text_overflow" href="/qtweb/news6/289806.html">如何进行python基础回顾(python记录操作过程)</a> </li><li> <a class="text_overflow" href="/qtweb/news19/206869.html">163邮箱主机名是什么?163邮箱收件服务器主机名</a> </li><li> <a class="text_overflow" href="/qtweb/news34/132584.html">电信境外服务叫什么?(电信境外服务叫什么名字)</a> </li><li> <a class="text_overflow" href="/qtweb/news28/260878.html">煤矿闭坑的标准?(昨天收到这个邮件一涉及注销备案号及域名信息主办单位名中国地质)</a> </li><li> <a class="text_overflow" href="/qtweb/news38/485388.html">C++include机制基本概念详解</a> </li><li> <a class="text_overflow" href="/qtweb/news2/253402.html">解析Flex开发环境的搭建</a> </li><li> <a class="text_overflow" href="/qtweb/news38/276588.html">后危机时代基于云计算的企业IT创新应用</a> </li><li> <a class="text_overflow" href="/qtweb/news10/219560.html">移动传真服务器?(移动传真服务器连接失败)</a> </li> </ul> </div> <div class="bkright tag"> <p><a href="https://www.cdcxhl.com/hangye/" target="_blank">分类信息网</a></p> <ul> <li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/blgds/" target="_blank">玻璃钢雕塑</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/yupeng/" target="_blank">雨棚定制</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zufadianji/" target="_blank">发电机租赁</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/baiwuyu/" target="_blank">白乌鱼</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ggsj/" target="_blank">广告设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/qchs/" target="_blank">报废汽车回收</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/dibang/" target="_blank">地磅秤</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/mutuopan/" target="_blank">木托盘</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/shipenji/" target="_blank">湿喷机</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zhendongpan/" target="_blank">振动盘</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/fadianjihs/" target="_blank">发电机回收</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/rxfhw/" target="_blank">柔性防护网</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/hntjbj/" target="_blank">混凝土搅拌机</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/blgzd/" target="_blank">玻璃钢坐凳</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/hntjbg/" target="_blank">混凝土搅拌罐</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/hntjbz/" target="_blank">混凝土搅拌站</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="http://www.kswsj.com/" target="_blank">成都网页设计</a>    <a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">四川绵阳主机托管</a>    <a href="http://seo.cdkjz.cn/quanwang/" target="_blank">百度推广公司</a>    <a href="https://www.cdxwcx.com/" target="_blank">成都网站制作</a>    <a href="http://seo.cdcxhl.cn/wztg/" target="_blank">成都谷歌SEO</a>    <a href="http://www.cqhuaxiang.cn/" target="_blank">重庆花箱</a>    <a href="https://www.xwcx.net/tuoguan.html" target="_blank">成都IDC机房托管</a>    <a href="http://www.wjzwz.com/" target="_blank">温江网站制作</a>    <a href="https://www.cdcxhl.com/seo/chengdu.html" target="_blank">四川成都seo网站优化</a>    <a href="http://www.cdxwcx.cn/" target="_blank">做网站</a>    <a href="http://chengdu.cdcxhl.com/dingzhi/" target="_blank">APP定制开发</a>    <a href="http://www.cdhuace.com/" target="_blank">成都广告制作</a>    <a href="https://www.xwcx.net/" target="_blank">成都机柜租用</a>    <a href="http://www.lsbanjia.cn/" target="_blank">崇州搬家公司</a>    <a href="http://www.cxjianzhan.com/" target="_blank">营销推广</a>    <a href="http://chengdu.cdxwcx.cn/wangzhan/" target="_blank">手机网站制作</a>    <a href="http://www.4006tel.net/yingxiao/" target="_blank">网络营销</a>    <a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a>    <a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都小程序开发</a>    <a href="http://www.cxjianzhan.com/" target="_blank">网络推广公司</a>     </div> </div> <footer> <div class="carousel-inner footjz"> <div class="container"> <i class="icon iconfont zbw"></i> 品质网站制作 <i class="icon iconfont"></i> 自适应网站 <i class="icon iconfont"></i> 开发成本低 <i class="icon iconfont"></i> 响应速度快 <i class="icon iconfont"></i> 多人服务好 <button type="button" class="btn btn-default btn-lg" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 立即开始600网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 600网站制作</button> </div> </div> <div class="carousel-inner bqsy"> <div class="container"> <div class="lxfs"> <h4 class="yutelnone">028-86922220 13518219792</h4> <h4 class="yutelblock"><a href="tel:02886922220">028-86922220</a> <a href="tel:13518219792">13518219792</a></h4> <a class="btn btn-default" href="tencent://message/?uin=532337155&Site=&Menu=yes" role="button">网站建设<span>QQ</span>:532337155</a> <a class="btn btn-default" href="tencent://message/?uin=631063699&Site=&Menu=yes" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=532337155&version=1&src_type=web&web_src=oicqzone.com" role="button">网站制作<span>QQ</span>:532337155</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=631063699&version=1&src_type=web&web_src=oicqzone.com" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn-default nonea" href="tencent://message/?uin=1683211881&Site=&Menu=yes" role="button">售后QQ:1683211881</a> <div class="dz">创新互联建站专注: <a href="http://www.mswzjz.cn/" target="_blank">攀枝花网站设计</a> <a href="http://www.mswzjz.cn/" target="_blank">攀枝花网站制作</a> <a href="http://www.mswzjz.cn/" target="_blank">攀枝花网站建设</a> <address>地址:成都太升南路288号锦天国际A幢10楼</address> </div> </div> <div class="bzdh dz"><img src="https://www.cdcxhl.com/imges/bottom_logo.png" alt="创新互联"> <p><a href="https://www.cdcxhl.com/menu.html" target="_blank">成都创新互联科技有限公司</a><br> Tel:028-86922220(7x24h)</p></div> </div> </div> </footer> </body> </html> <script> $.getJSON ("../../qtwebpic.txt", function (data) { var jsonContent = { "featured":data } var random = jsonContent.featured[Math.floor(Math.random() * jsonContent.featured.length)]; $(".adpic .adimg").attr("href",random.link) $(".adpic img").attr("src",random.pic); }) </script>