question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Styling <body> tag in _document - nextjs not working

See original GitHub issue

Hello Ben, Styling the custom body tag in nextjs doesn’t seem to work. added this in _document.tsx file

//_document.tsx 
import Document, { Html, Head, Main, NextScript } from 'next/document'
import tw from 'twin.macro'
...
 render() {
    return (
      <Html>
        <Head />
        <body tw="antialiased bg-black">
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ben-rogersoncommented, Jan 7, 2021

You’ll need to add the body within the stylesheet collection area instead:

Edit: This isn’t a good solution - it will duplicate the body tag 😦

// pages/_document.js

import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import tw from 'twin.macro'

export default class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const sheet = new ServerStyleSheet()
    const originalRenderPage = ctx.renderPage
    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: App => props =>
            sheet.collectStyles(
              <body css={tw`h-full mt-10 font-sans antialiased bg-yellow-700`}>
                <App {...props} />
              </body>
            )
        })
      const initialProps = await Document.getInitialProps(ctx)
      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        )
      }
    } finally {
      sheet.seal()
    }
  }
}
2reactions
ben-rogersoncommented, Jan 8, 2021

For some reason, I thought you needed to add the styling within _document.js, must have been before my morning coffee.

Yes, createGlobalStyle is how I would style the body too. GlobalStyles doesn’t accept custom styling, perhaps in the future but adding that feature is not as easy as would seem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js custom class on body using _document.js
The work around I suggest is to access the body tag from the component directly. Example: const handleClick = (e) => { document....
Read more >
Advanced Features: Custom `Document` - Next.js
A custom Document can update the <html> and <body> tags used to render a Page. This file is only rendered on the server,...
Read more >
How to add a class to the body tag in Next.js - smnh
The problem is that the _document.js is only rendered on the server. When the browser loads the first page, it will get the...
Read more >
Add custom attributes to the <body> tag #12325 - GitHub
As the question mentioned to "add a custom className to tag dynamically", I don't think the _document is the good place for that....
Read more >
How to add JavaScript tags in the body of a Next.js App
js located under the pages directory import Document, { Html, Head, Main, NextScript } from 'next/document'; class OverriddenDocument extends ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found