Bootstrap modal: background jumps to top on toggle
Asked Answered
M

30

120

I have a problem, with a modal. I have a button on a page, that toggles the modal. When the modal shows up, the page jumps to top.

I have done everything I could to find a solution/etc, but I am really lost.

EDIT:

I have also tried with: $('#myModal').modal('show'); but it has the exact same effect.

Morty answered 6/2, 2014 at 13:42 Comment(2)
can this help you, same issue: #12895070Piggery
this works for me bootstrap-v3-opening-a-modal-window-forces-the-page-to-scroll-up-to-the-topOthello
I
185

When the modal opens a modal-open class is set to the <body> tag. This class sets overflow: hidden; to the body. Add this rule to your stylesheet to override the bootstrap.css style:

body.modal-open {
    overflow: visible;
}

Now the scroll should stay in place.

Idiotism answered 19/2, 2014 at 13:27 Comment(8)
@pstenstrm, I have a doubt. you told to override the css for .modal-open class. But, what if there is extra closing divs, and in my answer, I have not override any classes in my resolution? It works fine after removing 2 extra divs. bs modal adding .modal-backdrop div which will get confused where it needed to be closed because of extra closing tags.Melchizedek
Does not work for me, I have the modal inside an iframe and it is scrolling to top... any ideas?Sot
This fixed it for me. Just to add the root of my problem was 'body { height: 100% }'.Perchloride
Not working for me. I've got a vanilla modal that I'm opening with .modal('show') and it still scrolls to the top of the page. Bootstrap v3.2.0Enamour
Works like charm!Anette
I had to add position: inherit to this class but worked like a charm after that.Xylograph
It works, I can scroll the screen below, and I love it.Armilla
This did not seem to work for me. Though the page did not scroll all the way to the top. rather a constant ~50 px gap. Tried with @Xylograph 's suggestion too.Fisticuffs
M
33

If you are calling modal with tag. Try removing '#' from href attribute.And call modal with data attributes.

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>
Mirtamirth answered 6/2, 2014 at 17:42 Comment(5)
I do open it exactly like you have posted there. Check out the linkMorty
if you do not use the "<button>" but <a>, as in my case, you lose the icon "finger button" when you move the mouse cursor on the object. To overcome this, I have done so: ... href = "# mai_modal_id" data-toggle = "modal" ... and I did not use data-targetPretzel
@Pretzel - Your comment worked best for my scenario.Peppery
I think, @Pretzel should write his comment as a separate answer. Only thing that helped. ThumbsUp!Worst
thank you, this was driving me nuts...I was opening with an anchor tag, changing it a div with an onclick fixed it.Box
S
24

if you are using anchor tag as <a href"#" ..> ... </a> and href="#" is creating problem, remove that. You can read more over here

Snowball answered 18/1, 2017 at 15:37 Comment(0)
W
17
$('the thing you click on').click(function ($e) {
            $e.preventDefault();
});

This will help you to stop the scroll bar going on the top.It worked for me

Witness answered 31/10, 2016 at 12:9 Comment(0)
W
6

Possibly working with modals nested somewhere in the code:

body.modal-open {
    overflow: visible;
    position: absolute;
    width: 100%;
    height:100%;
}

For me it was a combination of position, height and overflow.

Wickman answered 24/10, 2016 at 13:16 Comment(0)
R
5

I don't see a specific error, but I would advise you to check your html syntax.

A tiny test with your source gives me errors like

Line 127, Column 34: Unclosed element div.

              <div class="inner onlySides">

This could be an issue.

Retarded answered 6/2, 2014 at 13:59 Comment(2)
Thank you - I shall look into this - even though I find it hard to believe that this could trigger the issue I am experiencing.It looks like you are using a plugin to track this - if so; which?Morty
i don't use plugin except firebug. It's always a good thing to check your page validity on validator.w3.org. It would be an issue because if you have unclosed div, you will have strange behavior :)Retarded
R
5

In bootstrap 3.1.1 I solved with this solution:

body.modal-open {
    position: absolute !important;
}
Risible answered 29/7, 2016 at 8:43 Comment(1)
You saved my day @Risible Thank you so muchForrester
S
4

I had the same issue using Bootstrap 2.3.2

It was a problem for clicking on a link:

The trick was: return false;

<a href="#" class="btn" onclick="openPositionPopup(${positionReport[0]}); return false;">
     <i class="icon-map-marker"></i>
</a>

and the js:

function openModal() {
    $('#myModal').modal('show');
}
Stephenstephenie answered 3/6, 2014 at 13:38 Comment(0)
S
4

