我将导航栏分隔为左对齐列表以及按钮的右对齐列表,但出于某种原因,这两个列表堆叠在一起而不是在一条线上。如何在上面创建导航栏?
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;
}
答案 0 :(得分:1)
您不想使用text-align
向左或向右移动块元素。这只会将块元素中的内联元素与左侧或右侧对齐,并且块元素仍将位于其自己的行上。
你可以将左/右元素向左/向右浮动,但是我会在父对象上使用flexbox,justify-content: space-between
将左/右分开,并align-items
将它们垂直对齐,但是你想要
.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;
或者,如果您想要浮动它们,请在overflow: auto
上使用.bar
进行布局以清除浮动。
.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;