next.js - 如何将 Tailwind CSS 滚动平滑类添加到 Next.js

标签 next.js tailwind-css

我想在我的 Next.js 应用程序中添加滚动行为 smooth,Tailwind CSS 文档指示我们在 <html/> 中添加实用程序类.

    <html class="scroll-smooth ">
      <!-- ... -->
    </html>

此文件不包含 html标签:

  import Head from "next/head";
  import "@material-tailwind/react/tailwind.css";
  import "../styles/globals.css";
 
    function MyApp({ Component, pageProps }) {
      return (
        <>
          <Head>
            <link
              href="https://fonts.googleapis.com/icon?family=Material+Icons"
              rel="stylesheet"
            />
          </Head>
    
            <Component {...pageProps} />
    
        </>
      );
    }
    
    export default MyApp;

如何以及在何处添加 smooth-scroll我的项目中的实用程序类?

最佳答案

使用自定义 _document.js 并将其添加到那里 - Here是对它的作用的解释 -

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }

  render() {
    return (
      <Html class="scroll-smooth">
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

关于next.js - 如何将 Tailwind CSS 滚动平滑类添加到 Next.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71125642/

相关文章:

next.js - 是否可以测试 Next.js 13 API 路由处理程序?

html - 右侧的大文字推到左侧的图像

html - Tailwind CSS 在我的元素中没有发挥作用,我尝试学习这个新的 CSS 框架,我按照文档中的说明安装了所有内容,但它不起作用

css - 使用 Tailwind 时如何更改滚动条 (next.js/react)

css - 顺风居中文本(垂直/水平)

build - 我用 Tailwind 制作的 CSS 不适用于使用 gridsome for netlify 进行构建

reactjs - 我可以同时使用 Gatsby 和 NEXT.JS 吗?

javascript - 在Next js项目中使用Instascan时无法解析 'fs'

reactjs - React Typescript Hook 错误 - 该表达式不可调用

next.js - 如何让我的 NextJS 应用接受 otf 和 ttf 字体?