如何在导航栏中垂直对齐按钮?

时间:2019-07-17 06:09:36

标签: html css vertical-alignment

我正在尝试创建网站,目前正在尝试复制this site。目前,我正在使用导航栏,但是无法在导航栏中垂直对齐按钮。

.header {
  background-color: red;
  padding: 40px 20px;
}

.header h1 {
  background-color: yellow;
  float: left;
}

.header h1 img {
  display: block;
}

.header__nav {
  background-color: aqua;
  float: right;
}

.header__nav li {
  float: left;
  height: 38px;
}

.header__nav li a {
  padding: 0 20px;
  display: inline-block;
  line-height: 38px;
}

.contents {
  background-color: green;
}

.footer {
  background-color: blue;
}
<header class="header clearfix">
  <h1>
    <a href="#"><img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo"></a>
  </h1>
  <nav class="header__nav">
    <ul class="clearfix">
      <li><a href="#">View icons</a></li>
      <li><a href="#">Buy now</a></li>
      <li><button class="menu">menu</button></li>
    </ul>
  </nav>
</header>
<section class="contents">
  contents
</section>
<footer class="footer">
  footer
</footer>

My working image

我想垂直对齐导航栏的右上角按钮。我知道可以使用display: flex;解决此问题,但我想用另一种方法解决。有解决此问题的适当方法吗?

2 个答案:

答案 0 :(得分:2)

即使您使用updateSql = "Update tblUserScans SET DeviceType = 'P1', uPassword = @uPassword Where ( UID = '" & UIDpasscode & "')" 的解决方案对我来说似乎很好,您也可以在所有项目(两个链接和按钮)上使用display: flexvertical-align: middle;进行修复

答案 1 :(得分:0)

.header {
  background-color: red;
  padding: 40px 20px;
}

.header h1 {
  background-color: yellow;
  float: left;
}

.header h1 img {
  display: block;
}

.header__nav {
  background-color: aqua;
  float: right;
}

.header__nav li {
  float: left;
  height: 38px;
}

.header__nav li a {
  padding: 0 20px;
  display: inline-block;
  <!-- Remove - line-height: 38px; -->
}

.contents {
  background-color: green;
}

.footer {
  background-color: blue;
}
<!-- new added class -->
.clearfix{
  display: flex;
  align-items: center;
}
<header class="header clearfix">
  <h1>
    <a href="#"><img src="https://pixelicons.com/wp-content/themes/pexelicons/assets/pic/site-logo/logo.png" alt="Logo"></a>
  </h1>
  <nav class="header__nav">
    <ul class="clearfix">
      <li><a href="#">View icons</a></li>
      <li><a href="#">Buy now</a></li>
      <li><button class="menu">menu</button></li>
    </ul>
  </nav>
</header>
<section class="contents">
  contents
</section>
<footer class="footer">
  footer
</footer>