官方文档 https://tailwindcss.com/docs/dark-mode
默认情况下,Tailwind不启用暗模式。
启用黑暗模式
tailwind.config.js
// tailwind.config.js
module.exports = {
// media 根据系统自动切 class 手动切
darkMode: 'media',
// ...
}
使用黑暗模式
自动模式 media 下
<div class="bg-white dark:bg-gray-800">
<h1 class="text-gray-900 dark:text-white">Dark mode is here!</h1>
<p class="text-gray-600 dark:text-gray-300">
Lorem ipsum...
</p>
</div>
手动模式calss下
通过改变html标签的class属性切换
<!-- Dark mode not enabled -->
<html>
<body>
<!-- Will be white -->
<div class="bg-white dark:bg-black">
<!-- ... -->
</div>
</body>
</html>
<!-- Dark mode enabled -->
<html class="dark">
<body>
<!-- Will be black -->
<div class="bg-white dark:bg-black">
<!-- ... -->
</div>
</body>
</html>
其他样式启用黑暗模式
默认启用的样式:
backgroundColor
, borderColor
, gradientColorStops
, placeholderColor
, and textColor
.
其他样式需要添加如下:
// tailwind.config.js
module.exports = {
// ...
variants: {
extend: {
textOpacity: ['dark']
}
}
}