不太喜欢使用“!important”属性

偶几乎天天都在“折腾”,但主要是想着如何把自己的“小窝窝”打扮的更加漂亮^_^

前天重写了一下评论样式。本淫比较愚笨,遇到嵌套的CSS,干脆一层一层的写的(PS:偶系不系很有耐心呢)^_^。

虽然这样比较笨,但是也比较保险,各大浏览器也比较容易正常识别。

但是,在这里我还是要感谢可爱滴Braxiu弟弟的指点,给了我更加高效率的CSS编写方案。

Braxiu提供的代码如下:

.commentlist {
width:95%;
margin-left: 25px;
list-style: none;
}

.commentlist .comment{
display:block;
padding:0 0 5px 13px;
line-height: 24px;
width:95%;
border: 1px solid #222;
margin-bottom:10px!important;
}

.depth-1{margin-bottom:20px; }
.depth-1, .depth-3, .depth-5, .depth-7, .depth-9{ background:#222;}
.depth-2, .depth-4, .depth-6, .depth-8, .depth-10{ background:#333;}

呃,你会发现很奇怪的class——depth-x 。

其实,我也第一次看到这样的class,但是Braxiu解释说,这个是WP自动为嵌套评论添加的class。

好了,有了depth-x这样的class,就真的不用我一层一层写嵌套了。

试了一下,发现gavatar头像竟然漂到了页面左上角去了。

显然,CSS的继承性有问题,而且用了 !important 。

当然,偶不是说!important不好。

这个东西很NB,可以一下给calss或者id1000的优先性,也可以减少CSS的体积。

但是,我觉得不好的是,虽然你一句!important可以很快搞定问题,

但是你真正的理解了class与id的层次了么?

所以,为了层次清晰,头脑清晰,偶滴Theme的CSS里面几乎没有!important关键字(PS:等哈找一下,估计木有一个)。

我仔细分析了自己的模板结构,然后将braxiu提供的代码结合进来,就变成了如下样子:

.commentlist .comment {        //这个就代表第一层
width:95%;
margin-bottom:20px;
padding:0 0 5px 13px;    //这个内边距是可以继承的哦
position: relative;
border: 1px solid #222;
line-height: 24px;
background:#222;

}

.commentlist .depth-3.comment,
.commentlist .depth-5.comment,
.commentlist .depth-7.comment,
.commentlist .depth-9.comment{
background:#222;       //基数层背景色为222
margin-bottom:10px;
}

.commentlist .depth-2.comment,
.commentlist .depth-4.comment,
.commentlist .depth-6.comment,
.commentlist .depth-8.comment,
.commentlist .depth-10.comment{
background:#333;        //偶数层背景色为333
margin-bottom:10px;    //但是外底边距都为10px
}

对你会发现我的CSS罗嗦了很多,但是你也会发现,其实!important就是用来省略多级CSS访问的!

当然会轻巧很多啦。

虽然还是有这马长的CSS链,但是相对于我一层一层的写嵌套来说,效率,资源利用率还是高了许多。

毕竟用depth-9要比写9个li方便的多。。

当然,最重要的是我理清了层次,木有用!important.。

所以,Mucid建议——尽量不要使用CSS的!important属性,不是因为啥子兼容性问题,是你自己头脑清醒啊!

最后感谢一句——托Braxiu的福,偶终于将嵌套回复开到了10层^_^

.commentlist .comment {
width:95%;
margin-bottom:20px;
padding:0 0 5px 13px;
position: relative;
border: 1px solid #222;
line-height: 24px;
background:#222;}

.commentlist .depth-3.comment,
.commentlist .depth-5.comment,
.commentlist .depth-7.comment,
.commentlist .depth-9.comment{
background:#222;
margin-bottom:10px;
}

.commentlist .depth-2.comment,
.commentlist .depth-4.comment,
.commentlist .depth-6.comment,
.commentlist .depth-8.comment,
.commentlist .depth-10.comment{
background:#333;
margin-bottom:10px;
}

About Mucid

My life is brilliant !
This entry was posted in Technology and tagged , , , . Bookmark the permalink.

85 Responses to 不太喜欢使用“!important”属性

  1. 扣桥不渡 says: Unknown Unknown Unknown Unknown

    通过篇文章来看,你应该没有系统的学习过css

  2. NFL Merchandise says: Unknown Unknown Unknown Unknown

    呜呜,看不懂。。。。 :cry:

  3. OnC says: Unknown Unknown Unknown Unknown

    一直忽略了!important这个用法 :?:

  4. 阿修 says: Unknown Unknown Unknown Unknown

    我从来没用过important……

  5. yukirock says: Unknown Unknown Unknown Unknown

    CSS3里面有一个伪类,:nth-child(),用以匹配一个母元素下的子元素,括号里可以写数字,也可以用odd或even来匹配第偶数个或奇数个
    不过嵌套不是同级别的……这个没法用……

  6. 云在天边 says: Unknown Unknown Unknown Unknown

    这样不好啊 :?:

  7. Solo says: Unknown Unknown Unknown Unknown

    嵌套很壮观啊。

  8. yetone says: Unknown Unknown Unknown Unknown

    你修改一下你的那个js,把用IE的都跳转到我的站上 :twisted:

  9. 人好哇! says: Unknown Unknown Unknown Unknown

    哼哼,你这个拒绝IE的脚本还是被我用IE留下了评论 :twisted:
    其实IE8什么的还是可以放行的,啊哈哈!

  10. laowi says: Unknown Unknown Unknown Unknown

    回复嵌套几层不是后天有选项么 -0-

  11. Lambert says: Unknown Unknown Unknown Unknown

    这个内嵌评论样式还是很好看的
    PS:你还是很能折腾阿 :lol:

  12. ZYW says: Unknown Unknown Unknown Unknown

    现在最欣赏你博客的最右下角。
    我其实也想这样的~~ :!:

  13. joyla says: Unknown Unknown Unknown Unknown

    这段时间折腾的很频繁哈!