【问题标题】:Bootstrap 4 Center Nav DropdownBootstrap 4 中心导航下拉菜单
【发布时间】:2020-04-22 14:38:31
【问题描述】:

我正在使用 bootstrap 4 创建导航菜单。我的导航项目每个都包含一个下拉菜单。单击时,我希望此菜单显示相同的大小并在页面上水平居中。理想情况下,我希望这个下拉菜单与 ul 元素的大小相同。目前我不知道如何做到这一点,因为我对 bootstrap 比较陌生。

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

<nav class="navbar navbar-expand-md navbar-light bg-primary py-0">
  <div class="container h-100">
    <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 h-100" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto mx-auto h-100">
        <li class="nav-item dropdown px-2">
          <a class="nav-link dropdown-toggle text-white" href="/category/flat-roof-materials" id="flatRoofingDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Flat Roofing
                    </a>
          <div class="dropdown-menu border border-primary row mx-auto" aria-labelledby="flatRoofingDropdown">
            <div class="container">
              <div class="row">
                <div class="col-md-4">
                  <a class="dropdown-item text-primary" href="/category/epdm-rubber-roofing">EPDM Rubber Roofing</a>
                  <a class="dropdown-item" href="/category/classicbond">ClassicBond</a>
                  <a class="dropdown-item" href="/category/firestone-rubbercover">Firestone</a>
                  <a class="dropdown-item" href="/category/duoply">Duoply</a>
                  <a class="dropdown-item text-primary" href="/category/fibreglass-grp-roofing-systems">Fibreglass GRP</a>
                  <a class="dropdown-item" href="/category/fibreglass-grp-roofing-systems?filter232=Cromar&page=1">ProGRP</a>
                  <a class="dropdown-item" href="/category/flexitec-2020">Flexitec 2020</a>
                  <a class="dropdown-item" href="/tools/flexitec-calculator">Flexitec 2020 Roof Calculator</a>
                  <a class="dropdown-item" href="/tools/grp1010-calculator">GRP Roof 1010</a>
                  <a class="dropdown-item" href="/category/ultraflex">Ultraflex</a>
                  <a class="dropdown-item text-primary" href="/category/torch-on-felt">Roofing Felt</a>
                  <a class="dropdown-item" href="/category/torch-on-felt">Torch on felts</a>
                  <a class="dropdown-item text-primary" href="/category/timber-sheeting">Timber Sheeting</a>
                  <a class="dropdown-item" href="/category/osb">OSB</a>
                </div>
                <div class="col-md-4">
                  <a class="dropdown-item text-primary" href="/category/green-roofing">Green Roof System</a>
                  <a class="dropdown-item" href="/category/green-roofing?filter232=GrufeKit&page=1">Grufe kit</a>
                  <a class="dropdown-item text-primary" href="/category/liquid-waterproofing-systems">Liquid Waterproofing</a>
                  <a class="dropdown-item" href="/category/elastathane">Elastathane 25</a>
                  <a class="dropdown-item" href="/category/hydrosil-silicone-roof-coating">Hydrosil</a>
                  <a class="dropdown-item" href="/tools/flexitec-calculator">Liquid Rubber</a>
                  <a class="dropdown-item text-primary" href="#">Skylights</a>
                  <a class="dropdown-item" href="#">Flat Glass Rooflights</a>
                  <a class="dropdown-item" href="#">Domes</a>
                  <a class="dropdown-item text-primary" href="#">Flat Roof Outlets</a>
                  <a class="dropdown-item" href="/category/flat-roof-outlets">Water Outlets</a>
                  <a class="dropdown-item" href="/category/flat-roof-vents">Flat Roof Vents</a>
                </div>
                <div class="col-md-4">
                  <a class="dropdown-item navbar-brand text-primary h5" href="/category/epdm-rubber-roofing">EPDM Rubber Roofing</a>
                  <a class="dropdown-item" href="/category/classicbond">ClassicBond</a>
                  <a class="dropdown-item" href="/category/firestone-rubbercover">Firestone</a>
                  <a class="dropdown-item" href="/category/duoply">Duoply</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item navbar-brand text-primary" href="/category/fibreglass-grp-roofing-systems">Fibreglass GRP</a>
                  <a class="dropdown-item" href="/category/fibreglass-grp-roofing-systems?filter232=Cromar&page=1">ProGRP</a>
                  <a class="dropdown-item" href="/category/flexitec-2020">Flexitec 2020</a>
                  <a class="dropdown-item" href="/tools/flexitec-calculator">Flexitec 2020 Roof Calculator</a>
                  <a class="dropdown-item" href="/tools/grp1010-calculator">GRP Roof 1010</a>
                  <a class="dropdown-item" href="/category/ultraflex">Ultraflex</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item navbar-brand text-primary" href="/category/torch-on-felt">Roofing Felt</a>
                  <a class="dropdown-item" href="/category/torch-on-felt">Torch on felts</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item navbar-brand text-primary" href="/category/timber-sheeting">Timber Sheeting</a>
                  <a class="dropdown-item" href="/category/osb">OSB</a>
                </div>

              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</nav>


<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

当前行为:

预期结果,居中,大小相同:

建议修复的结果:

