Skip to content

前端兼容性不完全指南 #8

@runkingzhang

Description

@runkingzhang

常见兼容性问题

兼容性问题简分类

  • 按照所属?:CSS兼容性、JS兼容性、HTML兼容性,还可以继续细分。

兼容性文章很多很散,quirksmode,w3help文章较集中、较严谨,其他主要参考的站点还有前端观察

下面只列出部分工程中常见的兼容性问题,可忽略w3help中在quirk mode下的测试。

Quirk mode

某些特殊的doctype触发quirk mode(混杂模式、怪异模式),ie6未加doctype亦触发quirk mode。

表现之一:box model中width的计算(Demo

  • 混杂模式:border + padding + innerwidth
  • 标准模式:只有innerwidth

box

更多表现请参见Quirks mode and strict mode

quirk mode毕竟是“那个时代”的产物,渐渐离我们远去,工程上应该避免触发quirk mode。

结论:doctype推荐使用<!DOCTYPE html>

常见doctype请参考KB001: 兼容性问题与浏览器的内核及渲染模式

引发的问题

hasLayout

“The hasLayout property is a Document Object Model (DOM) property that indicates when an element has a layout. It is used internally only in quirks mode and IE7 mode to implement CSS positioning. It is not used at all in IE8 mode or IE9 Mode.”

犄角旮旯处找到上面描述,IE8和IE9标准模式下已经弃用hasLayout,下面是目录,这些信息很重要:

其他资源:

引发的问题

Block Formatting Context

Block Formatting Context,通常简称BFC

Floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport) establish new block formatting contexts for their contents.

Float

浮动有着十分严格的约束,参见W3C Float Position

Postion

了解z-index和层叠上下文stacking context

CSS Selector

完整列表参见quirksmodew3help"渲染-CSS相关",下面列举部分重要的问题:

  • LoVe HAte,了解下针对性(Specificity)这个概念,其实就是样式优先级。下面的写法是正解:

    a {font:bold 50px Verdana;}
    a:link {color:red;}    /* [0,0,1,1] */
    a:visited {color:green;}  /* [0,0,1,1] */
    a:hover {color:blue;}    /* [0,0,1,1] */
    a:active {color:yellow;}  /* [0,0,1,1] */
    a.test:link, a.test:visited, a.test:hover, a.test:active {color:black;}    /* [0,0,2,1] */
    
  • Multiple classesp.class1.class2IE6错误理解为p.class2

  • 子选择器div>p,IE6不支持

  • 相邻兄弟选择器div+p,IE6不支持

  • 属性选择器div[attr],IE6不支持

  • 伪元素

    • :first-child IE6不支持
    • :hover IE6不支持(A除外)
    • :active IE6、7不支持(A除外)
    • :focus、:before、:after IE6、7不支持

CSS Cascade

优先级升序(0为最低),请参考 CSS2.1规范Cascade Order

  1. user agent declarations
  2. user normal declarations
  3. author normal declarations
  4. author important declarations
  5. user important declarations

几处bug需要关注

其他问题

overflow

font-family,正确写法请不要随意使用引号(字体族除外)。

其他

一些心得

了解兼容性是为了写出最高质量的代码,降低后续维护难度。

太多的文章只是对官方标准的翻译(包括W3Help),你甘心永远读着别人的文章?为何不一劳永逸地搞懂问题?

所有不明白的最终都可以从官方文档中找到,请耐心尝试数次,以后就不需要求人了。

我们到底要耗多少时间才能彻底弄明白兼容问题?

  • 具体时间不知
  • 一定是积累的过程,很难一劳永逸
  • 适当的工具在手,可以减少工程上的耗时
  • 了解兼容性产生的原因,心里会更有底

兼容性分类

下面的分类同时也是调试前端bug的推荐流程:

  1. 特性支持 请使用caniuse确认核心特性的浏览器支持范围,典型如某些CSS选择器、某些CSS属性
  2. hasLayout 参见本文hasLayout章节,大部分涉及IE6/7的bug均源于此
  3. float
  4. margin
  5. table
  6. 自己不熟悉的特性往往也是bug的楦头

标准&工具

通用工具

  • 推荐quirksmode兼容表,内容庞大

  • caniuse 主要是HTML5, CSS3兼容性检测

    Compatibility tables for support of HTML5, CSS3, SVG and more in desktop and mobile browsers.
    
  • webdevout 很久前人家推荐的,留在这里作纪念

CSS

JS

  • ES5
  • ES6

HTML

  • HTML 4.01
  • HTML 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions