Bootstrap modal appearing under background

前端 未结 30 930
离开以前
离开以前 2020-11-22 17:09

I have used the code for my modal straight from the Bootstrap example, and have included only the bootstrap.js (and not bootstrap-modal.js). However, my modal is appearing u

相关标签:
30条回答
  • 2020-11-22 17:26

    Also, make sure that version of BootStrap css and js are the same ones. Different versions can also make modal appearing under background:

    For example:

    Bad:

    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    

    Good:

    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    
    0 讨论(0)
  • 2020-11-22 17:26

    I removed modal backdrop.

    .modal-backdrop {
        display:none;
    }
    

    This is not better solution, but works for me.

    0 讨论(0)
  • 2020-11-22 17:27

    I resolved my issue by adding data-backdrop="false" to the div:

    <div class="modal fade" id="exampleModalCenter" data-backdrop="false" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    
    .....
    
    0 讨论(0)
  • 2020-11-22 17:28

    Solution provided by @Muhd is the best way to do it. But if you are stuck in a situation where changing structure of the page is not an option, use this trick:

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                    aria-label="Close">
                    <span aria-hidden="true">&times;</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>
    

    The trick in here is data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);" by removing the default backdrop and create a dummy one by setting background color of the dialog itself with some alpha.

    Update 1: As pointed out by @Gustyn that clicking on the background does not close the dialog as is expected. So to achieve that you have to add some java script code. Here is some example how you can achieve this.

    $('.modal').click(function(event){
        $(event.target).modal('hide');
    });
    
    0 讨论(0)
  • 2020-11-22 17:28

    I was fixed this by adding style below to DIV tag

    style="background-color: rgba(0, 0, 0, 0.5);"
    

    And add custom css

    .modal-backdrop {position: relative;}
    
    0 讨论(0)
  • 2020-11-22 17:29

    The easiest solution I found, that worked for all my cases, was an answer in a similar post:

    Here it is:

    .modal {
      background: rgba(0, 0, 0, 0.5); 
    }
    .modal-backdrop {
      display: none;
    }
    

    Basically, the original backdrop is not used. Instead you use the modal as the backdrop. The result is that it looks and behaves exactly as the original should've. It avoids all the issues with z-indexes (for me changing z-index to 0 solved the issue, but created a new problem with my nav menu being between the modal and backdrop) and is simple.

    Credit goes to @Jevin O. Sewaruth for posting the original answer.

    0 讨论(0)
提交回复
热议问题