html - css 导航栏位置固定不工作

标签 html css

我正在尝试制作一个网站。我决定让导航栏有一个固定的位置,这样当我向下滚动时,我总能看到它,但是,当我将属性 position:fixed 添加到 nav 元素时,它就会消失。无法弄清楚发生了什么。有人可以解释一下,我做错了什么吗?非常感谢! P.S:我是编码新手。

HTML 和 CSS

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

html,
body {
  width: 100%;
  height: 100%;
}

header nav #logo {
  width: 140px;
  position: absolute;
  top: 15px;
}

nav {
  position: relative;
  background-color: #242628;
  height: 70px;
  padding-left: 20px;
}

nav ul {
  position: absolute;
  height: 100%;
  margin: auto;
  top: 0;
  bottom: 0;
  width: 600px;
  padding-left: 200px;
}

nav ul li {
  -moz-transition: all 0.2s linear;
  -webkit-transition: all 0.2s linear;
  -o-transition: all 0.2s linear;
  transition: all 0.2s linear;
  display: inline;
  float: left;
  height: inherit;
  width: 100px;
  border-right: 1px solid gray;
}

nav ul li:hover {
  background-color: rgba(12, 240, 255, 0.3);
}

nav ul li a {
  color: white;
  text-decoration: none;
  position: absolute;
  height: inherit;
  width: inherit;
  text-align: center;
  padding-top: 25px;
}
<header>
  <nav>
    <img id="logo" src="images/logo.png" alt="logo" />
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Rate it!</a></li>
      <li><a href="#">Courses</a></li>
      <li><a href="#">Videos</a></li>
    </ul>
  </nav>
</header>

最佳答案

一旦你确定了位置,你必须指定你想要的位置。

顶部:20px; 左:0px;

等等......

关于html - css 导航栏位置固定不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42680018/

相关文章:

javascript - 如何使用 javascript(没有 JQuery)进行日期屏蔽?

html - CSS画廊帮助UI开发

html - 将响应式 css 菜单对齐到中间而不改变它的移动分辨率

html - 响应式导航栏元素

css - GWT DataGrid -- CSS 边框不生效

javascript - 检查链接是否有 https ://in angularjs

java - 通过 Tab 键导航无法正常工作

javascript - 在 ReactJS 中从组件状态传递样式

css - 在 Bootstrap 中显示容器外的内容

css - 使用 Bulma CSS 使用图像和文本 block