【问题讨论】:

    标签: javascript html css twitter-bootstrap bootstrap-4


    【解决方案1】:

    在 CSS 下方添加。

    .dropdown {
        position: initial;
    }
    
    @media (min-width: 768px) {
      .dropdown-menu {
          width: 50rem; /* JUST FOR STYLING PURPOSE OF DROPDOWN */
          position: absolute;
          left: 50% !important;
          transform: translateX(-50%) !important;
      }
    }
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
    
    <nav class="navbar navbar-expand-md navbar-light bg-primary py-0">
      <div class="container h-100">
        <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 h-100" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto mx-auto h-100">
            <li class="nav-item dropdown px-2">
              <a class="nav-link dropdown-toggle text-white" href="/category/flat-roof-materials" id="flatRoofingDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Flat Roofing
                        </a>
              <div class="dropdown-menu border border-primary row mx-auto" aria-labelledby="flatRoofingDropdown">
                <div class="container">
                  <div class="row">
                    <div class="col-md-4">
                      <a class="dropdown-item text-primary" href="/category/epdm-rubber-roofing">EPDM Rubber Roofing</a>
                      <a class="dropdown-item" href="/category/classicbond">ClassicBond</a>
                      <a class="dropdown-item" href="/category/firestone-rubbercover">Firestone</a>
                      <a class="dropdown-item" href="/category/duoply">Duoply</a>
                      <a class="dropdown-item text-primary" href="/category/fibreglass-grp-roofing-systems">Fibreglass GRP</a>
                      <a class="dropdown-item" href="/category/fibreglass-grp-roofing-systems?filter232=Cromar&page=1">ProGRP</a>
                      <a class="dropdown-item" href="/category/flexitec-2020">Flexitec 2020</a>
                      <a class="dropdown-item" href="/tools/flexitec-calculator">Flexitec 2020 Roof Calculator</a>
                      <a class="dropdown-item" href="/tools/grp1010-calculator">GRP Roof 1010</a>
                      <a class="dropdown-item" href="/category/ultraflex">Ultraflex</a>
                      <a class="dropdown-item text-primary" href="/category/torch-on-felt">Roofing Felt</a>
                      <a class="dropdown-item" href="/category/torch-on-felt">Torch on felts</a>
                      <a class="dropdown-item text-primary" href="/category/timber-sheeting">Timber Sheeting</a>
                      <a class="dropdown-item" href="/category/osb">OSB</a>
                    </div>
                    <div class="col-md-4">
                      <a class="dropdown-item text-primary" href="/category/green-roofing">Green Roof System</a>
                      <a class="dropdown-item" href="/category/green-roofing?filter232=GrufeKit&page=1">Grufe kit</a>
                      <a class="dropdown-item text-primary" href="/category/liquid-waterproofing-systems">Liquid Waterproofing</a>
                      <a class="dropdown-item" href="/category/elastathane">Elastathane 25</a>
                      <a class="dropdown-item" href="/category/hydrosil-silicone-roof-coating">Hydrosil</a>
                      <a class="dropdown-item" href="/tools/flexitec-calculator">Liquid Rubber</a>
                      <a class="dropdown-item text-primary" href="#">Skylights</a>
                      <a class="dropdown-item" href="#">Flat Glass Rooflights</a>
                      <a class="dropdown-item" href="#">Domes</a>
                      <a class="dropdown-item text-primary" href="#">Flat Roof Outlets</a>
                      <a class="dropdown-item" href="/category/flat-roof-outlets">Water Outlets</a>
                      <a class="dropdown-item" href="/category/flat-roof-vents">Flat Roof Vents</a>
                    </div>
                    <div class="col-md-4">
                      <a class="dropdown-item navbar-brand text-primary h5" href="/category/epdm-rubber-roofing">EPDM Rubber Roofing</a>
                      <a class="dropdown-item" href="/category/classicbond">ClassicBond</a>
                      <a class="dropdown-item" href="/category/firestone-rubbercover">Firestone</a>
                      <a class="dropdown-item" href="/category/duoply">Duoply</a>
                      <div class="dropdown-divider"></div>
                      <a class="dropdown-item navbar-brand text-primary" href="/category/fibreglass-grp-roofing-systems">Fibreglass GRP</a>
                      <a class="dropdown-item" href="/category/fibreglass-grp-roofing-systems?filter232=Cromar&page=1">ProGRP</a>
                      <a class="dropdown-item" href="/category/flexitec-2020">Flexitec 2020</a>
                      <a class="dropdown-item" href="/tools/flexitec-calculator">Flexitec 2020 Roof Calculator</a>
                      <a class="dropdown-item" href="/tools/grp1010-calculator">GRP Roof 1010</a>
                      <a class="dropdown-item" href="/category/ultraflex">Ultraflex</a>
                      <div class="dropdown-divider"></div>
                      <a class="dropdown-item navbar-brand text-primary" href="/category/torch-on-felt">Roofing Felt</a>
                      <a class="dropdown-item" href="/category/torch-on-felt">Torch on felts</a>
                      <div class="dropdown-divider"></div>
                      <a class="dropdown-item navbar-brand text-primary" href="/category/timber-sheeting">Timber Sheeting</a>
                      <a class="dropdown-item" href="/category/osb">OSB</a>
                    </div>
    
                  </div>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    
    
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

    【讨论】:

    • 它仍然没有居中。我已经从您的代码中发布了上面的结果。
    • 你可以在 sn-p 中看到,它正好是中心,我认为你的其他 CSS 会产生问题
    • 看起来可能性很大。感谢您的宝贵时间。
    • 很高兴你喜欢我的回答。
    • 在 sm 及其下方居中,但在 md 及其上方不居中
    猜你喜欢
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多