Tailwind CSS 本质上就是 CSS,它的兼容性取决于两个因素:CSS 特性的浏览器支持,以及你使用的 Tailwind 功能。
Tailwind CSS 支持以下浏览器的最新版本:
| 浏览器 | 最低版本 |
|---|---|
| Chrome | 最新版 |
| Firefox | 最新版 |
| Safari | 最新版 |
| Edge | 最新版 |
| Opera | 最新版 |
对于生产环境,建议支持主流浏览器的最近两个版本。
Tailwind 使用的 CSS 特性在不同浏览器中的支持情况:
| 特性 | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
| Flexbox | ✅ 全支持 | ✅ 全支持 | ✅ 全支持 | ✅ 全支持 |
| Grid | ✅ 全支持 | ✅ 全支持 | ✅ 全支持 | ✅ 全支持 |
| CSS Variables | ✅ 49+ | ✅ 31+ | ✅ 9.1+ | ✅ 15+ |
| Gap (Flexbox) | ✅ 84+ | ✅ 63+ | ✅ 14.1+ | ✅ 84+ |
| aspect-ratio | ✅ 88+ | ✅ 89+ | ✅ 15+ | ✅ 88+ |
Flexbox 的 gap 属性在旧浏览器不支持:
<!-- 使用 gap -->
<div class="flex gap-4">
<div>项目1</div>
<div>项目2</div>
</div>
<!-- 兼容方案:使用 margin -->
<div class="flex -m-2">
<div class="m-2">项目1</div>
<div class="m-2">项目2</div>
</div>
Tailwind 4.0 开始大量使用 CSS 变量,旧浏览器需要降级:
/* Tailwind 生成的 CSS */
.text-blue-500 {
color: var(--color-blue-500);
}
/* 降级方案 */
.text-blue-500 {
color: #3b82f6;
color: var(--color-blue-500, #3b82f6);
}
视频、图片的宽高比在旧浏览器不支持:
<!-- 使用 aspect-ratio -->
<div class="aspect-video">...</div>
<!-- 兼容方案:使用 padding-bottom 技巧 -->
<div class="relative" style="padding-bottom: 56.25%">
<div class="absolute inset-0">...</div>
</div>
粘性定位在某些情况下有 bug:
<!-- 粘性定位 -->
<nav class="sticky top-0">导航</nav>
<!-- 注意:sticky 在 overflow: hidden/auto 的父元素中可能失效 -->
安装 autoprefixer 自动添加前缀:
npm install -D autoprefixer
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}
}
/* 检测是否支持 gap */
@supports (gap: 1rem) {
.flex-gap { gap: 1rem; }
}
@supports not (gap: 1rem) {
.flex-gap > * + * { margin-left: 1rem; }
}
先写基础样式,再添加高级特性:
<!-- 基础样式(所有浏览器支持) -->
<div class="bg-gray-100 p-4 rounded-lg">
<!-- 增强样式(现代浏览器) -->
<div class="bg-gray-100 p-4 rounded-lg backdrop-blur-sm">
</div>
使用系统偏好设置,兼容性好:
// tailwind.config.js
module.exports = {
darkMode: 'media',
}
<div class="bg-white dark:bg-gray-800">
自动跟随系统
</div>
手动控制,兼容所有浏览器:
// tailwind.config.js
module.exports = {
darkMode: 'class',
}
<html class="dark">
<div class="bg-white dark:bg-gray-800">
手动控制
</div>
</html>
// 检测系统偏好
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// 切换暗黑模式
function toggleDark() {
document.documentElement.classList.toggle('dark');
}
// 监听系统变化
window.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', (e) => {
if (e.matches) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
});
Tailwind 3.0+ 不再支持 IE11。如果必须支持 IE11:
npm install tailwindcss@2
配合 polyfill:
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6,Object.entries,Object.values,Array.prototype.includes"></script>
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
'postcss-preset-env': {
stage: 3,
features: {
'nesting-rules': false,
},
},
}
}
<!-- IE11 不支持 CSS 变量 -->
<div
class="bg-blue-500"
style="background-color: #3b82f6"
>
兼容 IE11
</div>
100vh 问题:
<!-- 问题:iOS Safari 的 100vh 包含地址栏 -->
<div class="h-screen">...</div>
<!-- 解决方案:使用 dvh -->
<div class="h-[100dvh]">...</div>
安全区域:
<!-- 适配 iPhone 底部安全区域 -->
<footer class="pb-[env(safe-area-inset-bottom)]">
底部内容
</footer>
某些安卓 WebView 版本较旧,需要注意:
// 检测 CSS 特性支持
function supportsCSS(property, value) {
return CSS.supports(property, value);
}
// 使用
if (supportsCSS('gap', '1rem')) {
console.log('支持 gap');
}
if (supportsCSS('aspect-ratio', '16/9')) {
console.log('支持 aspect-ratio');
}
根据用户群体确定需要支持的浏览器版本:
// .browserslistrc
> 0.5%
last 2 versions
not dead
not IE 11
确保核心功能在所有浏览器可用,高级特性作为增强:
<!-- 基础:所有浏览器 -->
<button class="bg-blue-500 text-white px-4 py-2">
按钮
</button>
<!-- 增强:现代浏览器 -->
<button class="bg-blue-500 text-white px-4 py-2 rounded-lg shadow-lg hover:shadow-xl transition-shadow">
按钮
</button>
在不同浏览器测试:
对于关键功能,准备降级方案:
<!-- 图片懒加载降级 -->
<img
class="w-full h-48 object-cover"
loading="lazy"
src="image.jpg"
alt="图片"
/>
<!-- 或使用 Intersection Observer -->