如何在导航栏中向右浮动按钮,以免影响主导航栏?

时间:2019-05-16 03:46:32

标签: css

如何将“登录”按钮放在右侧,主导航栏是否可以居中?

ul {
  margin-top: 18px;
  margin-bottom: 18px;
  padding: 0px;
  list-style-type: none;
  display: inline-block;
  align-items: center;
  align-self: center;
  align-content: center;
}

ul.login {
  float: right;
}

li {
  margin-right: 30px;
  float: left;
}

li.last {
  margin-right: 0px;
}

li a {
  text-decoration: none;
  text-align: center;
  display: block;
  font-family: Poppins;
  font-size: 1em;
  font-weight: normal;
}
<div class="main-nav">
  <ul>
    <li><a href="index.php" id="Home">Home</a></li>
    <li><a href="shop.php" id="Shop">Shop</a></li>
    <li><a href="about.php" id="About">About</a></li>
    <li><a href="account.php" id="Account">My Account</a></li>
    <li class="last"><a id="Cart" href="cart.php">Cart</a></li>
  </ul>
  <ul class="login">
    <li><a href="login.php">Login</a></li>
  </ul>
</div>

当前,“登录”按钮向右浮动,但主导航栏已向左稍移。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

.main-nav {
    display: flex;
}
ul:not(.login) {
    display: table;
    margin: 0 auto;
}
ul {
    margin-top: 18px;
    margin-bottom: 18px;
    padding: 0px;
    list-style-type: none;
    display: inline-block;
    align-items: center;
    align-self: center;
    align-content: center;
}
li {
    margin: 0 15px;
    display: table-cell;
}
ul li a {
    text-decoration: none;
    text-align: center;
    display: block;
    font-family: Poppins;
    font-size: 1em;
    font-weight: normal;
}
ul.login li {
    margin-right: 0;
}
ul:not(.login) li a {
    margin: 0 5px;
}

注意:它的工作原理完美。