Bootstrap 为什么区域与导航栏重叠?

bnl4lu3b  于 2023-02-27  发布在  Bootstrap
关注(0)|答案(2)|浏览(141)

在某些屏幕尺寸上,区域内容与导航栏重叠。我想知道为什么会这样,以及如何防止。是否需要将所有内容 Package 在容器中,或者该问题的解决方案是什么?
使用 Bootstrap v5.3.0-alpha 1
另外,如果我有一个折叠的导航栏,按钮不能被按下bc的部分是重叠它,所以固定这将是有帮助的

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>test</title>

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
        <link rel="stylesheet" href="{{ url_for('static', filename='css/dashboard.css') }}" />
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
    </head>
    <body>
        <nav class="navbar bg-body-tertiary">
            <div class="container-fluid">
                <a class="navbar-brand" href="#">
                    <img src={{url_for('static', filename='/images/logo.png')}} id="logo" alt="Logo" width="50" height="50" class="d-inline-block align-text-top" style="border-radius: 5px;">
                </a>

                <div class="btn-toolbar align-items-center justify-content-center" role="toolbar" aria-label="Toolbar with button groups">
                    <div class="btn-group me-2" role="group" aria-label="sign in">
                        <div class="dropdown">
                            <!-- dropdown-toggle -->
                            <button class="btn btn-primary" id="connect_wallet_button" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                Sign In
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end d-none" id="dropdown_menu">
                                <li><a class="dropdown-item" href="#" id="logout_button">Logout</a></li>
                            </ul>
                        </div>
                    </div>

                    <div class="btn-group me-2" role="group" aria-label="theme button">
                        <a href="/#">
                            <i class="bi bi-sun" id="theme_icon" style="font-size: 1.3rem;"></i>
                        </a>
                    </div>
                </div>
            </div>
        </nav>

        <div class="position-absolute top-50 start-50 translate-middle p-3">
            <div class="card">
                <div class="card-body">
                    <h3 class="card-title text-center">test form</h3>

                    <form class="needs-validation" id="form" action="/form" method="post">
                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>
                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="test" value="False" name="test" required>
                                <label class="form-check-label" for="test">
                                    ???
                                </label>
                            </div>
                        </div>
        
                        <div class="d-none" id="form_button_group">
                            <button type="button" class="btn btn-secondary" id="clear_button">Clear</button>
                            <button type="submit" class="btn btn-primary" id="submit_button">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>


        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
        <script type="text/javascript" src="{{ url_for('static', filename='js/dashboard.js') }}"></script>
        <script type="application/javascript" src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"></script>
    </body>
</html>
qncylg1j

qncylg1j1#

这个设置有很多问题,我不确定你想解决哪一个。如果你想知道的只是如何调整顶部的内容,那就是z-index设置。你可以用z-index来设置元素的样式,以控制顶部显示的内容。我在你的代码中添加了一个前台类。

<!doctype html>
<style>
    .foreground {
        z-index: 50;
    }
</style>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>test</title>

        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
        <link rel="stylesheet" href="{{ url_for('static', filename='css/dashboard.css') }}" />
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css">
    </head>
    <body>
        <nav class="navbar bg-body-tertiary foreground">
            <div class="container-fluid">
                <a class="navbar-brand" href="#">
                    <img src={{url_for('static', filename='/images/logo.png')}} id="logo" alt="Logo" width="50" height="50" class="d-inline-block align-text-top" style="border-radius: 5px;">
                </a>

                <div class="btn-toolbar align-items-center justify-content-center" role="toolbar" aria-label="Toolbar with button groups">
                    <div class="btn-group me-2" role="group" aria-label="sign in">
                        <div class="dropdown">
                            <!-- dropdown-toggle -->
                            <button class="btn btn-primary" id="connect_wallet_button" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                Sign In
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end d-none" id="dropdown_menu">
                                <li><a class="dropdown-item" href="#" id="logout_button">Logout</a></li>
                            </ul>
                        </div>
                    </div>

                    <div class="btn-group me-2" role="group" aria-label="theme button">
                        <a href="/#">
                            <i class="bi bi-sun" id="theme_icon" style="font-size: 1.3rem;"></i>
                        </a>
                    </div>
                </div>
            </div>
        </nav>

        <div class="position-absolute top-50 start-50 translate-middle p-3">
            <div class="card">
                <div class="card-body">
                    <h3 class="card-title text-center">test form</h3>

                    <form class="needs-validation" id="form" action="/form" method="post">
                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>
                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <label for="name" class="form-label">test</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>

                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="test" value="False" name="test" required>
                                <label class="form-check-label" for="test">
                                    ???
                                </label>
                            </div>
                        </div>
        
                        <div class="d-none" id="form_button_group">
                            <button type="button" class="btn btn-secondary" id="clear_button">Clear</button>
                            <button type="submit" class="btn btn-primary" id="submit_button">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>


        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
        <script type="text/javascript" src="{{ url_for('static', filename='js/dashboard.js') }}"></script>
        <script type="application/javascript" src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"></script>
    </body>
