- Chrome/Edge (Blink)
- Firefox (Gecko)
- Safari (WebKit)
- Opera (Blink)
-webkit- (Chrome, Safari, Edge, Opera)-moz- (Firefox)-ms- (Internet Explorer)-o- (旧版 Opera)/* 推荐写法:先写带前缀的,再写标准的 */
.gradient-bg {
background: -webkit-linear-gradient(red, blue); /* Safari 5.1-6 */
background: -o-linear-gradient(red, blue); /* Opera 11.1-12 */
background: -moz-linear-gradient(red, blue); /* Firefox 3.6-15 */
background: linear-gradient(red, blue); /* 标准语法 */
}
/* Flexbox 兼容性处理 */
.flex-container {
display: -webkit-box; /* 2009版语法 */
display: -moz-box;
display: -ms-flexbox; /* 2011版语法 */
display: -webkit-flex; /* 2012版语法 */
display: flex; /* 标准语法 */
}
// package.json 或 .browserslistrc
{
"browserslist": [
"> 1%" /* 全球使用率>1%的浏览器 */,
"last 2 versions" /* 每个浏览器的最后两个版本 */,
"not ie <= 11" /* 排除IE11及以下 */,
"not dead" /* 排除官方不再支持的浏览器 */
]
}
// postcss.config.js
module.exports = {
plugins: [
require("autoprefixer")({
grid: "autoplace", // 自动添加IE的grid前缀
}),
],
};
// 兼容性Mixin
@mixin flexbox() {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
@mixin transition($property) {
-webkit-transition: $property;
-moz-transition: $property;
-ms-transition: $property;
-o-transition: $property;
transition: $property;
}
@mixin gradient($from, $to) {
background-color: $to; /* 备用 */
background-image: -webkit-linear-gradient($from, $to);
background-image: -moz-linear-gradient($from, $to);
background-image: -o-linear-gradient($from, $to);
background-image: linear-gradient($from, $to);
}
// 使用示例
.container {
@include flexbox();
@include transition(all 0.3s ease);
@include gradient(#ff0000, #0000ff);
}
# 安装多个浏览器版本
# Chrome: 使用chrome://flags
# Firefox: 使用多个Profile
# Safari: 需要macOS设备
# 使用虚拟机测试IE
# Windows虚拟机 + IE Developer Tools
// 使用Jest + Puppeteer
const puppeteer = require("puppeteer");
async function testCSSSupport() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// 检查CSS特性支持
const supportsGrid = await page.evaluate(() => {
return CSS.supports("display", "grid");
});
console.log(`Supports CSS Grid: ${supportsGrid}`);
await browser.close();
}
| 特性 | Chrome | Firefox | Safari | Edge | IE | iOS Safari |
|---|---|---|---|---|---|---|
| Flexbox | 29+ | 28+ | 9+ | 12+ | 11* | 9+ |
| CSS Grid | 57+ | 52+ | 10.1+ | 16+ | 不支持 | 10.3+ |
| CSS Variables | 49+ | 31+ | 9.1+ | 15+ | 不支持 | 9.3+ |
| :has() | 105+ | 121+ | 15.4+ | 105+ | 不支持 | 15.4+ |
| Container Queries | 105+ | 110+ | 16+ | 105+ | 不支持 | 16+ |
*IE11 部分支持 Flexbox(2012 版语法)