The easiest way to solve the jumping background is to define two parameters:

body.modal-open {
  overflow: visible;
  padding-right: 0 !important;
}
Spinks answered 23/6, 2019 at 17:26 Comment(0)
A
3

I tried replicating this problem on my localhost and as weird as it may seem, there is a conflict with the Bootstrap JS file that you are using.

Try commenting your code of:

<script src="/min/?f=bootstrap.min.js,modernizr.js,bootstrap-datetimepicker.js,nprogress.js,feedback.js,select2.min.js,jquery.unveil.js,equalheights.jquery.js,hogan-v2.0.0.min.edu-custom.js,jquery.visible.min.js,handlebars-v1.2.0.js,typeahead.bundle.min.js,jquery.gridster.js,cookie.jquery.js,jquery.autosize.min.js,application.js&ver=1392814384"></script>    

And instead use Bootstrap 2.3.2:

<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

This made it work for me.

I tried it with Bootstrap version 3 but that didn't work. I am still unable to pinpoint the trouble part but somewhere along Firebug threw me a e.preventDefault() is not a function error - I am guessing this should be the root cause but I am yet to compare it with the other JS files.

Averett answered 19/2, 2014 at 13:46 Comment(0)
W
3

I tried to set .modal top at the center of the screen.

.modal {
    position: absolute;
    top: 50%;
}

Just my 2cents of thoughts.

Weakminded answered 25/2, 2014 at 13:13 Comment(1)
Close enough. I won't downvote because my problem may be elsehere, but this caused the bottom half of the screen to be dimmed, but the modal was positioned towards the centre.Moorehead
M
2

you have 2 extra closing div tags just before to the <div id="footer"> or after the <div id="mainFrame" class="group"> div. I'm using visual studio 2012 express to inspect this.

Please remove these 2 extra divs and let us know how it works. I removed extra divs and removed all custom scripts. retained only jquery core and bootstrap at the footer. here is the sreenshot of final output. here is the jsbin playground for you which is working fine.

I suggest you to use headjs to load all the scripts and css files. also it is not a good practice of loadidng any scripts twice.

enter image description here

Melchizedek answered 20/2, 2014 at 7:47 Comment(2)
downvoted answers need proper comments with reasons.Melchizedek
I don't downvote you but this answer doesn't help. It's not related to any problem, doesn't help any further users (can be categorized as typo) and probabily should be a coment. That's why some folks downvote you.Ganiats
S
2

Try using this to open modal and see what happens:

<a href="#generalModal" class="btn btn-primary btn-lg" data-toggle="modal">
  Launch demo modal 
</a>
Sporting answered 21/2, 2014 at 0:54 Comment(0)
I
2
body.modal-open {
overflow: visible !important;
}

I needed the important attribute for it to fix it

Interspace answered 11/1, 2017 at 10:38 Comment(1)
This worked for me, and I had @MandM's issue (in the comments) from pstenstrm's answer: "Not working for me. I've got a vanilla modal that I'm opening with .modal('show') and it still scrolls to the top of the page. Bootstrap v3.2.0". The only weird effect it has is that now the background will scroll if you keep scrolling a modal which is taller than the window, but this is a minor issue for me. Thanks.Alembic
G
2

body.modal-open { position: inherit; }

This worked like a charm for me.

Gelding answered 29/4, 2018 at 12:8 Comment(0)
R
2

If you are facing this issue in an Angular program add the below code at the first line of .scss file.

::ng-deep {   
     body.modal-open {
          overflow: visible !important;
          position: absolute !important;   
     } 
}
Respiratory answered 9/4, 2020 at 11:58 Comment(0)
G
2

Removing the href="#" absolutely solved it in my case

Germanophile answered 11/1, 2021 at 11:34 Comment(0)
H
1

I had the same problem and I think I figure it out. You just need to place the modal HTML as a direct child of the body tag! You can place the HTML code for the button triggering the modal wherever you need though. I believe this avoids CSS inheritance and position issues that trigger this problem.

Example:

<body>
    <!-- All your other HTML code -->
    <div>
        <!-- Button trigger modal -->
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
            Launch demo modal
        </button>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
</body>
Hyperbole answered 3/9, 2014 at 17:20 Comment(1)
Thisi is the best solution. Do not search another thins.Rohr
D
1

I spent hours on this trying everything here and elsewhere. For angularjs-material usage, turned out I had to disable animation on the uibModal.open config object arg.

For example:

  $uibModal.open(
    {
      animation: false,
      component: 'myDialogComponent',
      size: 'lg',
      resolve: {
        details: function() {
          return detailsCollection;
        }
      }
    }
  );

