Trouble with displaying vertical scroll bar in React app when height is set to 100%

I am developing a React application where I need the content to always occupy at least 100% of the height to style the background accordingly. I have attempted to achieve this by setting the CSS height to 100% at the top level, which works fine. However, the problem arises when the content within the root element exceeds 100%, as the scrollbar does not appear when the content extends beyond the page. Does anyone have suggestions on how to make the vertical scrollbar show up when the height is set to 100%?

CSS

body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}

html {
  height: 100%;
}

#root {
  height: 100%;
}

Index

<html>
  <body>        
    <div id="root"></div>            
  </body>
</html>

App (content is being rendered in content)

/* eslint-disable flowtype/require-valid-file-annotation */

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Drawer from 'material-ui/Drawer';
import AppBar from 'material-ui/AppBar';
... (the rest of the code goes here)
...

export default withStyles(styles, { withTheme: true })(ResponsiveDrawer);

Answer №1

The issue is likely due to the overflow: hidden; property set on the root element. Try adding

overflow-y: scroll;
overflow-x: hidden;

Answer №2

To implement scrolling for the component, apply the following style:

style={{ overflowY: 'scroll', height: 'calc(100vh - 127px)' }}

A specific height value is required in this case.

If there is already scrolling enabled for the root element or body (which is the default), two scroll bars may be visible. In such scenarios, you will need to remove the scroll from there. This can be achieved using hooks (if using functional components).

import { useLayoutEffect } from 'react';

const MyComponent = () => {
  useLockBodyScroll();

  /-----rest of the code  
}

const useLockBodyScroll = () => {
  useLayoutEffect(() => {
    const originalOverflowStyle = window.getComputedStyle(document.body).overflow;
    document.body.style.overflow = 'hidden';

    // Restores the root/body overflow to 'scroll' when the component is unmounted
    return () => document.body.style.overflow = originalOverflowStyle;
  }, []);
}

useLayoutEffect() is a React Hooks API similar to useState()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I show the post JSON response that was retrieved in ReactJS on the same form component that was used to submit the value?

Thank you for your assistance in advance. I am currently developing a web crawler that will operate in the following steps: The user inputs the seed URL (front-end) The user clicks the submit button (front-end) The seed URL is processed by the backend usi ...

Flexible type definition including omission and [key:string]: unknown

When I write code, I like to explain it afterwards: type ExampleType = { a: string; b: boolean; c: () => any; d?: boolean; e?: () => any; [inheritsProps: string]: unknown; // If this ^ line over is removed, TypeNoC would work as expecte ...

Managing state - It is not possible to update a component (`Provider`) while rendering another component

I'm currently exploring how to utilize jotai for updating a global indicator. This indicator will toggle the LinearProgress component (true or false). Within atom.js, I've set up a basic atom to act as the indicator: export const isDownloadAtom ...

Utilizing HTML and CSS allows for creating a responsive layout with three elements such as this

Need help with CSS to align three elements in a row, where the first two are on the left and the third is on the right. The layout should adjust so that when the window size is smaller, the second element moves below the first while the third element stays ...

The inline display property does not function properly with the <li> element

Here is the HTML code snippet I am working with: <div class="nav"> <ul class="nav-dots"> <li class='ni n1'><a href="" class='nia'>dot</a></li> <li class='ni n2'><a href="" clas ...

Utilizing ReactJS material-UI to extract information from nested JSON in a table format

My application uses ReactJS on the client side and Java Jersey on the server side. The Java Jersey backend produces JSON data with nested structures as shown below: [ { "projectname": "BMI", "testRun": &qu ...

"Transcribe as HTML" for embedded IFrame components within Chrome's Inspector tool

When using Chrome's web inspector, there is a helpful "Copy as HTML" option when you right-click on a DOM element. However, it seems that this feature does not include the contents of IFrame tags, even though they are displayed in the inspector as chi ...

Aligning vertical pills to the right using Bootstrap framework

Here is the relevant code snippet that I am working with: <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <scrip ...

What is the best way to extract user input from a web form and utilize it to perform a calculation using jQuery?

Is it possible to retrieve user input from a web form and use it to perform calculations with jquery? I am interested in extracting a numerical value entered by a user in a web form. Upon clicking "NEXT", I intend to multiply this number by a predefined c ...

Axios threw an error with a 400 status code during the request

Having some trouble with my login API built in node.js. Whenever I try to call the API using Axios, it results in a request failure message in the console. This is how I am making the axios call: axios .post( "http://localhost:8080/s ...

One way to enhance Razor helpers is by integrating parameters into them

Is there a way to create an HTML Helper similar to @Html.TextBoxFor(model => model.signature) that includes a data-id parameter in the generated input, like shown below? <input type="text" name="signature" data-id="No Signature" /> I understand ...

react-navigation hook for navigating

Currently, I am utilizing the react-navigation hook and instead of repeating the hook in various components, my goal is to pass navigation as a prop. const navigation = useNavigation(); ... <MyButton resetLocation={resetLocation} navigation= ...

When working on a REST APIs project with ReactJS fetch and NodeJS, I am encountering difficulties in reading authorization headers on the server side

I'm having issues retrieving headers on the server side using ReactJS fetch to call an API on the front end. In my old project, this functionality was working perfectly fine. I'm puzzled as to why it's not working now, especially since I hav ...

What could be causing my CSS module to not have precedence over Material UI styles?

Currently, I am working with nextJs and css modules to modify the material ui classes <Toolbar classes={{ root: cn(styles.root) }} /> In order to override the material ui classes, I have followed the instructions from the material docs by using Sty ...

What is the best way to add table pagination at the bottom of a table?

Can someone provide guidance on implementing table pagination for the material-ui table below? The documentation is a bit unclear: <Table ria-label="a dense table"> <TableHead> <TableRow> ...

Is there a way to apply a class to the main div once the checkbox has been clicked?

Whenever the checkbox is checked, it should add a class to the main div. Additionally, if I try to check another checkbox, the previously checked one should have its class removed and the new one should have the class added. Here is the code snippet for r ...

jQuery function causing lag in speed

Is there a reason why this particular function is running so slowly? If no obvious issue is found, I may have to rule out jQuery as the culprit and focus on my CSS. $("#dialog-modal").dialog({ modal: true, autoOpen: false, resizable: false, ...

Customize Metadata Efficiently with NextJS 13 - Eliminate Redundant Requests

Imagine you have a server-side component located in the app directory called page.js, which represents a person: export default async function Person({ params }) { const person = await getPerson(params.id); return ( <h1> {person. ...

Button-controlled automated presentation

I have devised a method to create an automatic slideshow using JavaScript. However, I am looking to incorporate manual control buttons as well. After adding manual control code, the slideshow seems to be malfunctioning. The provided code below is used for ...

Creating a clickable color-changing grid: A step-by-step guide

In a grid of size 12 by 12, each corner will be clickable, selecting a 6x3 cell area starting from the corner. The selected cells will change color upon clicking any of the 4 corners. After selecting one corner, the remaining cells (126 cells) will be che ...