对齐导航栏中的元素

时间:2017-04-13 03:14:08

标签: css nav

我有一个带3个物品的导航栏。我有一个项目,我想沿着酒吧的最左侧对齐。当我在导航栏中将第四个项目添加为div时,3 li项目向右倾斜。我使用bootstrap来设置导航栏的样式。这是标记:

<nav class="navbar navbar-toggleable-md navbar-inverse">
            <ul class="navbar-nav">
                <li style="float: left; color: white">
                    My Text Here
                </li>
                <li class="nav-item">
                    <a class="nav-link" routerLink="/item1" routerLinkActive="active">Item1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" routerLink="/item2" routerLinkActive="active">Item2</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" routerLink="/Item3" routerLinkActive="active">Item3</a>
                </li>
            </ul>  
        </nav>

我已尝试将第四项应用于sone风格,但它们没有任何影响。有什么指针吗?

2 个答案:

答案 0 :(得分:0)

你可以从ul中取出你的第一个项目并将其放在一个带有navbar-brand类的元素中,该类会自动拉到最左边并对文本进行样式设置以使其显而易见。您还可以使用班级navbar-leftnavbar-right向左或向右拉取元素。

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-toggleable-md navbar-inverse">
<div class="container">
  <div class="navbar-header navbar-left">
    <p class="navbar-brand">My Text Here</p>
  </div>
  <ul class="nav navbar-nav">
    <li class="nav-item">
      <a class="nav-link" routerLink="/item1" routerLinkActive="active">Item1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" routerLink="/item2" routerLinkActive="active">Item2</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" routerLink="/Item3" routerLinkActive="active">Item3</a>
    </li>
  </ul>
  </div>
</nav>

答案 1 :(得分:0)

我建议您使用bootstrap提供的标准。总是可以自定义添加一些类。如您所见,我已将品牌添加到您的导航中,我认为这正是您所寻找的。

检查第二个导航,你可以看到更多有用的类。 (在整页上查看它的片段。)

检查第三个导航是如何将徽标添加为img。

&#13;
&#13;
.this-is-my-brand {
height:100%;
margin:10px;
font-family:Verdana;
text-shadow:1px 1px 1px #fa8911;
}

.logo{
width:25%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"
      rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>      
<nav class="navbar navbar-toggleable-md navbar-inverse">

 
           
           <ul class="navbar-nav">
           <a class="navbar-brand this-is-my-brand" href="#">Brand</a>
                <li style="float: left; color: white">
                    My Text Here
                </li>
                <li class="nav-item">
                    <a class="nav-link" routerLink="/item1" routerLinkActive="active">Item1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" routerLink="/item2" routerLinkActive="active">Item2</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" routerLink="/Item3" routerLinkActive="active">Item3</a>
                </li>
                 
                </li>
            </ul>  
        </nav>
        
        <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="#">Brand</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="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>


<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">
        <img class="logo" alt="Brand" src="https://i.stack.imgur.com/MybMA.png">
      </a>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;