Opening modal causes body scroll top or bottom
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:11
- Comments:20
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Wild guess: Could it be that you have something like:
and forgot
event.preventDefault()
? (You could also use a<button>
.)Edit: I think this is a duplicate of https://github.com/reactjs/react-modal/issues/117#issuecomment-333409035
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?