创建左侧带标题的导航栏和右侧的按钮

时间:2017-03-12 17:47:18

标签: html css navbar

这就是我的导航栏现在的样子: enter image description here

这就是我想要的样子(左边的标题和右边的按钮): enter image description here

我将导航栏分隔为左对齐列表以及按钮的右对齐列表,但出于某种原因,这两个列表堆叠在一起而不是在一条线上。如何在上面创建导航栏?

HTML

<div class = "navBarWrapper">
   <div class="top bar">
        <ul class = "left">
            <li class = "title">Photo Albums</li>
        </ul>
        <ul class = "right">
            <li class = "buttons"> <a class = "iconLink" href = ""><img class = "iconNotHover" src = "images/uploadImage.png" alt = "uploadImage" width = "40" align = "middle"/></a></li>
            <li class = "buttons"> <a href = "login.html" class="otherPages">Login</a></li>
        </ul>
    </div> 
</div> 

CSS

.navBarWrapper {
  position: fixed;
    z-index: 1;
    width: 100%;

}

.left {
    text-align: left;
}

.right {
    text-align: right;
}

.bar {
  height: 8.02%;
}


.bar li {
    display: inline-block;
}

1 个答案:

答案 0 :(得分:1)

您不想使用text-align向左或向右移动块元素。这只会将块元素中的内联元素与左侧或右侧对齐,并且块元素仍将位于其自己的行上。

你可以将左/右元素向左/向右浮动,但是我会在父对象上使用flexbox,justify-content: space-between将左/右分开,并align-items将它们垂直对齐,但是你想要

&#13;
&#13;
.navBarWrapper {
  position: fixed;
  z-index: 1;
  width: 100%;
}

.bar {
  height: 8.02%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.bar li {
  display: inline-block;
}
&#13;
<div class="navBarWrapper">
  <div class="top bar">
    <ul class="left">
      <li class="title">Photo Albums</li>
    </ul>
    <ul class="right">
      <li class="buttons">
        <a class="iconLink" href=""><img class="iconNotHover" src="images/uploadImage.png" alt="uploadImage" width="40" align="middle" /></a>
      </li>
      <li class="buttons"> <a href="login.html" class="otherPages">Login</a></li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

或者,如果您想要浮动它们,请在overflow: auto上使用.bar进行布局以清除浮动。

&#13;
&#13;
.navBarWrapper {
  position: fixed;
  z-index: 1;
  width: 100%;
}

.left {
  float: left;
}

.right {
  float: right;
}

.bar {
  height: 8.02%;
  overflow: auto;
}

.bar li {
  display: inline-block;
}
&#13;
<div class="navBarWrapper">
  <div class="top bar">
    <ul class="left">
      <li class="title">Photo Albums</li>
    </ul>
    <ul class="right">
      <li class="buttons">
        <a class="iconLink" href=""><img class="iconNotHover" src="images/uploadImage.png" alt="uploadImage" width="40" align="middle" /></a>
      </li>
      <li class="buttons"> <a href="login.html" class="otherPages">Login</a></li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;