【问题标题】:Cannot center or raise navbar link items - something forcing them to the right and stopping me from raising them无法居中或提升导航栏链接项目 - 迫使它们向右移动并阻止我提升它们
【发布时间】:2020-07-22 18:42:16
【问题描述】:

我无法在纯 CSS 中的无序导航栏链接中居中或提升导航栏链接项。

我希望导航栏的链接位于屏幕顶部并覆盖导航栏区域的整个高度的块中,并且我希望所有项目居中。

body 上的边距填充边框为 0,我什至尝试使用 codepen 来隔离导航栏并尝试它仍然无法工作。

那么如何让导航栏链接占据导航栏区域的整个高度 - 以及如何将导航栏项目/链接居中,因为它们被轻微推到右侧而不是居中。

练习:https://codepen.io/pen/

nav {
  
  margin: 0px;
  border: 0px;
  padding: 0px;
  position: fixed;
  top: 0;
  width: 100%;
  height: 48px;
  background-color: darkslategrey;
  width: 100%;
 

}

nav ul {
  background-color: ;
  text-align: center;
  align-items: center;
  
} 

nav ul li {
  background-color: ;
  display: inline-block;
  margin: 0px;
  text-align: center;
  align-items: center;
}

nav li a {
  
  color: #FFFFFF;
  background-color: lightgreen;
  display: inline-block; 
  text-decoration: none;
  font-size: 16px;
  padding-top: 20px;
  padding-right: 40px;
  padding-bottom: 8px;
  padding-left: 40px;
  line-height: 4px;
  height: 15px;
  margin: 0px;
  border: 0px;
  
}

nav li :visited {
  color: #FFFFFF;
<nav>
    <ul>
      <li> 
        <a href= "#homeSection">Home</a> 
      </li>
      <li> 
        <a href= "#firstSection">First</a> 
      </li>
      <li> 
        <a href= "#secondSection">Second</a> 
      </li>
      <li> 
        <a href= "#thirdSection">Third</a> 
      </li>
      
      </ul>
    </nav>

【问题讨论】:

    标签: html css html-lists navbar inline


    【解决方案1】:

    移除 ul 元素的边距和内边距。

    .nav ul {
        padding: 0;
        margin: 0;
    }
    

    您还应该从nav 中删除 height 属性,这样它就会增长到适合ul 所需的高度。

    【讨论】:

    • 这适用于高度你知道为什么它被推到右边 - 当我让 ul 元素的颜色与 li 和一个元素不同时,我可以看到它是 ul 正在推动它在右边,好像它有一个不可见的项目符号点,但即使我添加 list-style-type: none; 它仍然存在。到 ul 元素。
    • 没关系,谢谢我发现答案与您的答案相似 - 添加边距:0px;边框:0px;填充:0px;到 li 元素使导航栏居中。
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 2021-01-15
    • 2020-01-26
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多