【问题标题】:tailwind style not applying in next js when using official cli to generate project使用官方cli生成项目时,tailwind样式不适用于下一个js
【发布时间】:2021-11-26 07:38:45
【问题描述】:

我正在关注tailwind doc生成nextJs应用。

npx create-next-app -e with-tailwindcss .

然后我通过复制tailwind doc的代码创建了NavBar组件

NavBar.js

import React from "react";

function NavBar() {
  return (
    <nav className="flex items-center justify-between flex-wrap bg-teal-500 p-6">
      <div className="flex items-center flex-shrink-0 text-white mr-6">
        <svg
          className="fill-current h-8 w-8 mr-2"
          width="54"
          height="54"
          viewBox="0 0 54 54"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path d="M13.5 22.1c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05zM0 38.3c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05z" />
        </svg>
        <span className="font-semibold text-xl tracking-tight">Tailwind CSS</span>
      </div>
      <div className="block lg:hidden">
        <button className="flex items-center px-3 py-2 border rounded text-teal-200 border-teal-400 hover:text-white hover:border-white">
          <svg
            className="fill-current h-3 w-3"
            viewBox="0 0 20 20"
            xmlns="http://www.w3.org/2000/svg"
          >
            <title>Menu</title>
            <path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z" />
          </svg>
        </button>
      </div>
      <div className="w-full block flex-grow lg:flex lg:items-center lg:w-auto">
        <div className="text-sm lg:flex-grow">
          <a
            href="#responsive-header"
            className="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4"
          >
            Docs
          </a>
          <a
            href="#responsive-header"
            className="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4"
          >
            Examples
          </a>
          <a
            href="#responsive-header"
            className="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white"
          >
            Blog
          </a>
        </div>
        <div>
          <a
            href="#"
            className="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-teal-500 hover:bg-white mt-4 lg:mt-0"
          >
            Download
          </a>
        </div>
      </div>
    </nav>
  );
}

export default NavBar;

NavBar 的外观和文档不一样

NavBar 应该是这样的:

tailwind.config.js

module.exports = {
  mode: 'jit',
  purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

postcss.config.js

// If you want to use other PostCSS plugins, see the following:
// https://tailwindcss.com/docs/using-with-preprocessors
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

_app.js

import Layout from "../components/Layout";
import "tailwindcss/tailwind.css";

function MyApp({ Component, pageProps }) {
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  );
}

export default MyApp;

布局.js

import React from 'react';
import NavBar from '../components/NavBar';

function Layout({children}) {
    return (
        <>
        <NavBar/>
        <div>
            <main>
                {children}
            </main>
        </div>
        </>
    )
}

export default Layout

【问题讨论】:

  • 如果那是来自一些官方代码并且你除了遵循文档之外没有做任何其他事情,我认为可以在相关的 GitHub repo 上开票

标签: javascript css reactjs next.js tailwind-css


【解决方案1】:

问题是由于teal 没有与颜色相关联,因此将theme: { colors: require('tailwindcss/colors') } 添加到tailwind.config.js 可以解决此问题。

module.exports = {
  theme: {
    colors: require('tailwindcss/colors'),
  },
}

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 2022-07-14
    • 2018-06-28
    • 2022-01-04
    • 2022-11-05
    • 2021-09-24
    • 2022-01-16
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多