大闹前端Web前端之路程序员

web前端开发【连载9】-CSS hack和优化

2017-04-14  本文已影响114人  Iris_mao

今天好热啊,有一种突然从冬天过到夏天的feel~


热死傻球了~
CSS hack
“-″减号是IE6专有的hack
“\\9″ IE6/IE7/IE8/IE9/IE10都生效
“\\0″ IE8/IE9/IE10都生效,是IE8/9/10的hack
“\\9\\0″ 只对IE9/IE10生效,是IE9/10的hack
*html *前缀只对IE6生效
*+html *+前缀只对IE7生效
@media screen\\9{...}只对IE6/7生效
@media \\0screen {body { background: red; }}只对IE8有效
@media \\0screen\\,screen\\9{body { background: blue; }}只对IE6/7/8有效
@media screen\\0 {body { background: green; }} 只对IE8/9/10有效
@media screen and (min-width:0\\0) {body { background: gray; }} 只对IE9/10有效
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {body { background: orange; }} 只对IE10有效
    只在IE下生效
    <!--[if IE]>
    这段文字只在IE浏览器显示
    <![endif]-->
    
    只在IE6下生效
    <!--[if IE 6]>
    这段文字只在IE6浏览器显示
    <![endif]-->
    
    只在IE6以上版本生效
    <!--[if gte IE 6]>
    这段文字只在IE6以上(包括)版本IE浏览器显示
    <![endif]-->
    
    只在IE8上不生效
    <!--[if ! IE 8]>
    这段文字在非IE8浏览器显示
    <![endif]-->
    
    非IE浏览器生效
    <!--[if !IE]>
    这段文字只在非IE浏览器显示
    <![endif]-->

详情参考:史上最全的CSS hack方式一览
鑫神的相关整理:http://www.zhangxinxu.com/wordpress/?s=css+hack

CSS 优化
 // 糟糕
ul#someid {..}
.menu#otherid{..}
// 好的
#someid {..}
#otherid {..},不要添加不必要的约束。
// 烂透了
html div tr td {..}
// 糟糕
.menu.left.icon {..}
// 好的
.menu-left-icon {..}
// 糟糕
.someclass {
 padding-top: 20px;
 padding-bottom: 20px;
 padding-left: 10px;
 padding-right: 10px;
 background: #000;
 background-image: url(../imgs/carrot.png);
 background-position: bottom;
 background-repeat: repeat-x;
}
// 好的
.someclass {
 padding: 20px 10px 20px 10px;
 background: #000 url(../imgs/carrot.png) repeat-x bottom;
}
// 糟糕
.someclass table tr.otherclass td.somerule {..}
//好的
.someclass .otherclass td.somerule {..}
// 糟糕
.someclass {
 color: red;
 background: blue;
 font-size: 15px;
}
.otherclass {
 color: red;
 background: blue;
 font-size: 15px;
}
// 好的
.someclass, .otherclass {
 color: red;
 background: blue;
 font-size: 15px;
}
// 糟糕
.someclass {
 color: red;
 background: blue;
 height: 150px;
 width: 150px;
 font-size: 16px;
}
.otherclass {
 color: red;
 background: blue;
 height: 150px;
 width: 150px;
 font-size: 8px;
}
// 好的
.someclass, .otherclass {
 color: red;
 background: blue;
 height: 150px;
 width: 150px;
}
.someclass {
 font-size: 16px;
}
.otherclass {
 font-size: 8px;
}
.someclass {
 /* Positioning */
 /* Display & Box Model */
 /* Background and typography styles */
 /* Transitions */
 /* Other */
}
//糟糕的
.yangshi{ 
        font-size:12px;
        border:1px solid #000000;
        padding:5px;
}
.yangshi2{ 
       font-size:12px;
       border:1px solid #000000;
       padding:5px;
}
//好的
.yangshi{ font-size:12px;border:1px solid #000000;padding:5px;}
.yangshi2{ font-size:12px;border:1px solid #000000;padding:5px;}

原文参考:css代码优化的12个技巧

上一篇下一篇

猜你喜欢

热点阅读