如何将导航器垂直居中到徽标?

时间:2017-09-17 23:57:14

标签: html css vertical-alignment nav

我正在尝试创建一个带右侧徽标的标题栏,左侧水平导航像往常一样。我需要导航项目垂直居中于徽标。

我尝试了几种方法。我很感激,如果有人能告诉我解决方法,并解释为什么它有效,以及我可能缺少的东西。

body {
  max-width: 995px;
  margin: 0 auto;
  padding: 20px 0;
  box-sizing: border-box;
  font-family: 'Arial';
}

a {
  text-decoration: none;
  color: #222;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.logo img {
  max-width: 235px;
  height: auto;
  float: left;
}

.menu li {
  display: inline-flex;
  padding-left: 30px;
}

.menu {
  float: right;
}

.nav {
  padding-bottom: 25px;
}

.group:after {
  content: "";
  display: table;
  clear: both;
}
<div class="nav group">
  <div class="logo"><img src="img/ahlogo.png" alt="Abdulla Hussain's Logo"></div>
  <div class="menu">
    <ul>
      <li><a href="#about">About</a></li>
      <li><a href="#portfolio">Portfolio</a></li>
      <li><a href="#thoughts">Thoughts</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
    </ul>
  </div>
</div>

非常感谢!

2 个答案:

答案 0 :(得分:0)

我想您想将导航与徽标的底端对齐?如果是,您可以在两者的父元素上使用display:flex以及以下设置:

.nav.group {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

&#13;
&#13;
body {
  max-width: 995px;
  margin: 0 auto;
  padding: 20px 0;
  box-sizing: border-box;
  font-family: 'Arial';
}

.nav.group {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

a {
  text-decoration: none;
  color: #222;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.logo img {
  max-width: 235px;
  height: auto;
  float: left;
}

.menu li {
  display: inline-flex;
  padding-left: 30px;
}

.menu {
  float: right;
}

.nav {
  padding-bottom: 25px;
}

.group:after {
  content: "";
  display: table;
  clear: both;
}
&#13;
<div class="nav group">
  <div class="logo"><img src="http://placehold.it/120x120/0af" alt="Abdulla Hussain's Logo"></div>
  <div class="menu">
    <ul>
      <li><a href="#about">About</a></li>
      <li><a href="#portfolio">Portfolio</a></li>
      <li><a href="#thoughts">Thoughts</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

备选方案:如果您想垂直居中对齐,请将align-items:更改为center;(或top以进行顶部对齐)

&#13;
&#13;
body {
  max-width: 995px;
  margin: 0 auto;
  padding: 20px 0;
  box-sizing: border-box;
  font-family: 'Arial';
}

.nav.group {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

a {
  text-decoration: none;
  color: #222;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.logo img {
  max-width: 235px;
  height: auto;
  float: left;
}

.menu li {
  display: inline-flex;
  padding-left: 30px;
}

.menu {
  float: right;
}

.nav {
  padding-bottom: 25px;
}

.group:after {
  content: "";
  display: table;
  clear: both;
}
&#13;
<div class="nav group">
  <div class="logo"><img src="http://placehold.it/120x120/0af" alt="Abdulla Hussain's Logo"></div>
  <div class="menu">
    <ul>
      <li><a href="#about">About</a></li>
      <li><a href="#portfolio">Portfolio</a></li>
      <li><a href="#thoughts">Thoughts</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这使用display:flex来设置一行中的项目。

如果您需要使用不支持display:flex的浏览器(我认为IE&lt; 9)并且您想要一种不同的方式,请告诉我。

包括开放sans,因为我喜欢它的外观。

橙色用于展示元素的中间位置。

如果您知道徽标的高度,这将有效。如果它未知,或者可能更改高度(很多地方的页面滚动后导航栏会略微缩小),您可能需要一个不同的解决方案。

&#13;
&#13;
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,700');
.nav {
  display: flex;
  background-color: orange;
  margin: 0;
  padding: 0;
  height: 50px;
  font-family: "Open Sans";
  font-weight: bold;
  justify-content: space-between;
}

.menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.menu ul li {
  display: inline-block;
  padding: 5px 10px;
  box-sizing: border-box;
  line-height: 40px;
  font-size: 20px;
}

.menu ul li:first-child {
  margin-left: 10px;
}

.menu ul li a {
  text-decoration: none;
}
&#13;
<div class="nav group">
  <div class="logo"><img src="//placehold.it/200x50" alt="Abdulla Hussain's Logo"></div>
  <div class="menu">
    <ul>
      <li><a href="#about">About</a></li>
      <li><a href="#portfolio">Portfolio</a></li>
      <li><a href="#thoughts">Thoughts</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;