Tailwind 在 Next.js 中没有应用样式到页面,但应用到了首页。

huangapple go评论64阅读模式
英文:

Tailwind not applying styles to pages in Nextjs but applies to index page

问题

全新使用Tailwind和Nextjs,但之前使用create-react-app制作过一些项目。我正在努力弄清楚为什么Tailwind不会将样式应用于我的页面,但会将样式应用于我的首页(page.tsx)。直接在test.tsx中导入Tailwind会应用样式,但我更希望只需在layout.tsx中进行单一导入就可以将Tailwind应用于所有内容。

globals.css:

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

tailwind.config.js:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    './pages/**/*.{js,ts,jsx,tsx,mdx}',
    './components/**/*.{js,ts,jsx,tsx,mdx}',
    './app/**/*.{js,ts,jsx,tsx,mdx}',
  ],
  theme: {
    extend: {
      backgroundImage: {
        'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
        'gradient-conic':
          'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
      },
    },
  },
  plugins: [],
}

layout.tsx:

import type { Metadata } from 'next';
import './globals.css';

export const metadata: Metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

文件夹结构(仅显示相关文件和文件夹):

.
├── app
│   ├── globals.css
│   └── layout.tsx
│   └── page.tsx
├── pages
│   └── test.tsx
├── tailwind.config.js
└── package.json
英文:

Brand new to Tailwind and Nextjs, but have made some projects in the past with create-react-app. I'm struggling to figure out why Tailwind is not applying styles to my pages, but applies styles to my index page (page.tsx). Directly importing Tailwind to test.tsx applies the styles but I would rather have Tailwind be applied to everything with just the single import in layout.tsx.

globals.css:

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

tailwind.config.js:

/** @type {import(&#39;tailwindcss&#39;).Config} */
module.exports = {
  content: [
    &#39;./pages/**/*.{js,ts,jsx,tsx,mdx}&#39;,
    &#39;./components/**/*.{js,ts,jsx,tsx,mdx}&#39;,
    &#39;./app/**/*.{js,ts,jsx,tsx,mdx}&#39;,
  ],
  theme: {
    extend: {
      backgroundImage: {
        &#39;gradient-radial&#39;: &#39;radial-gradient(var(--tw-gradient-stops))&#39;,
        &#39;gradient-conic&#39;:
          &#39;conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))&#39;,
      },
    },
  },
  plugins: [],
}

layout.tsx:

import type { Metadata } from &#39;next&#39;
 
import &#39;./globals.css&#39;
 
export const metadata: Metadata = {
  title: &#39;Create Next App&#39;,
  description: &#39;Generated by create next app&#39;,
}

export default function RootLayout({
    children,
}: {
    children: React.ReactNode
}) {
    return (
        &lt;html lang=&quot;en&quot;&gt;
            &lt;body&gt;{children}&lt;/body&gt;
        &lt;/html&gt;
    )
}

Folder Structure (Only showing relevant files and folders):

.
├── app
│   ├── globals.css
│   └── layout.tsx
│   └── page.tsx
├── pages
│   └── test.tsx
├── tailwind.config.js
└── package.json

答案1

得分: 2

一切都已正确配置。我必须以某种方式刷新我的 tailwind.config.js 文件,以将 Tailwind 类应用于 test.tsx。有关更多详细信息,请参阅 Victor L 的评论。

英文:

Everything was configured correctly. I had to somehow refresh my tailwind.config.js file to apply the Tailwind classes to test.tsx. See Victor L's comments for more detail.

huangapple
  • 本文由 发表于 2023年7月3日 04:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定