邪罗刹的菠萝阁


> 最近小邪有点忙,因为在努力学习英语中。嘿嘿,刚才顺便剥了个蛮粗糙的 Discuz 皮肤练练手。
> O(∩_∩)O,谁能告诉我,我的 灵魂独奏曲 - Soulsolo.Org 应该拿来做啥站点好哩?

> ╮(╯▽╰)╭,音乐站没那个资源,相册的话还是做在菠萝阁比较好,真令人纠结啊。
> 话说这篇文章主要是关于CSS执行的优先级的一点心得,以及无法处理优先级时候的替代方案。

1. CSS 优先级公认的五大原则

> 话说CSS优先级,是指CSS样式代码在浏览器中被解析的先后顺序。
> 如果遇到针对同一个元素需要实现相同性质不同参数的代码,这就显得很重要了。
> 主要是这次在剥皮的时候遇到默认模板的 CSS 优先级比我建立的模板的 CSS 优先级要高的问题。

> 原则一 - 继承不如指定。
> 原则二 - #ID > .class > 标签选择符。
> 原则三 - 越具体越强大。
> 原则四 - 标签#id >#id ; 标签.class > .class。
> 原则五 - 原则一 > 原则二 > 原则三 > 原则四。

2. 举一些例子来说明五大原则

> 原则一 - 继承不如指定。

