134

I am using Bootstrap and Parse framework to build a small web app. But those Bootstrap modals keep adding padding-right to the body after closed. How to solve this?

I tried to put this code in my javascript:

$('.modal').on('hide.bs.modal', function (e) {
    $("element.style").css("padding-right","0");
});

But it doesn't work. Does anybody know how to fix this?

My code:

        <button type="button" class="btn btn-lg btn-default" data-toggle="modal" data-target="#loginModal">Admin panel</button>

         <div id="loginModal" class="modal fade" role="dialog">
                  <div class="modal-dialog modal-sm">

                    <!-- Modal content -->
                    <div class="modal-content">

                      <!-- header -->
                      <div class="modal-header"> 
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Login</h4>
                      </div>

                      <!-- body -->
                      <div class="modal-body text-center" >
                          <input class='form-control' type="text" id="userName" placeholder="Username" ng-model='username'>
                          <input class='form-control' type="password" id="password" placeholder="Password" ng-model='password'>
                        
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" id="loginButton" ng-click="goToAdminPanel()">Login</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal" id="closeButton">Close</button>
                      </div>

                    </div>

                 </div>
                </div>
           </div>

        <div id="adminPanel" class="modal fade" role="dialog">
                  <div class="modal-dialog modal-lg">

                    <!-- Modal content -->
                    <div class="modal-content">

                      <!-- header -->
                      <div class="modal-header"> 
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Admin Panel</h4>
                      </div>

                      <!-- body -->
                      <div class="modal-body" >
                        
                        </div>
                        
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" id="saveButton">Save</button>

                        <button type="button" class="btn btn-default" data-dismiss="modal" id="closeButton">Close</button>

                      

                    </div>

                 </div>
                </div>
           </div>

    $scope.goToAdminPanel = function(){
    Parse.User.logIn($scope.username,$scope.password,{
        success: function(user){
            $('#loginModal').modal('hide');
            $('#adminPanel').modal();
        },
        error: function(user, error) {
            alert('Wrong username and/or password');
        }
    });
}

I am using bootstrap and Parse framework to build a small web app. But those Bootstrap modals keep adding padding-right to the body after closed. How to solve this?

phuwin
  • 3,130
  • 4
  • 26
  • 49
  • 3
    Its not just add `padding-right` but also add class as `modal-open` for `overflow: hidden` its just for hiding the *scroll-bar*. – vivekkupadhyay Sep 30 '15 at 09:30
  • @vivekkupadhyay Hi, I tried to override the `modal-open` with `padding-right:0 !important;` but it does not work, do you know why? – phuwin Sep 30 '15 at 10:38
  • please check my answer below, it surely help you. – vivekkupadhyay Sep 30 '15 at 10:39
  • You almost got it. You're firing the `padding-right` css property update on the correct event, but you're firing it on the wrong element. It needs to be fired on the `body` element. Please check out my answer below. – Sam Malayek Aug 21 '17 at 06:44
  • See: https://stackoverflow.com/a/65027287/7186739 – Billu Nov 26 '20 at 18:12

32 Answers32

153

I have just used the css fix below and it is working for me

body { padding-right: 0 !important }
HasanG
  • 12,734
  • 29
  • 100
  • 154
Ngatia Frankline
  • 2,897
  • 2
  • 20
  • 19
  • 3
    You've got the correct css property, but this needs to be fired on the `hide.bs.modal` bootstrap event. Please check out my answer below. – Sam Malayek Aug 21 '17 at 06:45
71

This might be a glitch from Bootstrap modal. From my tests, it seems like the problem is that #adminPanel is being initialized while #loginModal has not been totally closed yet. The workarounds can be removing the animation by removing the fade class on #adminPanel and #loginModal or set a timeout (~500ms) before calling $('#adminPanel').modal();. Hope this helps.

orlland
  • 1,256
  • 8
  • 12
  • 13
    I removed the class `.fade` from `#loginModal` and it works. – phuwin Sep 30 '15 at 11:50
  • 12
    What if I want to fix this, and maintain modal animation at the same time? – Leonardo Montenegro Aug 17 '17 at 20:43
  • @LeonardoMontenegro I also wanted this and recently I also posted the same question the issue was animation was removed but I fixed it by first removing the fade class just before hiding and adding it again just after that hide it worked though it's a workaround not a proper solution unless bootstrap fixes it in there file itself – Kramer Sep 05 '18 at 06:59
69
.modal-open {
    padding-right: 0px !important;
}

Just changed to

.modal {
    padding-right: 0px !important;
}
Ofisora
  • 2,707
  • 2
  • 14
  • 23
Suge
  • 2,808
  • 3
  • 48
  • 79
