如何在WordPress中居中Bootstrap菜单项?

时间:2018-02-11 11:17:43

标签: wordpress twitter-bootstrap bootstrap-4

我正在尝试将一个项目居中,这是Bootstrap导航菜单的一部分。

  <nav class="navbar navbar-toggleable-md navbar-light bg-faded navbar-expand-md" style="background-color: yellow; font-size: 30px";>
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">LOGO</a>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
       <?php
          wp_nav_menu( array(
                    'theme_location'    => 'primary',
                    'depth'             => 2,
                    'container_id'      => 'bs-example-navbar-collapse-1',
                    'menu_class'        => 'nav navbar-nav navbar-right',
                    'fallback_cb'       => 'WP_Bootstrap_Navwalker::fallback',
                    'walker'            => new WP_Bootstrap_Navwalker())
                );
          ?>
         </div>
     </nav>

现在,我的WordPress菜单中有5个项目。我试图这样做,导航栏品牌在左侧,中间的第一个菜单,右边的三个菜单项,最后的菜单项。

|Logo| |first| |second||third||fourth| |fifth|

默认情况下,徽标navbar-brand位于左侧,我设法通过

将第五个菜单项放在右侧
  .menu-item:nth-child(5) {
        position: absolute;
        right: 0px;
    }

现在我需要将第一个项目居中,然后将其余项目推向右侧。我试着做了

  .menu-item:nth-child(1) a {
        margin: 0 auto;
    }

但它不起作用。

1 个答案:

答案 0 :(得分:1)

要在Bootstrap 4中完成它,您需要将3个组中的每个组包装在它们自己的<ul>中,并将ml-auto类添加到该组中。

这是一个有效的HTML代码段(点击下面的“运行代码段”并展开到整页):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-md navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</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 ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="#">Link1</a>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="#">Link2</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link3</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link4</a>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="#">Link5</a>
            </li>
        </ul>
    </div>
</nav>

另请注意:Bootstrap 4中不存在navbar-toggleable-md类。