刚更新的css hack技巧
一 一般Hack
1概念:
不同的浏览器对CSS的解析效果不同,为了达到相同的效果,就得根据不同浏览器写不同的css
2规则:
CSS Hack大致有3种表现形式,CSS类内部Hack、选择器Hack以及HTML头部引用(if IE)Hack,CSS Hack主要针对IE浏览器。
类内部Hack:比如 IE6能识别下划线"_"和星号" * ",IE7能识别星号" * ",但不能识别下划线"_",而firefox两个都不能认识。等等
选择器Hack:比如 IE6能识别*html .class{},IE7能识别*+html .class{}或者*:first-child+html .class{}。等等
HTML头部引用(if IE)Hack:针对所有IE:<!--[if IE]><!--您的代码--><![endif]-->,针对IE6及以下版本:<!--[if lt IE 7]><!--您的代码--><[endif]-->这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效。
书写顺序,一般是将识别能力强的浏览器的CSS写在后面。
举个栗子:
background: #f00; 各浏览器都认识,主要给高级浏览器用
background: blue\; 网上说是给IE8的,不过经过测试,IE10、、8都认识他。
background:#F60\\; 这个东西是给IE8 玩
background: red\; 这个东西好玩了,所有的ie都认识他。
+background: yellow; *或+ 留给了IE7、 这一点还是不错的
_background:black; _专门留给ie6
:root .test{background: blue\;} :root是给ie9的,网上流传了个版本是 :root #test { background:blue\;},新版opera也认识,所以经过反复验证最终ie9特有的为:root 选择符 {属性\;}
| 浏览器内核 | Trident | Trident | Trident | Trident | Trident | Trident | Gecko | Presto | WebKit |
| IE6 | IE7 | IE8 | IE9 | IE10 | IE11 | FF | Opera | Sarari | |
| * | T | T | F | F | F | F | F | F | F |
| _ | T | F | F | F | F | F | F | F | F |
| !important | 见下面详解 | T | T | T | T | T | T | T | T |
| @cc_on(特性检测)激活条件编译 | 见下面详解 | 见下面详解 | 见下面详解 | 见下面详解 |
if(/*@cc_on!@*/false){ document.documentElement.className+='ie10'; } |
if (/*@cc_on!@*/true) {
document.documentElement.className += ' ie' + document.documentMode;
}
|
同IE11
|
同IE11
|
同IE11
|
| \9 | T | T | T | T | T | T | F | F | F |
| \0 | F | F | T | T | T | T | F | T | F |
| \9\0 | T,其余F |
3标准盒模型:元素宽度=width+padding+border+margin
IE: 元素宽度=width+margin(padding和border都包含在width中了)
4<!--[if IE]><![endif]-->
<!--[if IE]><link href="ie_styles.css" rel="stylesheet" type="text/css" /><![endif]-->我们可以通过这种方法在<!--[if IE]><![endif]-->里面加上只想让IE解析的东西,比如css,js,HTML。,其他浏览器会把他们当成注释。
5多类选择符的写法:
#my.c1.c2 { color:red;}
.c1.c2 { color:red;}
以上代码在IE6中会被理解为
#my.c2 { color:red;}
.c2 { color:red;}
在开发中最好不用多类选择器这样组合,因为IE6会只会读最后一个。
6!important在ie6中
识别:
<style type="text/css">
.demo{ color:red !important; }
.demo { color:green; }
</style>
<div class="demo">www.jb51.net</div>
不识别:
<style type="text/css">
.demo{
color:red !important;
color:green;
}
</style>
<div class="demo">www.jb51.net</div>
也就是说在在一个选择器中利用!important改变样式的优先级是没有用的,在不同选择器中又是可以的。
7关于ie8-9:请注意一些细小的差别:
background-color:red\;IE8和IE9都支持;
background-color:blue\\; 仅IE9支持;
background: red\; /*所有的 ie*/
:root .test{background: red\;} /*ie9*/
8@cc_on 语句的用法
/*@cc_on @*/
/*@
document.write("JScript version: " + @_jscript_version + ".");
document.write("
");
@if (@_win32)
document.write("Running on the 32-bit version of Windows.");
@elif (@_win16)
document.write("Running on the 16-bit version of Windows.");
@else
document.write("Running on a different operating system.");
@end
@*/
9火狐浏览器
@-moz-document url-prefix() { .selector { property: value; } }
上面是仅仅被Firefox浏览器识别的写法,具体如:
@-moz-document url-prefix() { .demo { color:lime; } }
支持Firefox的还有几种写法:
/* 支持所有firefox版本 */ #selector[id=selector] { property: value; } 或者: @-moz-document url-prefix() { .selector { property: value; } } /* 支持所有Gecko内核的浏览器 (包括Firefox) */ *>.selector { property: value; }
10 Webkit枘核浏览器(chrome and safari)
@media screen and (-webkit-min-device-pixel-ratio:0) { Selector { property: value; } }
上面写法主要是针对Webkit内核的浏览器,如Google Chrome 和 Safari浏览器:
@media screen and (-webkit-min-device-pixel-ratio:0) { .demo { color: #f36; } }
11 Opera浏览器
html:first-child>body Selector {property:value;} 或者: @media all and (min-width:0) { Selector {property: value;} } 或者: @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { head~body Selector { property: value; } }
上面则是Opera浏览器的Hack写法:
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { head~body .demo { background: green; } }
二、完美主义写法
这种方法是追求完美主义的写法,主要是配合我们上一节所说的IE条件注释,全部采用选择器Hack的写法。这种写法分两步:
1、创建条件样式表,并在HTML中body里添加相应的class类名:
<!–[if IE6]–><<!–[if IE7]–><!–[if IE8]–><!–[if IE9]–><!–[if !IE]–>
2、接着创建对应的样式
.demo {color: blue;}/*现代浏览器*/ .non-ie .demo {color: red;}/*除IE外浏览器*/ .ie9 .demo {color: yellow;}/*IE9浏览器*/ .ie8 .demo{color: green;}/*IE8浏览器*/ .ie7 .demo {color: orange;}/*IE7浏览器*/ .ie6 .demo {color: lime;}/*IE6浏览器*/ @media all and (min-width: 0px){ .demo {color:black;} /* webkit and opera */ } @media screen and (-webkit-min-device-pixel-ratio:0){ .demo{color:#369;}/* webkit */ } @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { head~body .demo{color:#cf6;}/* opera */ } @-moz-document url-prefix(){ .demo{color:#963;}/* firefox * / }
刚更新的css hack技巧的更多相关文章
- CSS hack技巧
CSS hack技巧一览,原文来自CSDN freshlover的博客专栏<史上最全CSS Hack方式一览> 什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6- ...
- CSS hack技巧大全
——作者:吴雷君 兼容范围: IE:6.0+,FireFox:2.0+,Opera 10.0+,Sarari 3.0+,Chrome 参考资料: 各游览器常用兼容标记一览表: 标记 IE6 IE7 I ...
- CSS Hack 技巧
IE Hack IE系列浏览器的hack大略如下: _nowamagic:1px;———–ie6 *nowamagic:1px;———–ie7 nowamagic:1px\0;———-ie89 now ...
- 2天驾驭DIV+CSS (技巧篇)(转)
这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是技巧篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” ...
- CSS hack样式兼容模式收藏
part1 —— 浏览器测试仪器,测试您现在使用的浏览器类型 IE6 IE7 IE8 Firefox Opera Safari (Chrome) IE6 IE7 IE8 ...
- css兼容技巧
CSS兼容常用技巧 请尽量用xhtml格式写代码,而且DOCTYPE影响 CSS 处理,作为W3C标准,一定要加DOCTYPE声明. 1.div的垂直居中问题 vertical-align:middl ...
- CSS Hack技术介绍及常用的Hack技巧集锦
一.什么是CSS Hack? 不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题.而这个针对不同的浏览器写不同的CS ...
- CSS Hack技术介绍及常用的Hack技巧
一.什么是CSS Hack? 不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题.而这个针对不同的浏览器写不同的CS ...
- CSS技巧(二):CSS hack
什么是CSS hack CSS hack由于不同的浏览器,比如IE6,IE7,Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果. 这个时候我们就需 ...
随机推荐
- Umbraco中的Examine Search功能讲解
转载原地址: http://24days.in/umbraco/2013/getting-started-with-examine/ Everytime I read the word Examine ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- mysql删除重复记录语句,删除除了 id 号不同,其他都相同的学生冗余信息
/** 在Mysql下执行: delete from my.stu where id not in( select min(id) id from my.stu group by code) ; 用途 ...
- java.util.Arrays的使用
在Java.util包中有一个工具类Arrays类,封装了一些操作数组的方法.简单使用如下: package com.arrays.test; import java.util.Arrays; pub ...
- iOS开发-数据持久化
iOS中四种最常用的将数据持久存储在iOS文件系统的机制 前三种机制的相同点都是需要找到沙盒里面的Documents的目录路径,附加自己相应的文件名字符串来生成需要的完整路径,再往里面创建.读取.写入 ...
- centos6.5下Python IDE开发环境搭建
自由不是想做什么就做什么,而是想不做什么就不做什么. ---摘抄于2016/11/30晚 之前学习了一段时间的Python,但所有部署都在windows上.正赶上最近在学习liux,以后 ...
- 【转】Android真机抓屏- Android Screen Monitor
http://www.cnblogs.com/xiaofeixiang/p/4086092.html 一般运行Android应用程序有两种方式一种是设置Android虚拟设备模拟器,通过Android ...
- Excel设置数据有效性实现单元格下拉菜单的3种方法(转)
http://blog.csdn.net/cdefu/article/details/4129136 一.直接输入: 1.选择要设置的单元格,譬如A1单元格: 2.选择菜单栏的“数据”→“有效性”→出 ...
- CDOJ 483 Data Structure Problem DFS
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...