Hope this helps someone else.

Dabbs answered 21/9, 2018 at 18:47 Comment(0)
A
1

I was also facing the same problem. Whenever modal popup opened the page scrolled to the top. I looked everywhere and tried all the ideas mentioned above but none of them really helped me. Except for the one with body.modal-open css.

So I added

body.modal-open {
        overflow: visible;
        position: relative;
        overflow-x: hidden;
    }

This to my CSS file which fixed the problem of scrolling to the top but it added 2 scrollers to the page, for which I came with a solution to actually add a CSS to the HTML tag itself. I don't know if it is ethical for coding or not but we are designers and we can give styling to HTML tag so I did. I added some jquery

$(document)
    .on('show.bs.modal', '.modal', function () { $('html').css('overflow', 'hidden')})
    .on('hidden.bs.modal', '.modal', function () { $('html').css('overflow', 'auto') })

for toggling the scrollbar of HTML and it worked for me.

Absonant answered 4/8, 2021 at 14:50 Comment(0)
U
0

This one did it for me

body.modal-open {
    overflow: inherit;
    padding-right: 0 !important;
}
Unlawful answered 10/2, 2016 at 8:13 Comment(0)
D
0

After reading dozens answers over several hours I copied the original code from the bootstrap file again and debugged step by step to see what caused that I always jump to the top. Because the actual latest version of Bootstrap 3 is showing the modal at the position you're at right now. I had found out that -webkit-transform: translate3d(0,0,0); and height: 100% in my css body-tag caused this behaviour. Maybe this helps some one.

Discriminating answered 2/11, 2016 at 16:34 Comment(0)
D
0

For me works when I changed the version from 3.1.1 to 3.3.7

If you do not have to use version 3.3.1 try replacing.

After the change, it is good to check that the rest of the function works correctly.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Diopside answered 3/11, 2017 at 9:55 Comment(0)
A
0

I tried all of the above examples - this is the only thing that worked for me:

.modal-open {

            padding-right: 0 !important;

        }

Removing the padding from the right side of the model - prevents the jump.

Appanage answered 15/10, 2018 at 7:39 Comment(0)
R
0

I got the same issue and none of answers helped me. Found a workaround which looks stupid but it works at least in my case.

I found that page is scrolling to top only once and after that next opened modals works as expected. Than I found that hiding of any element in the DOM initiates the same strange scroll. So I just creating a dummy div after page load than hidding and removing it and this solved the issue.

var $dummy= $("<div>");
$("body").append($dummy);
$dummy.hide().remove();
Rockling answered 29/1, 2019 at 12:6 Comment(0)
S
0

I find it easier with this:

overflow-y: hidden;

When applied to the body class on modal opening - this prevents the vertical scroll of the body in the background and allows the user to stay in the same place without the scroll to top also.

Schutzstaffel answered 4/11, 2020 at 13:22 Comment(0)
I
0

The default 'overflow' style of 'body' element is:

body {
    overflow: visible;
}

In my page the 'overflow' style was changed to:

body {
    overflow-y: hidden;
}

Then I've discovered that when modal is open, the 'modal-open' class is added to the 'body' element.

The 'modal-open' class mess with 'overflow' style and causes the page or inner element to scroll up.

To prevent this I need to keep the same settings before and after the 'modal-open' class is added to the 'body' element.

So I've added this section in my CSS file:

body.modal-open {
    overflow: visible;
    overflow-y: hidden;
}

Now both 'overflow' and 'overflow-y' are the same for 'body' element, before and after 'modal-open' is added to the 'body' element.

Maybe you need to add 'overflow-x' or another style element, just adapt to your case.

Ina answered 30/10, 2021 at 0:49 Comment(0)
A
-1

Try this it works perfectly

/* fix body.modal-open overflow:hidden */
body.modal-open {
    height: auto;
}
Aeriel answered 9/10, 2014 at 11:50 Comment(0)
E
-1

it's good idea for fixed it, it's look like jock!

just enough, added one of two css code in your style

.body.modal-open { overflow:visible;padding-right:0px !important;}
.body.modal-open { height:auto;padding-right:0px !important;}

https://github.com/jschr/bootstrap-modal/issues/131#issuecomment-153405449

Eulogy answered 10/8, 2018 at 10:31 Comment(0)
I
-3

If pstenstrm's answer doesn't work for you, try combining it with this replacement button HTML:

<a class="btn btn-primary btn-lg" data-toggle="modal" data-target="#generalModal" id="launchbutton" href="#launchbutton"> Launch demo modal </a>

This should keep the button on-screen.

Informative answered 26/2, 2014 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.