【问题标题】:Bootstrap 4 modal appears behind background (Django app)Bootstrap 4 模态出现在背景后面(Django 应用程序)
【发布时间】:2019-05-29 15:23:27
【问题描述】:

我已经彻底研究过这个问题,但是所有的解决方案都来自旧版本的 Bootstrap,它们没有帮助。

我的问题是当我按下按钮时,模态出现在背景后面。我发现的所有解决方案都说父容器/模态不能具有固定或相对位置。但是,我的模态框没有父容器,也没有为模态框编写任何自定义 CSS。

我认为问题可能在于我在这个 HTML 文件中使用 Django。这是代码 -

example.html

{% extends 'main/base.html' %}
{% load static %}

{% block title %}Example Title{% endblock %}

{% block fullscreen_content %}
  <div id="map-canvas" style="height: calc(100% - 50px); width: 100%;"></div>
  
	<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
  </button>

  <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
{% endblock %}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> 

base.html

{% load static %}
<!doctype html>
<html>
  <head>
    <title>{% block title %}{% endblock %}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="shortcut icon" href="{% static 'images/favicon.ico' %}">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
    <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/4.0.0-beta.2/flatly/bootstrap.min.css"> -->
    <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
    <link href="{% static 'style.css' %}" rel="stylesheet" type="text/css">
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
    {% block header %}{% endblock %}
  </head>
  <body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <a class="navbar-brand" href="{% url 'shelter:index' %}">Example</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          {% with 'example example example' as tabs %}
          {% for tab in tabs.split %}
            <li class="nav-item {% if page == tab %}active{% endif %}">
              <a href="{% url 'shelter:'|add:tab %}" class="nav-link">{{ tab|title }}</a>
            </li>
          {% endfor %}
          {% endwith %}
        </ul>
        {% if request.user.is_authenticated %}
        <ul class="navbar-nav">
          <li class="nav-item">
            <a href="{% url 'shelter:logout' %}" class="nav-link">Log out</a>
          </li>
        </ul>
        {% endif %}
      </div>
    </nav>
    {% block fullscreen_content %}{% endblock %}

    <div class="container">
      <div class="page">{% block content %}{% endblock %}</div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
    {% block footer %}{% endblock %}
  </body>
</html>

我删除/编辑了代码中的敏感部分或不必要的部分,因此希望有足够的内容可以继续。谢谢!

编辑 - 这是截图

【问题讨论】:

    标签: html css django bootstrap-4 bootstrap-modal


    【解决方案1】:

    在您的主 CSS 文件中,尝试将背景的 z-index 设置为 1,下面的 CSS 可能会有所帮助:

    .modal-backdrop{
      z-index: 1;
    }
    

    【讨论】:

      【解决方案2】:

      如果您对 SASS 中的引导程序主题感到满意,以下变量可以解决问题:

      $zindex-modal:              100001;
      $zindex-modal-backdrop:     100000;
      

      我这样做是为了让我的模式/背景出现在我的粘性导航栏上方。

      【讨论】:

        猜你喜欢
        • 2016-02-12
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 2020-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        相关资源
        最近更新 更多