如何垂直对齐导航栏的链接?

时间:2017-11-05 15:31:37

标签: html css vertical-alignment

我正在尝试制作导航栏,但我不确定如何垂直排列SELECT `id`, `icon`, `parent` FROM `category` AS `category` LIMIT 10; li标签。我怎样才能做到这一点?

a
#navbar {
    font-weight: bold;
    display: block;
    background-color: #000;
}

li {
    float: left;
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #000;
}

li a {
    color: white;
    vertical-align: middle;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover {
    background-color: #111;
}

1 个答案:

答案 0 :(得分:1)

现在常见的一种方法是使用 Flexbox

#navbar {
  font-weight: bold;
  display: block;
  background-color: #000;
}

ul {
  display: flex; /* displays flex-items (children) inline */
  align-items: center; /* centers them vertically */
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #000;
}

li {
  float: left;
}

li a {
  color: white;
  vertical-align: middle;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: #111;
}

/* addition */
img {display: block} /* or: "vertical-align: middle" // removes bottom margin/whitespace */
<div id="navbar">
  <ul>
    <li><img src="https://placehold.it/100x100" alt="" width="75" height="75"></li>
    <li><a href="#">HOME</a></li>
    <li><a href="#">MY WORK</a></li>
    <li><a href="#">HIRE ME</a></li>
    <li><a href="#">PROJECTS</a></li>
  </ul>
</div>