admin管理员组文章数量:1202037
Long story short is I'm working on a project where I want to have the content "fill" the vertical space below the static header. I've done this in React with tailwind like this:
<body class="flex flex-col h-screen text-gray-600 work-sans leading-normal text-base tracking-normal">
<header class="flex h-18 bg-white shadow-md">
{/* header menu options */}
</header>
<div class="flex flex-1 h-full bg-gray-200 p-6">
{/* page content */}
</div>
But with NextJS it seems to put the mounting div (i.e. <div id="__next">
) between the body and the wrest of the content. If I modify the CSS to give #__next { height: %100 }
but that makes the fill not work correctly, it overflows. So it looks like this:
<body class="flex flex-col h-screen text-gray-600 work-sans leading-normal text-base tracking-normal">
<div id="__next">
<header class="flex h-18 bg-white shadow-md">
{/* header menu options */}
</header>
<div class="flex flex-1 h-full bg-gray-200 p-6">
{/* page content */}
</div>
</div>
Here are screenshots to visually see why the extra div is causing problems: .jpg
The two possible options to solve this problem that theoretically might work are add classes to the #__next
div or mount to body instead of the #__next
div. Does anyone know how to achieve either of those?
Edit: Yes, I think I could change the layout to a fixed header and padding on top of the content element and that'd sidestep the problem and that may end up being the workaround I need but I'm still interested in knowing if either of the solutions I've mentioned are possible because if they aren't that's a technical limitation of NextJS that doesn't get much attention.
Long story short is I'm working on a project where I want to have the content "fill" the vertical space below the static header. I've done this in React with tailwind like this:
<body class="flex flex-col h-screen text-gray-600 work-sans leading-normal text-base tracking-normal">
<header class="flex h-18 bg-white shadow-md">
{/* header menu options */}
</header>
<div class="flex flex-1 h-full bg-gray-200 p-6">
{/* page content */}
</div>
But with NextJS it seems to put the mounting div (i.e. <div id="__next">
) between the body and the wrest of the content. If I modify the CSS to give #__next { height: %100 }
but that makes the fill not work correctly, it overflows. So it looks like this:
<body class="flex flex-col h-screen text-gray-600 work-sans leading-normal text-base tracking-normal">
<div id="__next">
<header class="flex h-18 bg-white shadow-md">
{/* header menu options */}
</header>
<div class="flex flex-1 h-full bg-gray-200 p-6">
{/* page content */}
</div>
</div>
Here are screenshots to visually see why the extra div is causing problems: https://i.sstatic.net/xZloH.jpg
The two possible options to solve this problem that theoretically might work are add classes to the #__next
div or mount to body instead of the #__next
div. Does anyone know how to achieve either of those?
Edit: Yes, I think I could change the layout to a fixed header and padding on top of the content element and that'd sidestep the problem and that may end up being the workaround I need but I'm still interested in knowing if either of the solutions I've mentioned are possible because if they aren't that's a technical limitation of NextJS that doesn't get much attention.
Share Improve this question edited Apr 21, 2021 at 16:04 Penny Liu 17.4k5 gold badges86 silver badges108 bronze badges asked Dec 19, 2020 at 21:46 Dillan WildingDillan Wilding 1,1112 gold badges12 silver badges30 bronze badges 6- set __next height 100% and box-sizing: border-box; padding and margin on 0 – Robert Commented Dec 19, 2020 at 22:11
- and body, html height 100% – Robert Commented Dec 19, 2020 at 22:14
- @Robert The result is the same as the overflow picture I posted to imgur – Dillan Wilding Commented Dec 20, 2020 at 0:30
- your header is fixed height. h-18, so just calc(100% - 18px) should be fine, or put this layer as absolute top 0 and min-height 100% should work as well. (but in second case you have to change zindex of header) – Robert Commented Dec 20, 2020 at 1:16
- or better. if header is always on top just make it fixed mean position: fixed and top: 0. this will remove it from __nav and height of 100 % will treated as 100% of page not plus height of header – Robert Commented Dec 20, 2020 at 1:32
4 Answers
Reset to default 12 +50I resolved the issue by removing classes from the body and applying them to the #__next
container div:
I used the approach in this example for using tailwind with Next.js.
Then I edited styles/index.css
@tailwind base;
/* Write your own custom base styles here */
/* #__next {
height: 100%;
} */
/* Start purging... */
@tailwind components;
/* Stop purging. */
html,
body {
@apply bg-gray-50 dark:bg-gray-900;
}
#__next {
@apply flex flex-col h-screen text-gray-600 leading-normal text-base tracking-normal;
}
/* Write your own custom component styles here */
.btn-blue {
@apply px-4 py-2 font-bold text-white bg-blue-500 rounded;
}
/* Start purging... */
@tailwind utilities;
/* Stop purging. */
/* Your own custom utilities */
As you said, the point is to add the classes to #__next
instead of the body
.
As you can see in the comments, it's important where to add the @apply
instruction.
The important lines are:
#__next {
@apply flex flex-col h-screen text-gray-600 leading-normal text-base tracking-normal;
}
As you've asked in your question title, this is one way to add tailwind styles to the #__next
container div.
The other solution is to set the classes after the component or page is loaded using the componentDidMount()
lifecycle hook or the useEffect
hook like this:
useEffect(() => {
document.querySelector("#__next").className =
"flex flex-col h-screen text-gray-600 leading-normal text-base tracking-normal";
}, []);
If you take a look at the Next.js documentation about Custom document you can see that the Main
component and NextScript
are the internals of Next.js and are required for the page to be properly rendered, and you can not change Next.js internals, so I think the solutions mentioned above are the best ways to add classes to the #__next container div.
There is no need to inject elements between or replace NextJS' technical root element (#__next
). Just stretch all parent elements of your app's root element to take all the screen height with 100vh
(vh is "vertical height", a unit which is 1% of the viewport height).
html, body, #__next, #app {
min-height: 100vh;
}
Your HTML might look like this:
<html>
<head><!-- head content --></head>
<body>
<div id="__next">
<div id="app"><!-- this is your app's top element --></div>
</div>
</body>
</html>
you can change your html
or body
tags by creating a custom document in ./pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
class CustomDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body className="custom-class-name">
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default CustomDocument
Take into account that these tags DO need to be included in your file: <Html>
, <Head />
, <Main />
and <NextScript />
.
Here's the full documentation
In your case, that element with the __next id is just what the element renders, and it's required for the main app to be mounted on. So you can't remove it or edit it. I think you can add some css in _document.js that targets #__next, using the @apply directive from tailwind might be the way to go instead of editing the HTML of the element (since it doesn't seem to be possible)
I found updating the styles/globals.css
to be the easiest way to achieve this.
/*
Extend the top most enclosing elements to entire height of the screen
to allow for the background image to fill the entire screen
*/
html,
body,
#__next {
height: 100%;
}
Note that that global.css
has to be imported into _app.tsx
本文标签: javascriptCan you modify NextJS mount element or add classes to next divStack Overflow
版权声明:本文标题:javascript - Can you modify NextJS mount element or add classes to __next div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738609269a2102537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论