</html>
uz75evzq

uz75evzq2#

这里的主要问题是,您的测试表单“div”使用absolute定位,导致它从正常文档流中删除。因此,当屏幕高度更小时或更大时,顶部位置将根据它的top: 50% !important规则重新计算,导致与导航栏重叠或在两个元素之间留下大量白色。
为了解决这个问题,我建议删除位置规则,并为测试表单“div”指定一个宽度(我在这里使用.container,您可以将其更改为适合您的设计的任何宽度)。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>test</title>

    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
      crossorigin="anonymous"
    />
    <link
      rel="stylesheet"
      href="{{ url_for('static', filename='css/dashboard.css') }}"
    />
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css"
    />
  </head>
  <body>
    <nav class="navbar mb-4 bg-body-tertiary">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">
          <img src={{url_for('static', filename='/images/logo.png')}} id="logo"
          alt="Logo" width="50" height="50" class="d-inline-block
          align-text-top" style="border-radius: 5px;">
        </a>

        <div
          class="btn-toolbar align-items-center justify-content-center"
          role="toolbar"
          aria-label="Toolbar with button groups"
        >
          <div class="btn-group me-2" role="group" aria-label="sign in">
            <div class="dropdown">
              <!-- dropdown-toggle -->
              <button
                class="btn btn-primary"
                id="connect_wallet_button"
                type="button"
                data-bs-toggle="dropdown"
                aria-expanded="false"
              >
                Sign In
              </button>
              <ul
                class="dropdown-menu dropdown-menu-end d-none"
                id="dropdown_menu"
              >
                <li>
                  <a class="dropdown-item" href="#" id="logout_button"
                    >Logout</a
                  >
                </li>
              </ul>
            </div>
          </div>

          <div class="btn-group me-2" role="group" aria-label="theme button">
            <a href="/#">
              <i
                class="bi bi-sun"
                id="theme_icon"
                style="font-size: 1.3rem"
              ></i>
            </a>
          </div>
        </div>
      </div>
    </nav>

    <div class="container">
      <div class="card">
        <div class="card-body">
          <h3 class="card-title text-center">test form</h3>

          <form class="needs-validation" id="form" action="/form" method="post">
            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>

            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>
            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>

            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>

            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>

            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>

            <div class="mb-3">
              <label for="name" class="form-label">test</label>
              <input
                type="text"
                class="form-control"
                id="name"
                name="name"
                required
              />
            </div>

            <div class="mb-3">
              <div class="form-check">
                <input
                  class="form-check-input"
                  type="checkbox"
                  id="test"
                  value="False"
                  name="test"
                  required
                />
                <label class="form-check-label" for="test"> ??? </label>
              </div>
            </div>

            <div class="d-none" id="form_button_group">
              <button type="button" class="btn btn-secondary" id="clear_button">
                Clear
              </button>
              <button type="submit" class="btn btn-primary" id="submit_button">
                Submit
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
      crossorigin="anonymous"
    ></script>
    <script
      type="text/javascript"
      src="{{ url_for('static', filename='js/dashboard.js') }}"
    ></script>
    <script
      type="application/javascript"
      src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
    ></script>
  </body>
</html>

相关问题