32

The issue is: I'm using parrallx scrolling, so I have z-index in the page now when I try to popup a box-modal by Bootstrap I get him to look like this https://www.dropbox.com/s/42tzvfppj4vh4vx/Screenshot%202013-11-11%2020.38.36.png

As you can see, the box-modal isn't on top and any mouse click disables it. if I disable this css code :

#content {
    color: #003bb3;
    position: relative;
    margin: 0 auto;
    width: 960px;
    z-index: 300;
    background: url('../images/parallax-philosophy-ltl.png') top center;
}

the box modal works. just to notice, Bootstrap default .modal is z-index:1050 , so I can't understand why it's not on top of all other context.

that's the box-modal:

   <section id="launch" class="modal hide fade">
    <header class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h3>אשר הגעה לחתונה  </h3>
    </header>
    <div class="modal-body">
        <form method="post" action="/update" id="myForm2">
            <input type="radio"  id="yes_arrive" name="arrive" checked="checked" value="yes">מגיע<br>
            <p><input type="text" required name="fsname" value="" placeholder="הכנס שם פרטי ומשפחה "></p>
            <p>כמה אנשים מגיעים?   </p>
            <select name="number" id="number">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>

            </select>
            <hr/>
            <input type="hidden" name="title" id="title" value="{{title}}">
            <input type="radio" name="arrive" id ="no_arrive" value="no">לא מגיע
            <p>סיבה לאי הגעה</p>
            <input type="text" name="no_arrive_reason" placeholder="קצר ולעניין, לא שדה חובה">
            <hr/>
            <p class="submit"><input type="submit" name="submit" value="שלח"></p>

        </form>
    </div>
    <footer class="modal-footer">
        <button class="btn btn-primary" data-dismiss="modal">Close</button>
    </footer>
</section>

and I trigger him from top menu:

  <li><a class="launch" data-toggle="modal" href="#launch">Approve</a></li>

thanks ahead.

EDIT

Solution found if anyone falls to the same problem. this is the way: add data-backdrop="false" to section or div that you contain the modal with e.g: <section id="launch" class="modal hide fade in" data-backdrop="false"> notice that it doesn't get the gray background that way, so it's kinda a dirty solution, will be happy to hear of a better one.

