Tailwind CSS 中边框(border)和分割线(divider)不生效通常有以下几种原因及解决方案:
Tailwind 默认不会生成所有边框类,需要确保:
- 检查 tailwind.config.js
中是否启用了边框相关配置:
js
module.exports = {
theme: {
borderWidth: {
DEFAULT: '1px',
0: '0',
2: '2px',
4: '4px',
},
borderColor: {
DEFAULT: '#e5e7eb', // 默认灰色
red: '#ef4444', // 自定义颜色
},
}
}
常见拼写错误:
- ❌ border-1
→ ✅ border
或 border-2
(Tailwind 无 border-1
)
- ❌ divide-x-1px
→ ✅ divide-x
或 divide-x-2
分割线需要满足:
- 父元素设置 divide-y
或 divide-x
- 子元素之间必须有空格(或换行):
html
<!-- 正确示例 -->
<div class="divide-y">
<div>Item 1</div> <!-- 这里需要有换行或空格 -->
<div>Item 2</div>
</div>
如果使用 Just-in-Time (JIT) 模式:
- 确保 tailwind.config.js
中启用了 JIT:
js
module.exports = {
mode: 'jit',
// ...
}
- 动态生成的类名(如 border-[#ff0000]
)需要 JIT 支持。
尝试清除浏览器缓存或使用无痕模式测试。
html
<div class="border border-red-500 p-4">测试边框</div>
html
<div class="divide-y divide-gray-200">
<div>项目1</div>
<div>项目2</div>
</div>
如果仍不生效,检查 Tailwind 是否正常加载(查看页面源码中是否生成了对应的 CSS 类)。
使用 @apply
时漏掉类名:
/* 错误示例 */
.my-class { @apply border; } /* 缺少 border-width */
/* 正确示例 */
.my-class { @apply border border-2 border-red-500; }
通过以上步骤通常可以定位问题所在。