导航栏对齐<​​ul>

时间:2019-03-15 18:44:01

标签: html css bootstrap-4

我需要在导航栏的中间垂直对齐该栏。我将align-items:设置为bar的中心,但它仅使登录按钮对齐。如您所见,它略高于中间位置。

nav a{
color:white;
}
nav{
    justify-content: space-between;
    align-items: center;
    background-color: brown;

}
nav ul{
    list-style: none;
    padding: 0;
    display: flex;
    align-items: center;
    background-color: black;

}
nav ul li a{
    padding: 1rem 0;
    margin: 0 0.5rem;
    position: relative;
    font-size: 14px;
    font-weight: bold;
}
.container{
  background-color: green;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container" >
            <nav class="d-flex flex-row">
                <h1 class="sling"><a href="index.html">Sling</a></h1>
                <ul>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Features</a></li>
                    <li><a href="#">Pricing</a></li>
                    <li><a href="#">Testimonial</a></li>
                    <li><a href="#">Download</a></li>
                    <li><a href="#">News</a></li>
                    <li><a href="#">Contact us</a></li>
                </ul>
                <button type="button" class="signin">Sign in</button>
            </nav>
        </div>

正确。

3 个答案:

答案 0 :(得分:2)

您需要将margin的底部nav ul置零。由于默认的浏览器样式,因此存在边距。

nav a {
  color: white;
}

nav {
  justify-content: space-between;
  align-items: center;
  background-color: brown;
}

nav ul {
  list-style: none;
  padding: 0;
  margin-bottom: 0; /* <-- + Added */
  display: flex;
  align-items: center;
  background-color: black;
}

nav ul li a {
  padding: 1rem 0;
  margin: 0 0.5rem;
  position: relative;
  font-size: 14px;
  font-weight: bold;
}

.container {
  background-color: green;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container">
  <nav class="d-flex flex-row">
    <h1 class="sling"><a href="index.html">Sling</a></h1>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Features</a></li>
      <li><a href="#">Pricing</a></li>
      <li><a href="#">Testimonial</a></li>
      <li><a href="#">Download</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Contact us</a></li>
    </ul>
    <button type="button" class="signin">Sign in</button>
  </nav>
</div>

答案 1 :(得分:2)

在UL上使用mb-0清除底部边距。

    <nav class="d-flex flex-row align-items-center">
        <h1 class="sling"><a href="index.html">Sling</a></h1>
        <ul class="mb-0">
            <li><a href="#">Home</a></li>
            <li><a href="#">Features</a></li>
            <li><a href="#">Pricing</a></li>
            <li><a href="#">Testimonial</a></li>
            <li><a href="#">Download</a></li>
            <li><a href="#">News</a></li>
            <li><a href="#">Contact us</a></li>
        </ul>
        <button type="button" class="signin">Sign in</button>
    </nav>

https://www.codeply.com/go/Fa33cYUN52

答案 2 :(得分:0)

其他人都说要清除保证金底部。怎么align-items: baseline;而不是居中呢?

nav {
    justify-content: space-between;
    align-items: baseline;
    background-color: brown;
}

https://jsfiddle.net/davidliang2008/3nzgecrt/4/

enter image description here

相关问题