如何将Navbar-Brand中间的链接项居中

时间:2018-09-28 03:31:04

标签: html css twitter-bootstrap

我的导航菜单出现问题。

我想要的是我的品牌,位于我的导航链接的中间。

示例 Home | product | Company Logo(this is my brand) | about us | contact us

当我在小型设备中打开时,我希望公司徽标停留在导航栏的当前位置或中间,并在单击切换图标时显示其他链接项(home|product|about us|contact us)。

下面是我的源代码:

                                                      

        <div class="collapse navbar-collapse" id="navigation">
            <ul class="navbar-nav">
                  <li class="nav-item active">
                    <a class="nav-link" href="#">About Us <span class="sr-only">(current)</span></a>
                  </li>
                      <li class="nav-item">
                        <a class="nav-link" href="#">Products</a>
                      </li>
                          <li class="nav-item dropdown">
                          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown
                          </a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                      <a class="dropdown-item" href="#">Action</a>
                                      <a class="dropdown-item" href="#">Another action</a>
                                      <div class="dropdown-divider"></div>
                                      <a class="dropdown-item" href="#">Something else here</a>
                                </div>
                          </li> 


        <a class="narbar-brand">Company Name</a><!-- I want this company name stay at top when I open my website via small device -->

                  <li class="nav-item">
                    <a class="nav-link" href="#">Suppport and Downloads</a>
                  </li>
                      <li class="nav-item">
                        <a class="nav-link" href="#">Partners</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" href="#">Contact Us</a>
                      </li>
            </ul>
            <!--<form class="form-inline my-2 my-lg-0">
              <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
              <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>-->
        </div>
    </nav>

以下是大/小屏幕设备中的示例导航。我希望公司名称出现在小型设备上

当前问题: big small

2 个答案:

答案 0 :(得分:0)

使用自定义CSS,可以实现

.navbar-header .navbar-brand{
  display:none;
}
.nav-item .navbar-brand{
    font-size: 18px;
    font-weight:bold;
}
@media only screen and (max-width: 767px) {
  .navbar-header .navbar-brand{
    display:block;
  }
  .nav-item .navbar-brand{
    display:none;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Company Name</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="nav-item active">
                    <a class="nav-link" href="#">About Us <span class="sr-only">(current)</span></a>
                  </li>
                      <li class="nav-item">
                        <a class="nav-link" href="#">Products</a>
                      </li>
                          <li class="nav-item dropdown">
                          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown
                          </a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                      <a class="dropdown-item" href="#">Action</a>
                                      <a class="dropdown-item" href="#">Another action</a>
                                      <div class="dropdown-divider"></div>
                                      <a class="dropdown-item" href="#">Something else here</a>
                                </div>
                          </li> 


        <!-- I want this company name stay at top when I open my website via small device -->

                  <li class="nav-item">
                    <a class="navbar-brand">Company Name</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="#">Suppport and Downloads</a>
                  </li>
                      <li class="nav-item">
                        <a class="nav-link" href="#">Partners</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" href="#">Contact Us</a>
                      </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

答案 1 :(得分:0)

您可以为此使用媒体查询并创建两个导航品牌元素

a.navbar-brand.center {
  display: none;
}


@media (max-width: 990px) {
  
  a.navbar-brand {
        display:none;
  }
  
  a.navbar-brand.center {
    position: absolute;
    display: block;
    transform: translate(-50%);
    left: 50%;
    top: 10px;
  }
    
  li.nav-item {
    text-align: center;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand center" href="#">Your brand</a>
  <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
    
    <ul class="navbar-nav mr-auto ml-auto mt-2 mt-lg-0">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <a class="navbar-brand" href="#">Your brand</a>
      
      <li class="nav-item">
        <a class="nav-link" href="#">About</a>
      </li>
    </ul>
  </div>
</nav>