导航容器内的中心导航项

时间:2018-08-31 14:19:29

标签: html css bootstrap-4

我正在努力在项目中将导航项的内容居中放在导航容器中。 我无法在ul> li项目上留出余量,因为它不能在其他分辨率下工作。 我可能需要一定百分比的东西,但是我尝试了却没有任何效果。 预期的结果是这样的 enter image description here

您可以找到一个小样here

非常感谢, 安德里亚。

.header-nav-fixed-down {
  height: 60px;
  padding: 20px;
  background-color: #2c3642;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.3);
}

.divider {
  width: 1px;
  height: 30px;
  background-color: #000000;
}

div#navbarCollapse ul li a{
    color: grey !important;
}
<!DOCTYPE html>
<html lang="en"><head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 4 Navbar with hover multi dropdown</title>

        <!-- Bootstrap -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

        <link href="css/bootstrap-4-hover-navbar.css" rel="stylesheet">


       
    </head>
    <body>

<nav class="navbar navbar-expand-md fixed-down header-nav-fixed-down ">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav m-auto">
            <li class=" nav-item ">
                <a href="#">ItemA</a>
            </li>
            <li role="separator" class="divider m-auto"></li>
        </ul>
        <ul class="navbar-nav m-auto">
            <li class="nav-item">
                 <a href="#">ItemB.1</a>
            </li>
            <li class="nav-item">
                <a href="#">ItemB.2</a>
            </li>
            <li class="nav-item">
                 <a href="#">ItemB.3</a>
            </li>
            <li role="separator" class="divider"></li>
        </ul>
        
        <ul class="navbar-nav m-auto">
            <li class="nav-item">
                 <a href="#">ItemB.1</a>
            </li>
            <li class="nav-item">
                <a href="#">ItemB.2</a>
            </li>
            <li class="nav-item">
                 <a href="#">ItemB.3</a>
            </li>
            <li role="separator" class="divider"></li>
        </ul>
       
        <ul class="navbar-nav m-auto">
            <li class="nav-item">
                 <a href="#">ItemD</a>
            </li>
           
            <li role="separator" class="divider"></li>
        </ul>
         <ul class="navbar-nav m-auto">
            <li class="nav-item">
                 <a href="#">ItemE1</a>
            </li>
            <li class="nav-item">
                 <a href="#">ItemE2</a>
            </li>
            <li role="separator" class="divider"></li>
        </ul>
         <ul class="navbar-nav m-auto">
            <li class="nav-item ">
                 <a href="#">ItemF1</a>
            </li>
            <li class="nav-item ">
                 <a href="#">ItemF2</a>
            </li>
            <li role="separator" class="divider"></li>
        </ul>
    </div>
</nav>








        <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.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

        <script src="js/bootstrap-4-hover-navbar.js"></script>
    

</body></html>

2 个答案:

答案 0 :(得分:0)

<nav class="navbar navbar-expand-md fixed-down header-nav-fixed-down ">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav m-auto">
            <li class="nav-item border-right px-4 ">
                <a class="nav-link" href="#">ItemA</a>
            </li>
            <li class="nav-item px-4 ">
                 <a class="nav-link" href="#">ItemB.1</a>
            </li>
            <li class="nav-item px-4 ">
                <a class="nav-link" href="#">ItemB.2</a>
            </li>
            <li class="nav-item border-right px-4 ">
                 <a class="nav-link" href="#">ItemB.3</a>
            </li>
            <li class="nav-item px-4 ">
                 <a class="nav-link" href="#">ItemC.1</a>
            </li>
            <li class="nav-item px-4 ">
                <a class="nav-link" href="#">ItemC.2</a>
            </li>
            <li class="nav-item border-right px-4 ">
                 <a class="nav-link" href="#">ItemC.3</a>
            </li>
            <li class="nav-item border-right px-4 ">
                 <a class="nav-link" href="#">ItemD</a>
            </li>
            <li class="nav-item border-right px-4 ">
                 <a class="nav-link" href="#">ItemE1</a>
            </li>
            <li class="nav-item border-right px-4 ">
                 <a class="nav-link" href="#">ItemE2</a>
            </li>
            <li class="nav-item px-4 ">
                 <a class="nav-link" href="#">ItemF1</a>
            </li>
            <li class="nav-item px-4 ">
                 <a class="nav-link" href="#">ItemF2</a>
            </li>
        </ul>
    </div>
</nav>

不需要<ul class="navbar-nav">个元素。如果您不希望所有元素上都有边框,那么只需在适用的地方删除边框即可。

您还缺少嵌套锚点的nav-link类。如果该项目不是链接,则可以使用nav-text类。

答案 1 :(得分:0)

您可以将Flexbox用于导航容器。这将使导航容器中的项目“变通”,并使您能够控制其方向(垂直,水平)和常规位置(开始,居中,结束)。最好的部分是它具有响应能力,因此,当屏幕大小更改时,您的孩子(导航栏中的列表项)将弯曲并适合他们周围的可用空间。您可以尝试以下入门。

.navigation {
   display: flex;
   justify-content: space-evenly;
   align-items: center;
 }