Preventing scrolling on the parent/body element while a modal is active in a react application

I have come across many questions similar to this. In my React project, I am trying to prevent the parent/body from scrolling when a modal/popup is in hover or focus. I want the parent scroll bar to remain visible but disabled when the modal is hovered or focused. I tried using overflow:hidden and position:fixed, but it caused the parent scroll to disappear. Is there a solution to achieve this? I am new to UI design, so any assistance would be greatly appreciated.

Answer №1

body {
  position: fixed;
  max-height: 100vh;
  overflow-y: scroll;
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempor congue leo, ullamcorper gravida dui. Integer dignissim euismod facilisis. Etiam eget accumsan justo. Ut vitae eros semper, pulvinar tortor eget, viverra metus. Proin eleifend eros
tortor, vel lobortis sem consequat quis. Phasellus euismod fermentum condimentum. Nulla id vehicula dolor, id rutrum metus. Curabitur tempor posuere enim, et accumsan lectus malesuada vitae. Morbi ultrices fringilla lacus vel ultricies. Etiam ut urna
massa. Morbi porttitor quam eget nisi volutpat, id tempor justo imperdiet. Nullam maximus venenatis turpis, a semper ligula placerat nec. Aliquam hendrerit magna a laoreet elementum. Duis efficitur, lacus sed lacinia porta, nibh ante eleifend lacus, et
viverra mi elit eget nulla. Fusce ultrices faucibus orci vel fermentum. Donec a consectetur turpis, id ultricies risus. Vestibulum iaculis porttitor justo, sit amet pellentesque est vulputate ac. Suspendisse nisi ex, gravida dapibus ipsum vitae, pharetra
efficitur tellus. Vivamus varius elementum euismod. Nunc elit diam, laoreet vel finibus at, porttitor vel est. Maecenas dignissim nibh eu nibh pellentesque ornare. Curabitur feugiat iaculis mi, ullamcorper hendrerit ex scelerisque ac. Donec blandit ipsum
sit amet nibh elementum, vitae efficitur nisi maximus. Curabitur sodales, elit a bibendum tempor, elit sem pellentesque metus, sit amet tempor diam nulla id sapien. In aliquam magna at turpis semper, et consectetur lorem egestas. Nunc ornare erat eros,
quis efficitur nibh tincidunt ac. Nunc imperdiet lectus id libero semper cursus eu vel turpis. Proin tincidunt sollicitudin metus consequat vehicula. Etiam sed nunc tincidunt, imperdiet mi eget, rutrum enim. Aenean scelerisque imperdiet tortor id sodales.
Aenean faucibus bibendum pharetra. Etiam sagittis odio nec risus malesuada egestas. Cras vel lorem a neque efficitur scelerisque. Pellentesque ut lorem id dolor varius dictum eget non metus. Nunc consectetur lectus sit amet nulla sollicitudin, eu tincidunt
neque cursus. Nullam in enim ullamcorper, ultrices velit non, suscipit lorem. Nunc eleifend urna non fermentum tincidunt. Curabitur fermentum dui eros, consectetur feugiat risus aliquam nec. Sed efficitur nec nulla eu porta. Suspendisse ipsum massa, elementum
vel blandit vel, hendrerit at nibh. Etiam tempus massa non pellentesque hendrerit. In commodo nisl quam, ut aliquet ipsum varius a. Donec magna justo, luctus eu congue tempus, feugiat ut est. Proin suscipit eu lorem vel imperdiet. Morbi suscipit nisi
sem, finibus pulvinar magna scelerisque non. Proin molestie, nisl non tincidunt suscipit, mauris nunc commodo diam, et lacinia diam libero sit amet felis. Praesent vestibulum risus id erat consequat, ac maximus mauris pulvinar. Etiam tempus interdum est
sed aliquet. In hac habitasse platea dictumst. Curabitur maximus sodales tempus. Suspendisse dapibus vestibulum blandit. Ut mollis interdum fringilla. Maecenas neque purus, rhoncus nec tortor at, elementum dictum sem. Aenean sit amet elit facilisis, euismod
felis non, elementum tortor. Mauris eget aliquam mauris, at volutpat mi. Donec in ligula ac lectus rhoncus pulvinar. Suspendisse a nulla id mi convallis congue quis vitae massa.

Answer №2

Discovered an even more accurate solution for this particular scenario. Just include the following property in the overlay class overscroll-behavior: contain

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

React-form library utilized for controlled component with pre-loaded values in inputs

I am currently utilizing react-form to construct forms for a web-based application, alongside Redux. I find myself needing to use these forms not just for entering information, but also for editing existing data. This has led me to consider the necessity ...

In a remarkable design with an array of buttons and an individual div assigned to each, a fascinating functionality unfolds. Whenever a

https://i.stack.imgur.com/emhsT.png When I click on the first "Respond" button, I want the adjacent text box to disappear. Currently, if I click on the first "Respond" button, both text boxes will disappear instead of just the first one. $('.comment ...

Can someone please explain why I keep encountering the error message "TypeError: this.state.users.map is not a function"?

I keep encountering an issue that shows up as "TypeError: this.state.users.map is not a function" even though there is a list variable in my state that is binded. I am puzzled as to why the map function is not functioning properly. getList(){ ...

What is the reason for JQuery not generating a fresh div during every loop?

I'm currently facing an issue where jQuery seems to be combining all the image divs and description divs into one container div, rather than creating individual containers for each pair in my for loop. This is causing a disruption in the overall layou ...

Webpack is refusing to compile my code after attempting to import a mongoose model

I've been working on a React blog app that utilizes MongoDB to store posts. However, I encountered an issue where webpack fails to compile when attempting to import a Mongoose model into my NewPost component. Below are the errors I'm facing: W ...

Why is React App showing up twice on the webpage?

After successfully creating a React app based on Free Code Camp's Drum Machine project that passed all tests on Code Pen, I encountered an issue when transferring the code to Visual Studio. Surprisingly, the app now fails one test (#6) even though it ...

What could be the reason for the sudden malfunction of the .tabs li:hover property on my

I'm having trouble with the :hover effect not working on my .tabs li element. I've tried commenting out my jQuery script, but it still won't apply. Can anyone help me figure out why? Here's a JSFiddle link for reference: https://jsfidd ...

What is the best way to calculate the product of two variables in JavaScript when my form is contained within a for each loop?

This form is part of a foreach loop and I'm trying to calculate the product of two inputs, "qty" and "uprice", for each row. However, currently only the result from the first row is being displayed. Form <tbody> <?php foreach($_POST[ ...

Beginning of my initial endeavor (seeking feedback, general advice, criticism)

Hey everyone! I'm looking for a forum-style platform similar to Stack Overflow where I can get some feedback on my first project. It's the first one I've built from scratch, so I'm open to any critiques or suggestions on what could be i ...

Need assistance with a coin flip using HTML and Javascript?

After spending hours trying to make the code work with the example provided, I find myself unable to get it functioning correctly. Is there anyone who can assist me in putting all of this together so that it runs smoothly? var result,userchoice; functio ...

Is it advisable to switch the doctype from HTML 4.01 or XHTML 1.0 to HTML5 for a web page?

When updating a legacy website to be more modern and "HTML5"-ish, is it considered safe to simply change the heading doctype as shown below? <!doctype html> The original doctype could be: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...

Angular refuses to showcase the object

Currently, I am working on a web application and facing an issue in parsing an object. I attempted to retrieve the IDs of all results but unfortunately, it is returning undefined in my console. Here's what I tried: var app = angular.module("DB", ...

Creating a Background Image Using CSS3: A Step-by-Step Guide

Hey there! I'm trying to figure out how to recreate the image attached using CSS3. I've tried a few online tools for generating gradients, but no luck so far. :( I have the original image, but I really want to create the background using CSS3 in ...

Countdown to Auction jQuery

I am facing a challenge in implementing an auction countdown on my web application. The issue is that only the last countdown timer is working properly. I have attempted to use browser breakpoints to address this problem, but without success so far. func ...

The Strapi plugin seems to be encountering an issue as the API is not reachable, leading to a

In an attempt to create a custom API in Strapi backend, I developed a plugin called "test" for testing purposes. However, when trying to access the test response in Postman, it displays a 404 error stating that it is not accessible. Let me provide you wit ...

The scrolling action triggered by el.scrollIntoViewIfNeeded() goes way past the top boundary

el.scrollIntoViewIfNeeded() function is used to scroll to element el if it's not within the visible browser area. Although it generally works well, I have encountered some issues when trying to use it with a fixed header. I have provided an example s ...

Unlock the powers of Express, Passport, and Redis sessions!

Lately, I have been utilizing the default MemoryStore for my Express sessions and everything has been running smoothly. However, I encountered a setback where all session data was lost between restarts. To address this issue, I am now attempting to configu ...

Automated task scheduled to execute every minute between the hours of 8am and 4.30pm using Cloudwatch Events

I am facing an issue with my working CRON schedule. It currently runs from 8am to 5pm and I need to change it to end at 4:30pm. Is it possible to set a specific half-hour time interval in CRON? Below is the current setting for my CRON: 0/1 8-17 ? * MON- ...

Communication with child processes in node.js is not done using the child_process module

Hello Everyone, I'm currently experimenting with node.js to utilize JS plugins developed by others using the child_process API. However, I'm facing an issue where the child process is not able to communicate effectively with the parent process. ...

Exploring Partial Views in Bootstrap Single Page View and AngularJS

Currently, I am utilizing Bootstrap's single page view design, specifically the one found at this link: http://www.bootply.com/85746. As my code in the view has grown to nearly 500 lines and is expected to expand further, I am seeking a method to crea ...