CSS_MIGRATION.md 4.88 KB

CSS 迁移指南 - 从 Tailwind CSS 到 common.css

不支持的类及替换方案

1. 渐变背景 (bg-gradient-*)

不支持原因:uni-app x App 端不支持 linear-gradient()

替换方案:

  • bg-gradient-to-r from-blue-500 to-purple-600 → bg-blue-500
  • bg-gradient-to-br from-indigo-500 to-purple-500 → bg-indigo-500

示例:

<!-- 修改前 -->
<view class="bg-gradient-to-r from-blue-500 to-purple-600">
    <!-- 修改后 -->
    <view class="bg-blue-500"></view
></view>

2. block 类在 <text> 元素上

不支持原因:<text> 元素默认就是块级显示,不需要 block 类

替换方案:直接删除 block 类

示例:

<!-- 修改前 -->
<text class="text-lg font-bold text-gray-800 mb-4 block">标题</text>

<!-- 修改后 -->
<text class="text-lg font-bold text-gray-800 mb-4">标题</text>

3. space-y-* 和 space-x-*

不支持原因:这些类依赖 CSS 选择器,uni-app x 只支持基本的类选择器

替换方案:使用 gap-* 类(需要父元素是 flex)或为每个子元素添加 mb-* / mr-*

示例:

<!-- 修改前 -->
<view class="space-y-4">
    <view>项目1</view>
    <view>项目2</view>
</view>

<!-- 修改后 - 方案1:使用 gap -->
<view class="flex flex-col gap-4">
    <view>项目1</view>
    <view>项目2</view>
</view>

<!-- 修改后 - 方案2:使用 margin -->
<view>
    <view class="mb-4">项目1</view>
    <view>项目2</view>
</view>

4. min-h-screen

不支持原因:uni-app x 不支持 vh 单位

替换方案:使用 min-h-screen 类(已在 common.css 中定义为 min-height: 100%)或使用 style="height: 100%"

5. 伪类选择器(:hover, :focus 等)

不支持原因:uni-app x 只支持基本的类选择器

替换方案:

  • :active → 使用 active:bg-* 类(已在 common.css 中定义)
  • :hover → 使用 @touchstart 和 @touchend 事件手动控制
  • :disabled → 使用 disabled:opacity-60 类

示例:

<!-- 修改前 -->
<button class="bg-blue-500 hover:bg-blue-600">
    <!-- 修改后 -->
    <button class="bg-blue-500 active:bg-blue-600"></button>
</button>

6. transform 相关类

支持情况:transform 和 scale 在 App 端支持,但需要写在 <style> 中

替换方案:将 transform 相关样式写在 <style> 标签中

示例:

<template>
    <view :class="{ 'card-pressed': isPressed }"> </view>
</template>

<style>
.card-pressed {
    transform: scale(0.98);
}
</style>

批量替换建议

使用正则表达式批量替换

  1. 删除 block 类:

    • 查找:\s+block(?=["'])
    • 替换:(空)
  2. 替换渐变背景:

    • 查找:bg-gradient-to-[a-z]+ from-([a-z]+-\d+) to-[a-z]+-\d+
    • 替换:bg-$1
  3. 替换 space-y-*:

    • 查找:space-y-(\d+)
    • 替换:gap-$1(同时确保父元素有 flex flex-col)

已支持的 common.css 类

布局

  • flex, flex-row, flex-col
  • items-center, items-start, items-end
  • justify-center, justify-between, justify-around
  • flex-1, flex-shrink-0, flex-grow

间距

  • p-*, px-*, py-*, pt-*, pb-*, pl-*, pr-*
  • m-*, mx-*, my-*, mt-*, mb-*, ml-*, mr-*
  • gap-*

尺寸

  • w-*, h-*, w-full, h-full

颜色

  • bg-*, text-*(支持 gray, blue, red, green, yellow, purple, pink, indigo, teal 等)

文字

  • text-xs, text-sm, text-base, text-lg, text-xl, text-2xl, text-3xl
  • font-normal, font-bold
  • text-left, text-center, text-right

边框

  • border, border-2, border-4
  • border-t, border-b, border-l, border-r
  • rounded, rounded-lg, rounded-xl, rounded-2xl, rounded-full
  • rounded-t-*(单边圆角)

阴影

  • shadow-sm, shadow, shadow-md, shadow-lg, shadow-xl

其他

  • opacity-*
  • overflow-hidden, overflow-auto
  • relative, absolute
  • top-*, bottom-*, left-*, right-*

注意事项

  1. 文字样式只能用于 <text> 和 <button>:

    • color, font-size, font-weight, text-align, line-height 等
  2. 不要在 <view> 上使用文字样式:

    <!-- 错误 -->
    <view class="text-lg text-blue-500">文字</view>
    
    <!-- 正确 -->
    <view>
        <text class="text-lg text-blue-500">文字</text>
    </view>
    
  3. 只使用 px 单位:

    • 不要使用 rem, em, vh, vw 等单位
    • 使用 rpx 仅当需要根据屏幕宽度自适应时
  4. 条件编译:

    • 如果某些样式只在特定平台支持,使用条件编译: html <!-- #ifdef APP --> <view class="bg-blue-500">App 端纯色</view> <!-- #endif --> <!-- #ifndef APP --> <view style="background: linear-gradient(to right, #3b82f6, #a855f7)">Web 端渐变</view> <!-- #endif -->