瀏覽器兼容性
關於flex的W3C規范:http://dev.w3.org/csswg/css-flexbox-1/
瀏覽器兼容性可以參考CanIUse:http://caniuse.com/#feat=flexbox
根據CanIUse的數據可以總結如下:
IE10部分支持2012,需要-ms-前綴
Android4.1/4.2-4.3部分支持2009,需要-webkit-前綴
Safari7/7.1/8部分支持2012,需要-webkit-前綴
IOS Safari7.0-7.1/8.1-8.3部分支持2012,需要-webkit-前綴
所以需要考慮新版本2012:http://www.w3.org/TR/2012/CR-css3-flexbox-20120918/
而Android需要考慮舊版本2009:http://www.w3.org/TR/2009/WD-css3-flexbox-20090723/
瀏覽器兼容的flex語法
/* 子元素-平均分欄 */ .flex1 { -webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */ -moz-box-flex: 1; /* OLD - Firefox 19- */ width: 20%; /* For old syntax, otherwise collapses. */ -webkit-flex: 1; /* Chrome */ -ms-flex: 1; /* IE 10 */ flex: 1; /* NEW, Spec - Opera 12.1, Firefox 20+ */ } /* 父元素-橫向排列(主軸) */ .flex-h { display: box; /* OLD - Android 4.4- */ display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */ display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */ display: -ms-flexbox; /* TWEENER - IE 10 */ display: -webkit-flex; /* NEW - Chrome */ display: flex; /* NEW, Spec - Opera 12.1, Firefox 20+ */ /* 09版 */ -webkit-box-orient: horizontal; /* 12版 */ -webkit-flex-direction: row; -moz-flex-direction: row; -ms-flex-direction: row; -o-flex-direction: row; flex-direction: row; } /* 父元素-橫向換行 */ .flex-hw { /* 09版 */ /*-webkit-box-lines: multiple;*/ /* 12版 */ -webkit-flex-wrap: wrap; -moz-flex-wrap: wrap; -ms-flex-wrap: wrap; -o-flex-wrap: wrap; flex-wrap: wrap; } /* 父元素-水平居中(主軸是橫向才生效) */ .flex-hc { /* 09版 */ -webkit-box-pack: center; /* 12版 */ -webkit-justify-content: center; -moz-justify-content: center; -ms-justify-content: center; -o-justify-content: center; justify-content: center; /* 其它取值如下: align-items 主軸原點方向對齊 flex-end 主軸延伸方向對齊 space-between 等間距排列,首尾不留白 space-around 等間距排列,首尾留白 */ } /* 父元素-縱向排列(主軸) */ .flex-v { display: box; /* OLD - Android 4.4- */ display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */ display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */ display: -ms-flexbox; /* TWEENER - IE 10 */ display: -webkit-flex; /* NEW - Chrome */ display: flex; /* NEW, Spec - Opera 12.1, Firefox 20+ */ /* 09版 */ -webkit-box-orient: vertical; /* 12版 */ -webkit-flex-direction: column; -moz-flex-direction: column; -ms-flex-direction: column; -o-flex-direction: column; flex-direction: column; } /* 父元素-縱向換行 */ .flex-vw { /* 09版 */ /*-webkit-box-lines: multiple;*/ /* 12版 */ -webkit-flex-wrap: wrap; -moz-flex-wrap: wrap; -ms-flex-wrap: wrap; -o-flex-wrap: wrap; flex-wrap: wrap; } /* 父元素-豎直居中(主軸是橫向才生效) */ .flex-vc { /* 09版 */ -webkit-box-align: center; /* 12版 */ -webkit-align-items: center; -moz-align-items: center; -ms-align-items: center; -o-align-items: center; align-items: center; } /* 子元素-顯示在從左向右(從上向下)第1個位置,用於改變源文檔順序顯示 */ .flex-1 { -webkit-box-ordinal-group: 1; /* OLD - iOS 6-, Safari 3.1-6 */ -moz-box-ordinal-group: 1; /* OLD - Firefox 19- */ -ms-flex-order: 1; /* TWEENER - IE 10 */ -webkit-order: 1; /* NEW - Chrome */ order: 1; /* NEW, Spec - Opera 12.1, Firefox 20+ */ } /* 子元素-顯示在從左向右(從上向下)第2個位置,用於改變源文檔順序顯示 */ .flex-2 { -webkit-box-ordinal-group: 2; /* OLD - iOS 6-, Safari 3.1-6 */ -moz-box-ordinal-group: 2; /* OLD - Firefox 19- */ -ms-flex-order: 2; /* TWEENER - IE 10 */ -webkit-order: 2; /* NEW - Chrome */ order: 2; /* NEW, Spec - Opera 12.1, Firefox 20+ */ }