19

I am trying to use Flexbox in my React app to create a simple two column webpage that occupies the full width and height.

I can get this to work with HTML and CSS on their own but not within a React app.

So far I have:

:root {
  overflow-x: hidden;
  height: 100%;
}

body {
  min-height: 100%
}

.flexbox {
  height: 100%;
  display: flex;
}

.left {
  flex: 0 0 200px;
  height: 100%
}

.right {
  flex: 1
}

and:

<div class="flexbox">
  <div class="left">
    Left
  </div>
  <div class="right">
    Right
  </div>
</div>

I realise that I need to account for the additional <div id="root"></div> tag in my index.html so I have also added the following to my css:

#root {
  height: 100%;
}

And my render function:

render() {
  return (
    <div className="flexbox">
      <div className="left">Left</div>
      <div className="right">Right</div>
    </div>
  )
}

but this doesn't work. The columns exist but are not full height. Why not?

tommyd456
  • 10,443
  • 26
  • 89
  • 163

8 Answers8

38

If you use create-react-app, it adds an element with class=App and an element with id=root to the DOM. They should also get height: 100%

html, body, #root, .App {
  height: 100%;
}
Arne Jenssen
  • 1,235
  • 3
  • 14
  • 22
11

Use viewport height units in css:

#root {
    min-height: 100vh;
}
zdolny
  • 999
  • 1
  • 11
  • 21
5

You forgot that <html> is also a tag. Moreover, it is parent to all parents! That's why you should give it height of 100% as well.

html, body { height: 100%; }

Vitiok
  • 84
  • 7
4

The body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well.

However, the content of the body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.

html {
  height: 100%;
}
body {
  height: 100%;
}
Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
1

To the parent div of the page, add the following css.

position: fixed;
overflow: auto;
height: 100vh;
ShashankAC
  • 1,016
  • 11
  • 25
0

Try This:

[data-reactroot] 
        {height: 100% !important; }
Rafiqul Islam
  • 931
  • 11
  • 14
0

Alternatively, if anything suggested above doesn't seems to be working for some reason, try using normalize.css package. It wipes all browser provided CSS and creates a fresh window for the app.

   "normalize.css": "^8.0.1" //package.json (yarn add or npm install)
   import "normalize.css/normalize.css"; //in app.js

Then in css file define the body height and width as 100vh & 100vw respectively.

docs: https://necolas.github.io/normalize.css/

Sasi Kumar M
  • 2,440
  • 1
  • 23
  • 23
0

There are a lot of answers here but none of them is working. I have tested all of them. The best way to achieve minimum height in react js is to use the main tag inside your app and then add the minimum height for that tag.

Your App.js code should look like this.

<React.Fragment>
            <Header />
            <main>
                <Container>

                </Container>
            </main>
            <Footer />
</React.Fragment>

Note You can simply remove React.Fragment if you want to use empty fragments. then your code should look like this.

<>
    <Header />
    <main>

    //Write your code here

    </main>
    <Footer />
</>

Now add CSS to your CSS file.

main {
    min-height: 80vh;
}

Important Few things you have to keep in mind. Your header and footer or component call should be outside of the main tag. Like the code, I have shared above. I have called the header and footer outside the tag. If you want to use routing then simply add Router above the tag. Your code with routing and components call should look like this.

<React.Fragment>
            <Router>
                <Header />
                <main>
                    <Container>
                        <Route path='/' component={HomeScreen} exact />
                        <Route path='/products' component={ProductScreen} />
                        <Route path='/cart' component={CartScreen} />
                        <Route path='/login' component={LoginScreen} />
                    </Container>
                </main>
                <Footer />
            </Router>
        </React.Fragment>
John Zampa
  • 115
  • 1
  • 6