You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Next.js + Tailwind CSS v4 编译报错“Cannot apply unknown utility class”且样式不生效

Next.js + Tailwind CSS v4 编译报错“Cannot apply unknown utility class”且样式不生效

看起来你在Tailwind CSS v4的迁移过程中踩了几个常见的版本差异坑——这个版本和v3比改动幅度很大,尤其是CSS引入逻辑和配置细节,咱们一步步来解决你的问题:

1. 修复CSS入口文件的引入逻辑(核心问题)

Tailwind v4彻底重构了CSS的引入方式,你当前的globals.css还是v3的写法,这是样式不生效+工具类报错的主要原因。把你的文件改成下面的写法:

@import url("https://fonts.googleapis.com/css2?family=Work+Sans:ital,wght@0,100..900;1,100..900&display=swap");

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --radius: 0.5rem;
    /* 提前定义字体变量,确保主题配置里的var(--font-work-sans)能被正确解析 */
    --font-work-sans: 'Work Sans', sans-serif;
  }
}

@layer utilities {
  .flex-between {
    @apply flex justify-between items-center;
  }

  .text-30-extrabold {
    @apply text-[30px] font-extrabold text-white;
  }

  .text-30-bold {
    @apply text-[30px] font-bold text-black;
  }

  .text-30-semibold {
    @apply font-semibold text-[30px] text-black;
  }

  .text-26-semibold {
    @apply font-semibold text-[26px] text-black;
  }

  .text-24-black {
    @apply text-[24px] font-black text-black;
  }

  .text-20-medium {
    @apply font-medium text-[20px] text-black;
  }

  .text-16-medium {
    @apply font-medium text-[16px] text-black;
  }

  .text-14-normal {
    @apply font-normal text-sm text-white;
  }

  .pink_container {
    @apply w-full bg-primary min-h-[530px] flex justify-center items-center flex-col py-10 px-6;
  }

  .tag {
    @apply bg-secondary px-6 py-3 font-work-sans font-bold rounded-sm uppercase relative tag-tri;
  }

  /* 补充tag-tri的具体样式,避免被摇树优化移除 */
  .tag-tri {
    /* 比如你之前的伪元素逻辑,这里补充完整 */
  }

  .heading {
    @apply uppercase bg-black px-6 py-3 font-work-sans font-extrabold text-white sm:text-[54px] sm:leading-[64px] text-[36px] leading-[46px] max-w-5xl text-center my-5;
  }

  .sub-heading {
    @apply font-medium text-[20px] text-white max-w-2xl text-center break-words;
  }

  .section_container {
    @apply px-6 py-10 max-w-7xl mx-auto;
  }

  .card_grid {
    @apply grid md:grid-cols-3 sm:grid-cols-2 gap-5;
  }

  .card_grid-sm {
    @apply grid sm:grid-cols-2 gap-5;
  }

  .no-result {
    @apply text-black-100 text-sm font-normal;
  }

  /* profile */
  .profile_container {
    @apply w-full pb-10 pt-20 px-6 max-w-7xl mx-auto lg:flex-row flex-col flex gap-10;
  }

  .profile_card {
    @apply w-80 px-6 pb-6 pt-20 flex flex-col justify-center items-center bg-primary border-[5px] border-black shadow-100 rounded-[30px] relative z-0 h-fit max-lg:w-full;
  }

  .profile_title {
    @apply w-11/12 bg-white border-[5px] border-black rounded-[20px] px-5 py-3 absolute -top-9 after:absolute after:content-[''] after:-top-1 after:right-0 after:-skew-y-6 after:bg-black after:-z-[1] after:rounded-[20px] after:w-full after:h-[60px] before:absolute before:content-[''] before:-bottom-1 before:left-0 before:-skew-y-6 before:w-full before:h-[60px] before:bg-black before:-z-[1] before:rounded-[20px] shadow-100;
  }

  .profile_image {
    @apply rounded-full object-cover border-[3px] border-black;
  }

  /* idea details */
  .divider {
    @apply border-dotted bg-zinc-400 max-w-4xl my-10 mx-auto;
  }

  .view_skeleton {
    @apply bg-zinc-400 h-10 w-24 rounded-lg fixed bottom-3 right-3;
  }

  /* navbar */
  .avatar {
    @apply p-0 focus-visible:ring-0 bg-none rounded-full drop-shadow-md !important;
  }

  .dropdown-menu {
    @apply w-56 border-[5px] border-black bg-white p-5 rounded-2xl !important;
  }

  .login {
    @apply border-[5px] py-4 border-black bg-white text-black relative shadow-100 font-work-sans font-medium hover:shadow-none transition-all duration-500 !important;
  }

  /* searchform */
  .search-form {
    @apply max-w-3xl w-full min-h-[80px] bg-white border-[5px] border-black rounded-[80px] text-[24px] mt-8 px-5 flex flex-row items-center gap-5;
  }

  .search-input {
    @apply flex-1 font-bold placeholder:font-semibold placeholder:text-black-100 w-full h-auto outline-none;
  }

  .search-btn {
    /* 补充你的完整样式,比如@apply size-[60px] bg-black text-white rounded-full flex items-center justify-center; */
  }
}

关键改动说明

  • 移除了v3的@import "tailwindcss"系列语句,改用v4标准的@tailwind base; @tailwind components; @tailwind utilities;
  • @layer base里补充了字体变量,确保主题配置能正确引用
  • 所有自定义类都放在@layer utilities中,避免被Tailwind的摇树优化误删

2. 调整Tailwind配置文件的细节

你的配置文件大部分是对的,只需补充依赖安装验证,确保v4兼容:

import type { Config } from "tailwindcss";

const config: Config = {
  darkMode: "class",
  content: [
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "./sanity/**/*.{js,ts,jsx,tsx,mdx}",
  ],
  theme: {
    extend: {
      screens: {
        xs: "475px",
      },
      colors: {
        primary: {
          "100": "rgb(255 232 240)",
          DEFAULT: "#EE2B69",
        },
        secondary: "#FBE843",
        white: {
          "100": "rgb(247 247 247)",
          DEFAULT: "#FFFFFF",
        },
        black: {
          "100": "rgb(51 51 51)",
          "200": "rgb(20 20 19)",
          "300": "rgb(125 128 135)",
          DEFAULT: "#000000",
        },
      },
      fontFamily: {
        "work-sans": ["var(--font-work-sans)"],
      },
      borderRadius: {
        lg: "var(--radius)",
        md: "calc(var(--radius) - 2px)",
        sm: "calc(var(--radius) - 4px)",
      },
      boxShadow: {
        100: "2px 2px 0px 0px rgb(0, 0, 0)",
        200: "2px 2px 0px 2px rgb(0, 0, 0)",
        300: "2px 2px 0px 2px rgb(238, 43, 105)",
      },
    },
  },
  plugins: [require("tailwindcss-animate"), require("@tailwindcss/typography")],
};

export default config;

关键验证

  • 重新安装最新版依赖确保兼容性:
    npm install tailwindcss@latest tailwindcss-animate @tailwindcss/typography
    
  • v4兼容v3的主题扩展语法,你的颜色、阴影等定义无需大改

3. 剩余问题排查

重启Next.js开发服务器后,如果还有报错:

  1. 检查报错类名的拼写(比如bg-primary是否写成bg-priamry
  2. 确认用到该类的文件在content数组中被包含
  3. 打开浏览器开发者工具,检查Tailwind生成的样式是否正确加载

Tailwind v4的迁移核心就是CSS引入和配置细节的调整,改对这两点后,大部分v3代码都能无缝兼容~

内容来源于stack exchange

火山引擎 最新活动