CSS饥人谷技术博客

关于浏览器兼容

2017-03-22  本文已影响97人  萧雪圣

什么是 CSS hack

不同厂商的浏览器或者同一个厂商的浏览器的不同版本,对CSS的解析认识会有差异,这就会导致页面产生不一样的效果。
所以我们需要针对不一样的浏览器编写不同的CSS代码,来实现相同的页面效果,这个过程就叫做CSS hack。


谈一谈浏览器兼容的思路

1. 产品的受众,及受众使用的各浏览器的比例,效果优先还是基本功能优先。
2. 成本的考虑,投入产出比。
3. 兼容哪些浏览器,兼容到什么版本,让哪些浏览器支持哪些效果。
4. 渐进增强和优雅降级的选择
5. 根据兼容需求选择技术框架,比如:

Bootstrap (>=ie8)
jQuery 1.~ (>=ie6), jQuery 2.~ (>=ie9)
Vue (>= ie9)

6. 根据兼容需求选择兼容工具html5shiv.js、respond.js、css reset、Modernizr、postCSS
7. 条件注释、CSS Hack、js 能力检测做一些修补。

列举5种以上浏览器兼容的写法

1. 条件注释

条件注释 (conditional comment) 是于HTML源码中被IE有条件解释的语句。条件注释可被用来向IE提供及隐藏代码。

    <!--[if IE 6]>
    <p>You are using Internet Explorer 6.</p>
    <![endif]-->
    <!--[if !IE]><!-->
    <script>alert(1);</script>
    <!--<![endif]-->
    <!--[if IE 8]>
    <link href="ie8only.css" rel="stylesheet">
    <![endif]-->

使用了条件注释的页面在 Windows Internet Explorer 9 中可正常工作,但在 Internet Explorer 10 中无法正常工作。

条件注释.png

2. 属性前缀法

.box{
  color: red;
  _color: blue; /*ie6*/
  *color: pink; /*ie67*/
  color: yellow\9;  /*ie/edge 6-8*/
}
属性兼容.png

3. 选择器前缀法

*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有效等等

4.条件注释结合类选择器整体优化

<!DOCTYPE html>
<!--[if IEMobile 7 ]> <html dir="ltr" lang="en-US"class="no-js iem7"> <![endif]-->
<!--[if lt IE 7 ]> <html dir="ltr" lang="en-US" class="no-js ie6 oldie"> <![endif]-->
<!--[if IE 7 ]> <html dir="ltr" lang="en-US" class="no-js ie7 oldie"> <![endif]-->
<!--[if IE 8 ]> <html dir="ltr" lang="en-US" class="no-js ie8 oldie"> <![endif]-->
<!--[if (gte IE 9)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html dir="ltr" lang="en-US" class="no-js"><!--<![endif]-->

5. 利用Modernizr工具

运行的时候它会在html元素上添加一批CSS的class名称,这些class名称标记当前浏览器支持哪些特性和不支持哪些特性,支持的特性就直接显示该天特性的名称作为一个class(例:canvas,websockets),不支持的特性显示的class是“no-特性名称”。以下是IE9下生成的特征类型。

<html class=" js no-flexbox canvas canvastext no-webgl no-touch geolocation 
              postmessage no-websqldatabase no-indexeddb hashchange no-history 
        draganddrop no-websockets rgba hsla multiplebgs backgroundsize 
        no-borderimage borderradius boxshadow no-textshadow opacity 
        no-cssanimations no-csscolumns no-cssgradients no-cssreflections
        csstransforms no-csstransforms3d no-csstransitions fontface 
        generatedcontent video audio localstorage sessionstorage 
        no-webworkers no-applicationcache svg inlinesvg smil svgclippaths">

可以直接使用Modernizr在元素里生成的class名称,在你的css文件里定义相应的属性以便支持当前浏览器。例如,下面的代码可以属性,在支持shadow阴影的浏览器显示shadow,不支持的浏览器显示标准的边框:

.boxshadow #MyContainer { 
border: none;
 -webkit-box-shadow: #666 1px 1px 1px;
 -moz-box-shadow: #666 1px 1px 1px;
} 
.no-boxshadow #MyContainer {
 border: 2px solid black;
}

以下工具/名词是做什么的

一般在哪个网站查询属性兼容性?

caniuse.com

CSS hack

上一篇 下一篇

猜你喜欢

热点阅读