question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opening modal causes body scroll top or bottom

See original GitHub issue

I have an issue that wehever I open the modal the body is immediatelly scrolled to top or to bottom of the website. When the modal is closed the user is not in the place he/she opened the modal but either on the top or on the bottom of website.

I used following CSS to prevent parent element from scrolling (it allways causes the body scrolling top when modal is opened):

.ReactModal__Body--open {
    overflow: hidden;
    position: fixed;
    width: 100%;
}

When I try to use just overflow:hidden as recommended in readme.md it does not work for me - the parent element is still scrollable. When I try to use it without position: fixed as below:

.ReactModal__Body--open {
    overflow: hidden;
    width: 100%;
}

It always causes the body scroll bottom when modal is opened.

Skipping width: 100% causes the body content is shifted to the left if position: fixed is used as well.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:11
  • Comments:20

github_iconTop GitHub Comments

6reactions
lydellcommented, Jan 3, 2018

Wild guess: Could it be that you have something like:

<a href="#" onClick={() => this.openModal()}>Open modal</a>

and forgot event.preventDefault()? (You could also use a <button>.)

<a href="#" onClick={event => {event.preventDefault(); this.openModal()}}>Open modal</a>

Edit: I think this is a duplicate of https://github.com/reactjs/react-modal/issues/117#issuecomment-333409035

4reactions
TheMangoTraincommented, Aug 4, 2019

Just adding an outsider perspective. I’ve dealt with the OP’s problem (background jumps to top when opening modal) with vanilla JS modals and the culprit is often the <a href="#"> and how a browser deals with “#”.

Consider making your clickable element not <a>, perhaps a button or just a plain div with a listener. In any case, semantically, should an <a> be used for anything other than an actual link?

Read more comments on GitHub >

github_iconTop Results From Across the Web

modal window is causing the parent page body to scroll to the ...
For me, all of the modals cause the page to scroll up. This is because upon opening the modal, the class .modal-open is...
Read more >
Prevent Page Scrolling When a Modal is Open | CSS-Tricks
If we know the top of the scroll location and add it to our CSS, then the body will not scroll back to...
Read more >
How to prevent Body from scrolling when a modal is opened ...
Given an HTML document with a modal, the task is to prevent the body element from scrolling whenever the modal is in an...
Read more >
modal scrolls to top (I explain why) - WordPress.org
I've experimented in the past a bit with trying to get rid of the scrollbar on modals. In general I advise against doing...
Read more >
Modal with long content causes background to scroll...
... top or bottom of the modal, but continue to scroll. It appears that the body.modal-open CSS declaration preventing this behavior is being...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found