html - 我有一个导航栏。如何将元素的高度设置为导航栏的 100%?

标签 html css height navbar

我一直在尝试制作一个导航栏,但一直不太成功。导航栏 div 的高度为 60px,但是,我似乎无法以任何方式增加内部元素的高度。 (填充除外)我做错了什么?
What I'm getting
What I'm trying to get

#navbar {
      width: 100vw;
      height: 60px;
      background: #deff00;
      box-shadow: 0 0 50px black;
      z-index: 2;
      position: relative;
      top: 85px;
    }
    #navbar ul {
      height: 100%;
      list-style: none;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    .navbar-link {
      text-decoration: none;
      color: black;
      height: 60px;
      padding: 0 20px;
    }
    .navbar-link:hover {
      background: #adc703;
    }
<div id="navbar">
          <ul>
            <li>
              <a href="#" style="font-weight: bold;" class="navbar-link"
                >ÚVODNÍ STRÁNKA</a
              >
            </li>
            <li>
              <a href="#" class="navbar-link">ŠKOLA</a>
            </li>
            <li><a href="#" class="navbar-link">STUDIUM</a></li>
            <li><a href="#" class="navbar-link">FOTOGALERIE</a></li>
            <li><a href="#" class="navbar-link">KONTAKT</a></li>
          </ul>
    </div>

    

谢谢!

最佳答案

试试这个:

#navbar {
      width: 100vw;
      height: 60px;
      background: #deff00;
      box-shadow: 0 0 50px black;
      z-index: 2;
      position: relative;
      top: 85px;
}
#navbar ul {
      height: 100%;
      list-style: none;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
}

#navbar li {
      display: table;
} 

.navbar-link {
      text-decoration: none;
      color: black;
      height: 60px;
      padding: 0 20px;
      vertical-align: middle;
      display: table-cell;
}

.navbar-link:hover {
      background: #adc703;
}

刚刚为 li 添加了 display: table;,为 a 标签添加了 vertical-align: middle;display: table-cell,有时这种旧技术非常适合) 代码笔:https://codepen.io/Liveindream/pen/mdyXmxj?editors=1100

关于html - 我有一个导航栏。如何将元素的高度设置为导航栏的 100%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59634843/

相关文章:

html - 通过 SSL 登录到 servlet

html -::before,::after 选择器在使用 position:absolute 时转到下一行;

jquery - CSS 定位问题

html - 如何在 HTML/CSS 中设置百分比高度值

javascript - 如何根据 body 高度动态更改元素 margin-top

jquery - 让两个div具有相同的高度

html - 如何使相同的颜色在不同的浏览器中看起来相同?

javascript - 仅使用 Javascript 访问 DOM 元素

css - 在悬停时添加一些样式并更改链接颜色 : CSS

javascript - 一起注释掉 HTML、CSS、Javascript 代码