42

A pure CSS solution that keeps the bootstrap functionality as it should be.

body:not(.modal-open){
  padding-right: 0px !important;
}

The problem is caused by a function in the bootstrap jQuery that adds a bit of padding-right when a modal window opens if there is a scroll bar on the page. That stops your body content from shifting around when the modal opens, and it's a nice feature. But it's causing this bug.

A lot of the other solutions given here break that feature, and make your content shift slightly about when the modal opens. This solution will preserve the feature of the bootstrap modal not shifting content around, but fix the bug.

ezio4df
  • 3,541
  • 6
  • 16
  • 31
chasmani
  • 2,362
  • 2
  • 23
  • 35
  • 1
    I don't know why someone would want no CSS solution. I want no JS solution. This one looks good. – Csaba Toth Feb 06 '18 at 07:36
  • 1
    I needed this because if I Dismissed the Modal (**_after_** an Asyc-Postback inside the Modal) then it would still retain that weird padding. However, I still needed @Benjamin Oats answer too for it to all work when I had any Scrollbars on the Page. – MikeTeeVee Jun 03 '19 at 04:25
  • this one works still great with Bootstrap 4. only solution that worked for me – linus Sep 17 '21 at 06:28
18

If you're more concerned about the padding-right related thing then you can do this

jQuery:

$('#loginModal').on('show.bs.modal', function (e) {
     $('body').addClass('test');
});

this will addClass to your body and then using this

CSS:

.test[style] {
     padding-right:0 !important;
 }

and this will help you to get rid of padding-right.

But if you're also concerned about the hiding scroll then you've to add this too:

CSS:

.test.modal-open {
    overflow: auto;
 }

Here's the JSFiddle

Please have a look, it will do the trick for you.

vivekkupadhyay
  • 2,851
  • 1
  • 22
  • 35
  • Seems like it does not work. Or I did something wrong. It still adds `padding-right` to `body` and increases the amount of `padding-right` by 15px each time I close the Admin Panel. – phuwin Sep 30 '15 at 10:44
  • I've just checked your *link* it seems your code is not executing as its out-of `$(document).ready(function(){` try to put your *code* inside of it. – vivekkupadhyay Sep 30 '15 at 10:51
  • try putting an alert inside of your *code* then you can check that your *code* is not running. its working fine on fiddle and will work on your page too only if you use it accordingly – vivekkupadhyay Sep 30 '15 at 10:55
  • For a reason, the code does not run when the modal is shown. – phuwin Sep 30 '15 at 11:14
  • One thing I do not understand is that, if the Bootstrap `modal`s add a class to `body`, it should maintain the `padding-right` to 15px, not add 15px each time the `#adminPanel` is closed – phuwin Sep 30 '15 at 11:18
  • Its not just add `padding-right` but also add class as modal-open for overflow: hidden its just for hiding the **scroll-bar**, when the modal-box open, it tries to hide the scroll bar by adding the `padding-right` and when it closes it reset the `padding-right`. – vivekkupadhyay Sep 30 '15 at 11:20
  • What i don't understand from normal behaviour of bootstrap you mentioned is that it does not reset the padding-right, but add 15px padding-right to body, and increase the amount of padding right each time I closed the modals. I guess the guy below has his idea and it works. – phuwin Sep 30 '15 at 11:40
  • @PhuNguyen whatever floats your boat :) – vivekkupadhyay Sep 30 '15 at 11:41
  • 1
    @vivekkupadhyay Thank you so much for your effort. Have a nice day. – phuwin Sep 30 '15 at 11:49
  • @PhuNguyen glad to help :) – vivekkupadhyay Sep 30 '15 at 11:51
  • thank you very much... this solve my case without losing the animation – adjieq Mar 22 '20 at 13:30
14

Just open bootstrap.min.css

Find this line (default)

.modal-open{overflow:hidden;}

and change it as

.modal-open{overflow:auto;padding-right:0 !important;}

It will work for every modal of the site. You can do overflow:auto; if you want to keep scrollbar as it is while opening modal dialog.

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Ashok Sen
  • 183
  • 1
  • 3
  • @Ashok Can you please elaborate the cause of the issue? – dude May 17 '16 at 13:16
  • 9
    This solution pointed me in the right direction. But, instead of modifying bootstrap.min.css, I put .modal-open{overflow:auto;padding-right:0 !important;} in my main theme stylesheet and it worked! – Pratyush Deb May 23 '16 at 12:09
  • 5
    Never edit bootstrap's css directly. Always add the overrides to the css in your own stylesheet. Just like @PratyushDeb suggested. – dotnetengineer Jun 03 '16 at 16:08
  • This isn't a great solution because it allows the content behind the modal to be scrolled. The modal grays out that content because the user is supposed to be interacting with it and only it. – ubiquibacon Jun 29 '19 at 07:38
  • This should be most reliable answer. Thanks – Hasnat Babur Aug 31 '20 at 19:14