<style type="text/css">
<!--
 body { color:#000; }
 #test { color:#2299cc; }
-->
</style>
<div id="test">文字的颜色是 #2299cc 兰色。</div>

> 原则二 - #ID > .Class > 标签选择符。

<style type="text/css">
<!--
 div { color:#000; }
 #test { color:#2299cc; }
 .try { color:#ee8811 }
-->
</style>
<div id="test" class="try">文字的颜色是 #2299cc 兰色。</div>

> 原则三 - 越具体越强大。

<style type="text/css">
<!--
 #test { color:#ee8811; }
 #test #test1 { color:#2299cc; }
-->
</style>
<div id="test"><div id="test1">文字的颜色是 #2299cc 兰色。</div></div>

> 原则四 - 标签#id > #id 且 标签.class > .class (注意!!标签和#id或者.class之间无空格)。

<style type="text/css">
<!--
 div#test { color:#2299cc; }
 #test { color:#ee8811; }
 div.test1 { color:#2299cc; }
 .test1 { color:#ee8811; }
-->
</style>
<div id="test">文字的颜色是 #2299cc 兰色。</div>
<div class="test1">文字的颜色是 #2299cc 兰色。</div>

> 原则五 - 原则一 > 原则二 > 原则三 > 原则四 (挺住啊,勇士)。

<style type="text/css">
<!--
 #t1 div#test { color:#777; }
 #t1 #test { color:#222; }
 #t1 div.test1 { color:#777; }
 #t1 .test1 { color:#222; } 

 .t2 div#test { color:#eee; }
 .t2 #test { color:#fff; }
 .t2 div.test1 { color:#eee; }
 .t2 .test1 { color:#fff; } 

 div div#test { color:#aaa777; }
 div #test { color:#aaa222; }
 div div.test1 { color:#aaa777; }
 div .test1 { color:#aaa222; }

 #t1 div div#test { color:#323232; }
 #t1 div #test { color:#ee8811; }
 #t1 div div.test1 { color:#323232; }
 #t1 div .test1 { color:#ee8811; } 

 .t2 div div#test { color:#1212; }
 .t2 div #test { color:#2323; }
 .t2 div div.test1 { color:#1212; }
 .t2 div .test1 { color:#2323; }

 div div div#test { color:#eeee11; }
 div div #test { color:#eeee44; }
 div div div.test1 { color:#eeee11; }
 div div .test1 { color:#eeee44; } 

 /* Evlos >> - -+ */

 #t1 div#test p { color:#777; }
 #t1 #test p { color:#222; }
 #t1 div.test1 p { color:#777; }
 #t1 .test1 p { color:#222; } 

 .t2 div#test p { color:#eee; }
 .t2 #test p { color:#fff; }
 .t2 div.test1 p { color:#eee; }
 .t2 .test1 p { color:#fff; } 

 div div#test p { color:#aaa777; }
 div #test p { color:#aaa222; }
 div div.test1 p { color:#aaa777; }
 div .test1 p { color:#aaa222; }

 #t1 div div#test p { color:#2299cc; }
 #t1 div #test p { color:#ee8811; }
 #t1 div div.test1 p { color:#2299cc; }
 #t1 div .test1 p { color:#ee8811; } 

 .t2 div div#test p { color:#1212; }
 .t2 div #test p { color:#2323; }
 .t2 div div.test1 p { color:#1212; }
 .t2 div .test1 p { color:#2323; }

 div div div#test p { color:#eeee11; }
 div div #test p { color:#eeee44; }
 div div div.test1 p { color:#eeee11; }
 div div .test1 p { color:#eeee44; }
-->
</style>
<div id="t1" class="t2">
<div>
<div id="test"><p>文字的颜色是 #2299cc 兰色。</p></div>
<div class="test1"><p>文字的颜色是 #2299cc 兰色。</p></div>
</div>
</div>

> 话说写完这段代码,我几乎要挂了 ╮(╯▽╰)╭,所以看的时候要备上枕头。
> 顺序由原则四至原则一,逐步递进,这样子的顺序理解是比较容易的。
> 其实网上也有人写过例子,不过是论证型的,更让人崩溃。
> 这里如果看不懂可以用赋予权重的方法进行计算即可,权重根据大于小于设定。

3. 覆盖法(带图片的元素):

<style type="text/css">
<!--
 body { background:#e7e7e7; }
 #t1 { background:url(1.png); width:32px; height:32px; }
 #t1 a { background:url(2.png); width:32px; height:32px; }
-->
</style>
<div id="t1"><a href="http://www.evlos.org">邪罗刹的菠萝阁</a></div>

> 假设 #t1 这行代码处于一个不可以修改的文件中时,第三行只好这样写,链接的背景就被覆盖为 2.Png 了。

4. 覆盖法(文字 DIV):

<style type="text/css">
<!--
 body { background:#e7e7e7; }
 #t1 { height:20px; }
 #t2 { position:relative; bottom:20px; background:#e7e7e7; }
-->
</style>
<div id="t1">Hello World !</div>
<div id="t2">Hello 7ucking G.F.W. !</div>

> 相对定位移动 t2 到 t1 上方,并设置背景色,如果需要的话还要设置行高、行宽之类的,就OK了。

5. 刚才剥下来的皮(蛮粗糙的,不敢放顶部了,哈哈)

6. 欢迎来 Follow 我的 Twitter @AngelSolo

Leave a comment
50 Comments.
  1. Hello 7ucking G.F.W. !
    这行经典……

    @
  2. 小y Mozilla Firefox Windows 坐板凳!#2

    = =好复杂。。
    发现了7Uck GFW..帮你补充。。7UCK工程队。。 :lol: :lol:

    @
  3. 若谷 Google Chrome Linux 躺地板!#3

    你这个网站测速工具是哪里阿

    @
  4. SATURN Mozilla Firefox Windows 天花板!#4

    把那个站搞成个7UCK G7W相关的吧,如果而已在国外搞服务器的话。。。

    @
  5. awu Google Chrome Windows 下水道!#5

    这个灵魂独奏曲可以做个鬼片的网站!哈哈~~~

    @
  6. 阿士 Internet Explorer Windows 地心!#6

    同意,做鬼片吧

    @
  7. 还没细究过呢

    @
  8. 再得瑟我就说递归了啊?! :mrgreen:

    @
  9. 写的很强大

    @
  10. 灵魂佐罗
    做wordpress博客的RSS聚合吧。感觉wpg里面的不怎么好。
    就是整理一些好的博文或资源,做成博客站,就像sina博客一样的。
    也就相当于一个home吧。

    @
  11. 还有,就是相信博主的技术。呵呵

    @
  12. 好多代码呀哈哈

    @
  13. Discuz 好像博主改的还不错~~

    @
  14. 有道理,但是这么指定CSS中字符就多了,每个访问站的人都得多加载几字节,还是简明点好。

    @
    • @九站 , 恩 ~ 呵呵 ~ 平时尽量短 ~
      假设有两个CSS文件矛盾的时候 ~
      而另外个CSS文件 我们是不可以修改的 ~
      就得这样做啦 ~ :smile: ~

      @
  15. 我是习惯写css尽量简化,只要不出现浏览器兼容性问题,css文件还是尽量小点。。。
    话说我解决了添加@ user :问题,但是mail to commenter插件就是不能发送邮件,
    用@all测试也只能给我自己发邮件。。。。。。。

    @
    • @QiQiBoY , ........... :exclaim:
      话说我收到你的 @all 邮件了 ~
      不过没有发件人 所以是在垃圾邮件里找到的 ~
      内容有点地方不大对 我转发给你看下好了 ~
      加油哦 ~ 强力支持你 ~

      @
    • @QiQiBoY , 恩 ~ 呵呵 ~ 平时尽量短 ~
      假设有两个CSS文件矛盾的时候 ~
      而另外个CSS文件 我们是不可以修改的 ~
      就得这样做啦 ~ :lol: ~

      @
    • @QiQiBoY , 汗 ~ 我知道怎么回事了 ~
      你的用法不对喔 ~
      用法有两种 ~
      第一种是双引号之内的”@user:“ ~
      第二个是双引号之内的”@user空格“ ~

      @
  16. 你贴的Google网站性能分析图,却没有说明哪段是你优化后的效果吧?

    @
  17. 说到CSS,我还真不知道你说的那么多,我仅仅知道去掉多余的代码
    例如空格,分号之类.没有研究过优先级,其实也不是完全不知道,就是没有去总结,和归类.
    你这里说的很好,很具体很受用!

    @
  18. 遗忘者 Google Chrome Windows #18

    郁闷,我的网站比96%的都要慢!

    @
  19. 高手啊

    @
  20. 你的博客人气很多

    @
  21. 7Uck GFW 是啥?

    @
  22. css难吗?我们以后好像要学。我还是新手呢

    @
  23. 哦,谢谢,以后学的时候看看

    @

Leave a Reply


[ Ctrl + Enter ]