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

What is the best way to access and verify data from an array that has been passed through props

I am working with a component that takes an array as a prop <Component runners={['1','2']}/> Inside this functional component, my goal is to generate an element for each value in the array. Here's my logic: const { runners ...

Can a create-react-app be created on a virtual private server (VPS)?

I am eager to begin working on a create-react-app project on a VPS so my client can monitor my progress. While I understand you can "Add React to a Website," I am unsure how to incorporate libraries into my site using the tutorial provided on the ReactJS ...

When you set the href attribute, you are essentially changing the destination of the link

UPDATE: I'm not referring to the status bar; instead, I'm talking about the clickable text that leads to a link. /UPDATE I've gone through a few similar posts but couldn't find a solution. I have a link that needs to be displayed. www ...

Creating responsive tabs that transition into a drop-down menu for mobile devices is a useful feature for

I am looking to create a responsive tab design that drops down like the example shown below: Desktop/Large Screen View https://i.stack.imgur.com/hiCYz.png Mobile View https://i.stack.imgur.com/gRxLv.png I have written some code for this, but I am unsure ...

Using React Native's stylesheet to apply multiple values in a CSS statement

Here's a scenario to help clarify my question: Imagine I want to add margin to an element in the following way: const myView = () => <View style={styles.viewStyle}></View> const styles = StyleSheet.create({ viewStyle: { margin: ...

Having difficulty transitioning Paper.js to work with React and Node.js

Trying to integrate paperjs code into a new react application has presented some challenges. In the realm of HTML, I currently have two animation files in paperjs, with each having its own corresponding HTML file. While the following setup works fine in ...

Data is not appearing as expected in the React component when using the data

I'm currently facing an issue while working with MUI. I am able to retrieve the list in console.log, but nothing is being displayed on the screen - no errors or data, just the console.log output. Here is a snippet of the data that I am receiving: ...

Can the typical drag and drop cursors be ignored in an HTML drag operation?

I've been working with the HTML drag and drop API to allow users to resize columns in a table. However, I've noticed that when a user starts dragging a column, the cursor changes to one of the default drag and drop effects (such as move or none). ...

I am planning to divide my web application into two sections by utilizing react router. I intend to incorporate a router within one of the routes mentioned earlier

/src |-- /components | |-- /signin | |-- SignIn.js | |-- /home | |-- Home.js | | |-- /dashboard | |-- Dashboard.js | |-- /assignee |-- /App.js |-- /index.js Dividing the project into two main parts: signi ...

Keys in React Native's ListView are used to uniquely identify

There is a warning in my app that is causing me some concern. React keeps prompting me to add keys for each row, but no matter what I try, I can't seem to include these keys. This is how my code looks: <ListView style={styles.listView} dat ...

After deploying to Firebase hosting, the next/image component is not showing any images

While my images load and display correctly on npm run dev, they do not show up after deploying my project. I have been using next/image to upload the images, storing them in an Assets folder with a format of src='/Assets/car.jpg'. How can I res ...

Using jQuery to store the last selected href/button in a cookie for easy retrieval

Recently, I've been working on a document that features a top navigation with 4 different links. Each time a link is clicked, a div right below it changes its size accordingly. My goal now is to implement the use of cookies to remember the last select ...

AWS Amplify is not displaying images correctly, although they are functioning properly in development mode

I am facing an issue with my Next.js project deployed on AWS Amplify. While images load properly in development mode, they are not loading in production mode. I have tried various solutions such as: Enabling the "Legacy cache settings" option and setting ...

Enhancing navigation functionality using CSS

I have two separate pages with navigation included in both. This way, it's easier to edit the navigation menu once instead of doing so on each page individually. However, I am now facing uncertainty on how to implement the 'active' class usi ...

The argument provided, 'Item', cannot be assigned to the parameter, 'string'. TS2345 error encountered

Encountering an issue with type 'string'. Error code TS2345 Problem: Argument of type 'Item' is not compatible with parameter of type 'string'. TS2345 filter(resortList:ResortResult[], selectedFilters:SelectedFilters) { ...

Stopping the page from scrolling back to the top when an asynchronous update occurs in Angular

Once the asynchronous request is complete, I dynamically add a new element to the top with this code snippet: Array.prototype.unshift.apply(scope.conversation, conversation.data.message); The issue arises when the added element causes the scroll position ...

When clicked, duplicate two input elements while excluding a specific element

My component renders an 'individual item' with 3 input fields. There is a button at the bottom that copies and adds another individual item on click. I am trying to figure out how to render the first two inputs without the final price input when ...

Can you utilize useSWR without placing it inside a useEffect hook in React?

Here is a snippet of my code: export default function myComponent() { async function getResults(myParams ){ const { data, error } = useSWR('/api/blah>myParams=' + myParams ); //do some other stuff... return dat ...

Attempting to show different fields depending on the chosen option

Having an issue with the signup process for my team and competition setup. I want to implement a feature where, if the user selects 'Competition', the promo code field will be displayed. However, this field should only appear when 'Competiti ...

Developing a customizable datepicker with the ability to select specific months or date ranges

I am currently developing a WebApp using flask and constructing templates in HTML/JS for the front end. I am in need of a datepicker that will provide the user with the option to choose a specific date range or select a range of months. Take a look at the ...