Dagan Bog-Computers
  • 598
  • 1
  • 5
  • 16
  • It's hard to draw conclusions from so little information. Show more code or a demo, please. – isherwood Nov 11 '13 at 22:17
  • Sounds like you just need to override the Bootstrap CSS z-index values for the modal and backdrop – Phil Nov 11 '13 at 23:23
  • 1
    tried to do that, with no succeess, data-backdrop="false" the only thing that did the trick – Dagan Bog-Computers Nov 11 '13 at 23:26
  • See related [Bootstrap modal appearing under background](http://stackoverflow.com/questions/10636667/bootstrap-modal-appearing-under-background) – razzed Oct 23 '14 at 22:43

10 Answers10

45

The problem almost always has something to do with "nested" z-indexes. As an Example:

<div style="z-index:1">
  some content A
  <div style="z-index:1000000000">
    some content B
  </div>
</div>
<div style="z-index:10">
  Some content C
</div>

if you look at the z-index only you would expect B,C,A, but because B is nested in a div that is expressly set to 1, it will show up as C,B,A.

Setting position:fixed locks the z-index for that element and all its children, which is why changing that can solve the problem.

The solution is to find the parent element that has the z-index set and either adjust the setting or move the content so the layers and their parent containers stack up the way you want. Firebug in Firefox has a tab in the far right named "Layout" and you can quickly go up the parent elements and see where the z-index is set.

Jon
  • 997
  • 9
  • 16
  • 2
    The bootstrap 3 modal shows as a z-index of 1040, setting my elements as 1041 and 1042 still doesn't show as expected. All elements inside the modal otherwise have z-index set to `auto`. There are no other z-indexes that are ancestors to the modal. `position:fixed` indeed has an effect, but then my field layout in the modal goes crazy. Any thoughts? – kross Dec 31 '14 at 19:16
  • @kross - I would still look at the element in firefox using the Layout tab in firebug and then going through each parent and seeing whether z-index is set. If z-index is not working as expected, it will have something to do with a parent element, not the elements you are expecting to behave. – Jon Jan 05 '15 at 20:42
  • my issue was that the `input` element was not `positioned`, so z-index was not applied. `modal-body` was `position: relative` (resetting my z-index foundation) so I shouldn't have worried about high z-index numbers like 1040 etc. I solved the problem by a) adding `position:relative` to the input, giving the input a `z-index:1` and giving my underlay a `z-index:0`. On non-modal pages, my underlay was `z-index:-1`, but in modal situations, it actually placed it under the modal. I think this is a browser bug but easy enough to work around. – kross Jan 05 '15 at 22:14
17

I found this question as I had a similar problem. While data-backdrop does "solve" the issue; I found another problem in my markup.

I had the button which launched this modal and the modal dialog itself was in the footer. The problem is that the footer was defined as navbar_fixed_bottom, and that contained position:fixed.

After I moved the dialog outside of the fixed section, everything worked as expected.

Igor Escobar
  • 1,047
  • 1
  • 12
  • 13
Sam Ruby
  • 4,270
  • 22
  • 21
  • 3
    Confirmed working, just move the modal-div to just before the `

    ` or something like that.

    – cederlof Feb 16 '14 at 16:04
  • On a side, note, during testing i actually moved the modal out side of the full tags by accident and it still worked. – rolinger Nov 04 '22 at 20:59
13

The modal dialog can be positioned on top by overriding its z-index property:

.modal.fade {
  z-index: 10000000 !important;
}
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
  • Is it possible to set this as attribute? (without java script) – FrenkyB Jan 19 '18 at 08:38
  • This worked for a screen that had an implementation of jQuery Impromptu, where a new Bootstrap modal caused the prompt to appear above the prompt. Now the prompt can appear above. – Adrian J. Moreno Jan 22 '20 at 16:19
5

Well, despite of this entry being very old. I was using bootstrap's modal this week and came along this "issue". My solution was somehow a mix of everything, just posting it as it may help someone :)

First check the Z-index war to get a fix there!

The first thing you can do is deactivate the modal backdrop, I had the Stackoverflow post before but I lost it, so I wont take the credit for it, keep that in mind; but it goes like this in the HTML code:

<!-- from the bootstrap's docs -->
<div class="modal fade" tabindex="-1" role="dialog" data-backdrop="false">
  <!-- mind the data-backdrop="false" -->
  <div class="modal-dialog" role="document">
    <!-- ... modal content -->
  </div>
</div>

The second approach was to have an event listener attached to the bootstrap's shown modal event. This is somehow not so pretty as you may think but maybe with some tricks by your own may somehow work. The advantage of this is that the element attaches the event listener and you can completely forget about it as long as you have the event listener attached :)

var element = $('selector-to-your-modal');

// also taken from bootstrap 3 docs
$(element).on('shown.bs.modal', function(e) {
  // keep in mind this only works as long as Bootstrap only supports 1 modal at a time, which is the case in Bootstrap 3 so far...
  var backDrop = $('.modal-backdrop');
  $(element).append($(backDrop));
});

The second.1 approach is basically the same than the previous but without the event listener.

Hope it helps someone!

3

I had this problem too. Problem is comming from html, created by bootstrap js:

<div class="modal-backdrop fade in"></div>

This line is created directly before end of <body> element. This cause "z-index stacked element problem." I believe that bootstrap .js do creation of this element wrong. If you have in mvc layout page, this script will cause still the same problem. Beter js idea cut be to get target modal id and inject this line to html before...

this.$backdrop = $(document.createElement('div'))
    .addClass('modal-backdrop ' + animate)
    .appendTo(this.$body)

SO SOLUTION IS repair bootstrap.js - part of modal:

.appendTo(this.$body)  
//REPLACE TO THIS:
 .insertBefore(this.$element) 
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Miroslav Siska
  • 401
  • 4
  • 15
  • While I didn't use this solution, learning that Bootstrap inserts the modal's backdrop just before the closing `body` tag did help me solve why the backdrop wasn't applying evenly across elements. The solution for me was to set the modal's z-index high enough to appear above all the others and set the backdrop's z-index to one less than that. – ele Feb 13 '17 at 18:12
1

ok, so if you are using bootstrap-rtl.css, what you can do is go to the following class .modal-backdrop and remove the z-index attribute. after that all should be fine

Mohammed Nafie
  • 403
  • 1
  • 5
  • 14
1

I fell into this this with using the JQLayout plugin, especially when using nested layouts and modals with Bootstrap 4.

An overriding css needs to be added to correct the behaviour,

.pane-center{
    z-index:inherit !important;
}
Marc Magon
  • 713
  • 7
  • 11
0

Try this Script:

function addclassName(){
setTimeout(function(){
            var c = document.querySelectorAll(".modal-backdrop");
            for (var i = 0; i < c.length; i++) {
                c[i].style.zIndex =  1040 + i * 20  ;
            }
            var d = document.querySelectorAll(".modal.fade");
            for(var i = 0; i<d.length; i++){
                d[i].style.zIndex = 1050 + i * 20;
            }
}, 10);

}

Rekcs
  • 869
  • 1
  • 7
  • 23
0

Resolved this issue for vue, by adding to the options an id: 'alertBox' so now every modal container has its parent set to something like alertBox__id0whatver which can easily be changed with css:

div[id*="alertBox"] { background: red; }

(meaning if id name contains ( *= ) 'alertBox' it will be applied.

Magor Menessy
  • 381
  • 4
  • 13
0

function ShowGroupModal()
{
  $('#dvGroupInfoPopup').removeClass("fade");
  return false;
}

function GroupModelReset()
{
  $('#txtGroupName').val("");
  $('#dvGroupInfoPopup').addClass("fade");
  return false;
}

function SaveClientDetails()
{
  if($('#txtName').val() == "")
  {
    alert("Please enter Client Name");
    $('#txtName').focus();
    return false;
  }
  else
  {
    alert("Client information successfully saved.");
    return false;
  }
}

function SaveGroup()
{
if($('#txtGroupName').val() == "")
  {
    alert("Please enter Group Name");
    $('#txtGroupName').focus();
    return false;
  }
  else
  {
    alert("Group information successfully saved.");
    return false;
  }
}
.fade {display:none;}
.show {display:block;}
.modalParentBox { z-index : 10040 !important;} 
.modalChildBox { z-index : 10042 !important;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--Parent Modal-->
<div class="modal show modalParentBox" id="idModalAddClient" data-bs-backdrop="static" tabindex="1" aria-labelledby="ModalAddClientLabel" aria-modal="true" role="dialog">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content border-top border-0 border-4 border-primary">
                <div class="modal-header">
                    <h5 class="modal-title" id="ModalAddClientLabel"></h5>
                    <div>
                        <i class="bx bxs-user me-1 font-22 text-primary"></i>
                    </div>
                    <h5 id="idH5AddUpdateClientHeader" class="mb-0 text-primary">Add Client</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-xl-12 mx-auto">
                            <div class="border p-3 rounded">
                                <div class="row mb-3">
                                    <label for="ddlGroupToAddClient" class="col-lg-4 col-form-label"><span class="text-danger">* </span>Group <b>:</b></label>
                                    <div class="col-lg-8">
                                        <div class="input-group">
                                            <select class="form-select" id="ddlGroupToAddClient">
                                                <option selected="selected" value="-1">-- Select Group --</option>
                                            </select>
                                            <button class="btn btn-primary" type="button" data-bs-toggle="modal" data-bs-target="#dvGroupInfoPopup" onclick="ShowGroupModal()">Add Group</button>
                                        </div>
                                        <span id="idSpnClientTypeError" name="nameSpnForClearAddClientError" class="text-danger small fw-bold"></span>
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <label for="txtName" class="col-lg-4 col-form-label"><span class="text-danger invisible">* </span>Client Name <b>:</b></label>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control" id="txtName" maxlength="150">
                                        <span id="idSpnCompanyNameError" name="nameSpnForClearAddClientError" class="text-danger small fw-bold"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button id="idBtnSaveClient" type="button" class="btn btn-sm btn-primary" onclick="SaveClientDetails()">Save</button>
                    <button type="button" class="btn btn-sm btn-danger" data-bs-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>
    
    <!--Child Popup Modal-->
    <div id="dvGroupInfoPopup" class="modal fade modalChildBox" tabindex="2" data-bs-backdrop="static" data-bs-keyboard="false" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="dvGroupInfoPopupLabel">Add Group</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" onclick="GroupModelReset()"></button>
                </div>
                <div class="modal-body">
                    <div class="row mb-2">
                        <div class="col-lg-5">
                            <label for="txtGroupName" class="form-label">Group Name:<span class="text-danger">*</span></label>
                        </div>
                        <div class="col-lg-7">
                            <input id="txtGroupName" type="text" maxlength="150" class="form-control" autocomplete="off" required />
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success btn-sm" onclick="SaveGroup()">Save Group</button>
                    <button type="button" class="btn btn-danger btn-sm" data-bs-dismiss="modal" aria-label="Close" onclick="GroupModelReset()">Cancel</button>
                </div>
            </div>
        </div>
Pargat
  • 1
  • 1