13

I had this same problem for a VERY long time. None of the answers here worked! But the following fixed it!

$(document.body).on('hide.bs.modal,hidden.bs.modal', function () {
    $('body').css('padding-right','0');
});

There are two events that fire on closing of a modal, first it's hide.bs.modal, and then it's hidden.bs.modal. You should be able to use just one.

Sam Malayek
  • 3,595
  • 3
  • 30
  • 46
  • 3
    It was not working for me when I added listener for both `hide.bs.modal` and `hidden.bs.modal`. But worked perfectly when I removed `hide.bs.modal` from the listener. :) – HariV Feb 22 '18 at 08:06
  • To the down-voter: Please leave constructive criticism in a comment so I can improve this answer. – Sam Malayek Jun 13 '18 at 23:37
  • @SamMalayek There are two issues, instead of `hide.bs.modal,hidden.bs.modal` one should use `show` and `shown` events, second `$('body').css('padding-right','0');` seems to be disregarded in the shown event somehow. – user31782 May 20 '22 at 05:08
5

This is what worked for me:

body.modal-open {
    overflow: auto !important;
}

// Bootstrap uses JS to set the element style so you can
// override those styles like this
body.modal-open[style] {
    padding-right: 0px !important;
}
Jack Vial
  • 2,354
  • 1
  • 28
  • 30
4

easy fix

add this class

.modal-open {
    overflow: hidden;
    padding-right: 0 !important;
}

Its a override but works

Benjamin Oats
  • 573
  • 1
  • 8
  • 25
  • 1
    This Class, combined with the answer posted by @Chasmani, made a total and complete solution for me. Also +1 for saying "_add this class_". It's crazy reading everyone's Responses here suggesting to override the Bootstrap.css file. Leave that file alone, and have your own CSS File where you apply any "_Bootstrap Overrides_". This is important so you may then document them all in one place, not lose them when updating/upgrading Bootstrap, and review them (to see if you still need the workarounds) after upgrading to a new Bootstrap Version. – MikeTeeVee Jun 03 '19 at 04:32
  • 1
    I agree @MikeTeeVee that you should definitely not edit the bootstrap css. I didn't include that in my answer because I figured its slightly out of the scope for answering this question – chasmani Jun 03 '19 at 10:18
3

I'm loading the default bootstrap 3.3.0 CSS and had a similar problem. Solved by adding this CSS:

body.modal-open { overflow:inherit; padding-right:inherit !important; }

The !important is because bootstrap modal javascript adds 15px of padding to the right programatically. My guess is that it's to compensate for the scrollbar, but I do not want that.

Gauravbhai Daxini
  • 2,032
  • 2
  • 22
  • 28
2

I just removed the fade class and change the class fade effect with animation.css. I hope this will help.

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
2

removeAttr won't work you will to remove the CSS property like this:

        $('.modal').on('hide.bs.modal', function (e) {
            e.stopPropagation();
            $('body').css('padding-right','');
        }); 

With no property value, the property is removed.

MCSI
  • 2,818
  • 26
  • 35
jcdsr
  • 1,123
  • 1
  • 17
  • 35
2

I know this is an old question, but none of the answers from here removed the problem in my similar situations and I thought it would be useful for some developers to read my solution too.

I use to add some CSS to the body when a modal is opened, in the websites where it is still used bootstrap 3.

body.modal-open{
            padding-right: 0!important;
            overflow-y:scroll;
            position: fixed;
        }
Madalina Taina
  • 1,968
  • 20
  • 26
2

With Bootstrap 4 this worked for me:

$(selector).on('hide.bs.modal', function (e) {
    $('body').css('padding-right','0');
});
michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
1

I have same problem with Bootstrap 3.3.7 and my solution for fixed navbar: Adding this style to your custom css.

.modal-open {
    padding-right: 0px !important;
    overflow-y: scroll;
}

it will make your modal showed while scroll window still working. thanks, hope this will help you too

1

My solution does not require any additional CSS.

As pointed by @Orland, one event is still happening when the other starts. My solution is about starting the second event (showing the adminPanel modal) only when the first one is finished (hiding the loginModal modal).

You can accomplish that by attaching a listener to the hidden.bs.modal event of your loginModal modal like below:

