CSS Navbar项不会右对齐

时间:2018-06-22 14:50:45

标签: html css

这是我要登录和注册的html代码,与导航栏的右侧对齐

<navbar class="navbar">
    <div class="content">
<a class="navbarbrand" href="#">Brand</a>
<div class="navbaritemcon">
    <a href="#" class="navbaritem">Home</a>
    <a href="#" class="navbaritem">About Us</a>
</div>

<div class="navbaritemcon right">
    <a href="#" class="navbaritem">Login</a>
    <a href="#" class="navbaritem">Sign Up</a>
</div>

</div>
</navbar>

</body>

CSS:-

尝试使用float选项,但也不起作用

.navbar{
    background-color: #17a2b8;;
    width: 100%;
    height: 40px;
    display: flex;
    padding-left: 1rem;
    padding-right: 1rem;
    position: sticky;
    align-items: center;
}

.navbar>.content{
    display: flex;
    align-items: center;
}
.content{
    position: relative;
    padding-top: 8px;
    padding-bottom: 8px;
    max-width: 100%;
    min-width: 80%;
    margin-right: auto;
    margin-left: auto;


}

.navbarbrand{
    display: inline-block;
    color: white;
    font-weight: 700;
    font-size: 1.5rem;
    text-decoration: none;
    margin-right: 1rem; 
    position: relative;
}

.navbaritemcon{
    list-style: none;
    float: left;  

}

.navbaritem{
    font-size: 1rem;
    color: white;
    display: inline-block;
    font-weight: normal;
    text-decoration: none;
    margin-right: 1rem;
    margin-left: 1rem;
}

.right{
    justify-content: flex-end;
}

如何使登录名和注册显示在导航栏的右侧。请帮助无法找出正确的方法


3 个答案:

答案 0 :(得分:2)

只需使用margin-left:auto;,您就可以使用

.navbar {
  background-color: #17a2b8;
  width: 100%;
  height: 40px;
  display: flex;
  padding-left: 1rem;
  padding-right: 1rem;
  position: sticky;
  align-items: center;
}

.navbar>.content {
  display: flex;
  align-items: center;
}

.content {
  position: relative;
  padding-top: 8px;
  padding-bottom: 8px;
  max-width: 100%;
  min-width: 80%;
  margin-right: auto;
  margin-left: auto;
}

.navbarbrand {
  display: inline-block;
  color: white;
  font-weight: 700;
  font-size: 1.5rem;
  text-decoration: none;
  margin-right: 1rem;
  position: relative;
}

.navbaritemcon {
  list-style: none;
  float: left;
}

.navbaritem {
  font-size: 1rem;
  color: white;
  display: inline-block;
  font-weight: normal;
  text-decoration: none;
  margin-right: 1rem;
  margin-left: 1rem;
}

.right {
  margin-left:auto;
}
<navbar class="navbar">
  <div class="content">
    <a class="navbarbrand" href="#">Brand</a>
    <div class="navbaritemcon">
      <a href="#" class="navbaritem">Home</a>
      <a href="#" class="navbaritem">About Us</a>
    </div>

    <div class="navbaritemcon right">
      <a href="#" class="navbaritem">Login</a>
      <a href="#" class="navbaritem">Sign Up</a>
    </div>

  </div>
</navbar>

答案 1 :(得分:1)

.navbar>.content{
    display: flex;
    align-items: center;
  justify-content:space-between;
}

<navbar class="navbar">
  <div class="content">
<div class="navbaritemcon">
  <a class="navbarbrand" href="#">Brand</a>
  <a href="#" class="navbaritem">Home</a>
  <a href="#" class="navbaritem">About Us</a>
</div>

<div class="navbaritemcon right">
  <a href="#" class="navbaritem">Login</a>
  <a href="#" class="navbaritem">Sign Up</a>
</div>

</div>
</navbar>

我更改了html,以便更轻松地使用CSS进行操作,但是您可以进行不同的操作,重要的部分是您在父div上有父div和2个子div,并在两端之间找到了合理的内容空间

答案 2 :(得分:0)

我对html和css做了一些更改

  <div class="navbaritemcon">
    <a class="navbarbrand" href="#">Brand</a>
    <a href="#" class="navbarit">Home</a>
    <a href="#" class="navbarit">About Us</a>
  </div>

  <div class="navbaritemconright">
    <a href="#" class="navbaritem">Login</a>
    <a href="#" class="navbaritem">Sign Up</a>
  </div>

  </div>
  </navbar>

Css在

以下
.navbar{
    background-color: #17a2b8;;
    width: 100%;
    height: 40px;
    display: flex;
    padding-left: 1rem;
    padding-right: 1rem;
    position: sticky;
    align-items: center;
    justify-content: space-between

}
.navbar>.content{
    display: flex;
    align-items: center;
}
.content{
    position: relative;
    padding-top: 8px;
    padding-bottom: 8px;
    max-width: 100%;
    min-width: 80%;
    margin-right: auto;
    margin-left: auto;


}

.navbarbrand{
    display: inline-block;
    color: white;
    font-weight: 700;
    font-size: 1.5rem;
    text-decoration: none;
    margin-right: 1rem; 
    position: relative;
}

.navbaritemcon{
    list-style: none;
    float: left;  

}

.navbaritem{
    font-size: 1rem;
    color: white;
    display: inline-block;
    font-weight: normal;
    text-decoration: none;
    margin-right: 1rem;
    margin-left: 1rem;
}

.right{
    justify-content: flex-end;
}

在选择课程时,您还需要小心。 希望这行得通。