反应-组件全屏幕(高度100%)

我不得不显示一个名为“ home”的 React 组件,它占据了屏幕100% 的高度。 无论我使用 CSS 或者 React inline 样式,它都不起作用。

在下面的例子中,Html尸体# app被设置为高度: CSS 为100% 。对于 。家,我使用内联样式(但不管我使用 CSS 还是内联样式都是一样的) : enter image description here 这个问题似乎来自于 <div data-reactroot data-reactid='1'>,它没有设置身高: 100% 。

如果我用 Chrome 开发工具黑了它,那就是工作: enter image description here

那么,在 React 中显示全高度组件的正确方法是什么呢?

任何帮助都是受欢迎的:)

238818 次浏览

try using !important in height. It is probably because of some other style affecting your html body.

{ height : 100% !important; }

also you can give values in VP which will set height to viee port pixel you mention likeheight : 700vp; but this wont be portable.

It annoys me for days. And finally I make use of the CSS property selector to solve it.

[data-reactroot]
{height: 100% !important; }
body{
height:100%
}


#app div{
height:100%
}

this works for me..

html, body, #app, #app>div {
height: 100%
}

This will ensure all the chain to be height: 100%

You could also do:

body > #root > div {
height: 100vh;
}

While this may not be the ideal answer but try this:

style=\{\{top:'0', bottom:'0', left:'0', right:'0', position: 'absolute'}}

It keeps the size attached to borders which is not what you want but gives you somewhat same effect.

try <div style = \{\{height:"100vh"}}> </div>

Adding this in the index.html head worked for me:

<style>
html, body, #app, #app>div { position: absolute; width: 100% !important; height: 100% !important; }
</style>

For a project using CRNA i use this in index.css

html, body, #root {
height: 100%;
}

and then in my App.css i use this

.App {
height: 100%;
}

and also set height to 100% for a div within App if there is one eg-

.MainGridContainer {
display: grid;
width: 100%;
height:100%;
grid-template-columns: 1fr 3fr;
grid-template-rows: 50px auto;
}

Despite using of React here - elements layout is completely html/css feature.

The root cause of the issue is in how height property in css works. When you are using relative values for height (in %) - this means that height will be set in relation to its parent.

So if you have a structure like html > body > div#root > div.app - to make div.app 100% height all its ancestors should have 100% height. You may play with next example:

html {
height: 100%;
}


body {
height: 100%;
}


div#root {
height: 100%; /* remove this line to see div.app is no more 100% height */
background-color: indigo;
padding: 0 30px;
}


div.app {
height: 100%;
background-color: cornsilk;
}
<div id="root">
<div class="app"> I will be 100% height if my parents are </div>
</div>

Few arguments:

  • Usage of !important - despite some time this feature is useful in ~95% of cases, it indicates a poor structure of html/css. Also, this is not a solution to the current problem.
  • Why not position: absolute. Property positon is designed to change how the element will be rendered in relation to (own position - relative, viewport - fixed, nearest parent whos position is not static - absolute). Ans despite position: absolute; top: 0; right: 0; bottom: 0; left: 0; will result in the same look - it also pushes you to change parents position to something not static - so you need to maintain 2 elements. That also causes parent div be collapsed in a line (0-height), and inner - full screen. That makes confusion in element inspector.

I managed this with a css class in my app.css

.fill-window {
height: 100%;
position: absolute;
left: 0;
width: 100%;
overflow: hidden;
}

Apply it to your root element in your render() method

render() {
return ( <div className="fill-window">{content}</div> );
}

Or inline

render() {
return (
<div style=\{\{ height: '100%', position: 'absolute', left: '0px', width: '100%', overflow: 'hidden'}}>
{content}
</div>
);
}

I had the same issue displaying my side navigation panel height to 100%.

My steps to fix it was to:

In the index.css file ------

.html {
height: 100%;
}
.body {
height:100%;
}

In the sidePanel.css (this was giving me issues):

.side-panel {
height: 100%;
position: fixed; <--- this is what made the difference and scaled to 100% correctly
}

Other attributes were taken out for clarity, but I think the issue lies with scaling the height to 100% in nested containers like how you are trying to scale height in your nested containers. The parent classes height will need to be applied the 100%. - What i'm curious about is why fixed: position corrects the scale and fails without it; this is something i'll learn eventually with some more practice.

I've been working with react for a week now and i'm a novice to web developing, but I wanted to share a fix that I discovered with scaling height to 100%; I hope this helps you or anyone who has a similar issue. Good luck!

I had trouble until i used the inspector and realized react puts everything inside a div with id='root' granting that 100% height along with body and html worked for me.

#app {
height: 100%;
min-height: 100vh;
}

Always full height of view min

<div style=\{\{ height: "100vh", background: "#2d405f" }}>
<Component 1 />
<Component 2 />
</div>

Create a div with full screen with background color #2d405f

CRA has a #root div to which we render our react app, so it should also be considered a parent div and give appropriate height according to your need. This answer is based on my experience with a similar situation and giving 100% height to #root helped me fix the height issue with one of it's child element.

This depends on the layout of your app, in my case the child was not able to takeup the given height because #root(parent) div had no specified height

Try it to solve your problem

<div style = \{\{height:"100vh"}}> </div>

Funny how this works since I thought html was the one with not full height, turns out it was the body.

Just add the below css in index.css:

body{
height: 100%;
}

There is an existing body tag? Add it in there!

I'm currently trouble shooting in NextJS 13 & Tailwind to achieve this. There's an additional layer of < div>'s that I'm unable to locate generated from Next's new AppDir.

One way to trouble shoot that nobody mentioned, which is easy to overlook is:

Open your Web Dev Tools and modify each ancestor to height:100% or in Tailwind 'h-full' and you'll save time to see if height full is the appropriate solution for your use case. I was quickly able to find out my footer component overlaps my div with this method instead of wasting time.

Edit: Reason for Next 13 user https://github.com/vercel/next.js/issues/42345