$('#loginModal').on('hidden.bs.modal', function (event) {
    $('#adminPanel').modal('show');
}

And, when necessary, just hide the loginModal modal.

$('#loginModal').modal('hide');

Of couse you can implement your own logic inside the listener in order to decide to show or not the adminPanel modal.

You can get more info about Bootstrap Modal Events here.

Good luck.

1

Bootstrap models add that padding-right to the body if the body is overflowing.

On bootstrap 3.3.6 (the version I'm using) this is the function responsible for adding that padding-right to the body element: https://github.com/twbs/bootstrap/blob/v3.3.6/dist/js/bootstrap.js#L1180

A quick workaround is to simply overwrite that function after calling the bootstrap.js file:

$.fn.modal.Constructor.prototype.setScrollbar = function () { };

This fixed the issue for me.

scottx
  • 51
  • 1
  • 4
1
body.modal-open{
  padding-right: 0 !important;
}
tian
  • 11
  • 2
1

You can use Bootstrap's existing classes to fix this by adding pe-0 to the body tag.
That translates to

.pe-0 {
    padding-right: 0 !important;
}

So if you have access to the body tag you don't need to add any additional CSS or javaScript.

Robert Went
  • 2,944
  • 2
  • 17
  • 18
0

Just remove the data-target from the button and load the modal using jQuery.

<button id='myBtn' data-toggle='modal'>open modal</button>

jQuery:

$("#myBtn").modal('show');
chharvey
  • 8,580
  • 9
  • 56
  • 95
Edison D'souza
  • 4,551
  • 5
  • 28
  • 39
0

I dug into the bootstrap javascript and found that the Modal code sets the right padding in a function called adjustDialog() which is defined on the Modal prototype and exposed on jQuery. Placing the following code somewhere to override this function to do nothing did the trick for me (although I don't know what the consequence of not setting this is yet!!)

$.fn.modal.Constructor.prototype.adjustDialog = function () { };

Phil
  • 2,232
  • 1
  • 26
  • 35
0

This can solve the problem

.shop-modal.modal.fade.in {
    padding-right: 0px !important;
}
Dinoart
  • 11
  • 1
0

This is a bootstrap bug and fixed in v4-dev: https://github.com/twbs/bootstrap/pull/18441 till then adding below CSS class should help.

.fixed-padding {
  padding-right: 0px !important;
}
0

I had that same problem using modals and ajax. It was because the JS file was referenced twice, both in the first page and in the page called by ajax, so when modal was closed, it called the JS event twice that, by default, adds a padding-right of 17px.

0
body {
    padding-right: 0 !important;
    overflow-y: scroll!important;
}

Worked for me. Note that the Scrollbar is forced in body - but if you have a "scrolling" page anyways, it doesn't matter (?)

0
$('.modal').on('hide.bs.modal,hidden.bs.modal', function () {
 setTimeout(function(){
  $('body').css('padding-right',0);
 },1000);
});

Try this

It will add the padding right 0px to the body after the modal close.

Sibaram Sahu
  • 990
  • 2
  • 13
  • 21
0

It occurs when you open a modal that previous modal is not completely closed yet. To fix it just open new modal at the time that all modal are completely closed, check codes below:

  showModal() {
    let closeModal = $('#your-modal');

    closeModal.modal('hide');
    closeModal.on('hidden.bs.modal', function() {
      $('#new-open-modal').modal('show');
    });
  }
Sakata Gintoki
  • 1,817
  • 16
  • 23
0

I now this is old, and all the solutions offered here may work. I'm just adding something new in case that could help someone: I had the same issue and noticed that opening the modal was adding a margin-right to my sidenav (probably a kind of inheritance from the padding added to the body). Adding {margin-right:0 !important;} to my sidenav did the trick.

Lo Bellin
  • 485
  • 4
  • 20
-1

As @Orland says if you are using some effect like fade then there we need to wait before displaying the modal, this is a bit hacky but will do the trick.

function defer(fn, time) {
    return function () {
        var args = arguments,
            context = this;

        setTimeout(function () {
            fn.apply(context, args);
        }, time);
    };
}

$.fn.modal.Constructor.prototype.show = defer($.fn.modal.Constructor.prototype.show, 350);
netomo
  • 104
  • 1
  • 7
-2

I hope there is still someone who needs this answer. There is a function called resetScrollbar() in bootstrap.js, for some stupid reason the developers decided to add the scrollbar dimensions to the body's padding right. so technically if you just set right-padding to an empty string it will fix the problem

wkirk
  • 9
  • 1
-2

I have tried so many things but worked small thing for me.

    body {
    font-size: 12px;
    font-family: brownFont;
    padding-right: 0 !important ;
}
Sagar Raut
  • 57
  • 1
  • 4
-2

this should fix the issue

body {
    padding: 0 !important 
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64
david
  • 19
  • 4