导航栏中的按钮无法对齐

时间:2020-07-15 14:31:47

标签: html css

我尝试创建一个带有按钮的简单导航栏,但似乎无法完美对齐按钮。 代码看起来像这样

* {
  margin: 0;
  padding: 0;
}

.nav {
  width: 100%;
  height: 60px;
  background: #393e46;
}

.nav .logo {
  float: left;
  width: 100px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: orange;
  font-size: xx-large;
}

.nav .butn {
  float: right;
  width: 150px;
  height: 40px;
  background: beige;
  line-height: 60px;
}

a {
  color: orange;
  font-size: x-large;
}
<body>
  <div class="nav">
    <div class="logo">
      <ul>
        <li>Hello</li>
      </ul>
    </div>

    <div class="butn">
      <a href="#">click me</a>
    </div>
  </div>
</body>

我还包括了Codepen链接enter link description here

2 个答案:

答案 0 :(得分:0)

只需将margin: 10px 0添加到.nav .butn,以便将剩余空间用作边距

* {
  margin: 0;
  padding: 0;
}

.nav {
  width: 100%;
  height: 60px;
  background: #393e46;
}

.nav .logo {
  float: left;
  width: 100px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: orange;
  font-size: xx-large;
}

.nav .butn {
  float: right;
  width: 150px;
  height: 40px;
  background: beige;
  line-height: 60px;
  margin: 10px 0;
}

a {
  color: orange;
  font-size: x-large;
}
<body>
  <div class="nav">
    <div class="logo">
      <ul>
        <li>Hello</li>
      </ul>
    </div>

    <div class="butn">
      <a href="#">click me</a>
    </div>
  </div>
</body>

答案 1 :(得分:0)

这是您的答案。不要在每个元素上使用高度,而在按钮和徽标上使用float属性,而应使用flex属性。

* {
        margin: 0;
        padding: 0;
      }

      .nav {
        width: 100%;
        height: 60px;
        background: #393e46;
        display: flex;
        align-items: center;
      }

      .nav .logo {
        text-align: center;
        color: orange;
        font-size: xx-large;
      }

      .nav .butn {
        background: beige;
        margin: auto;
      }

      a {
        color: orange;
        font-size: x-large;
      }
<div class="nav">
      <div class="logo">
        <ul>
          <li>Hello</li>
        </ul>
      </div>

      <div class="butn">
        <a href="#">click me</a>
      </div>
    </div>