简体   繁体   English

CSS导航栏位置固定不起作用

[英]css navigation bar position fixed not working

I am trying to make a website. 我正在尝试建立一个网站。 I decided to make the navigation bar have a fixed position so when I scroll down I can always see it but, when I add to the nav element the property position:fixed it just disappears. 我决定将导航栏设置为固定位置,以便在向下滚动时始终可以看到它,但是当我向nav元素添加属性position:fixed时,它就会消失。 Cant' figure out what's happening. 不能知道发生了什么。 Can someone explain, what I'm doing wrong? 有人可以解释我在做什么错吗? Thank you very much! 非常感谢你! PS: I am new to coding. PS:我是编码新手。

HTML and CSS 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> 

Once you fix the position you have to specify where you want it. 确定位置后,您必须指定所需位置。

top: 20px; 顶部:20px; left: 0px; 左:0px;

Etc.... 等等....

You have to add a width to fixed elements (100% in this case): 您必须为固定元素添加width (在这种情况下为100%):

 * { 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: fixed; width: 100%; 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> 

If your nav is set to realtive and is changed to fixed your absolute elements will be relative to the body and those elements will leave the nav . 如果您的nav设置为realtive而变成fixedabsolute元素将相对于body和这些元素将离开nav

Change the position of the absolute elements and the css of the nav to: absolute元素的positionnav的css更改为:

nav {
    background-color: #242628;
    height: 70px;
    padding-left: 20px; }
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
}

I let you here a working version. 我在这里给你一个工作版本。 I only replaced relative with fixed in your code to the nav 我只将fixed relative替换为nav中的代码

 * { box-sizing: border-box; padding: 0; margin: 0; } html, body { width: 100%; height: 300%; } header nav #logo { width: 140px; position: absolute; top: 15px; } nav { position: fixed; 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> 

Note that I set height to 300% to have some scroll on the document 请注意,我将height设置为300%,以便在文档上滚动

I honestly don't see anything wrong: 老实说,我没有发现任何错误:

http://codepen.io/anon/pen/BWpLdd http://codepen.io/anon/pen/BWpLdd

.scrollTest {
  height: 2000px;
  background: -moz-linear-gradient(top, #a90329 0%, #8f0222 44%, #6d0019 100%); /* FF3.6-15 */
  background: -webkit-linear-gradient(top, #a90329 0%,#8f0222 44%,#6d0019 100%); /* Chrome10-25,Safari5.1-6 */
  background: linear-gradient(to bottom, #a90329 0%,#8f0222 44%,#6d0019 100%);
}
* {
     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: fixed;
     background-color: #242628;
     height: 70px;
     padding-left: 20px;
     width: 100%;
}

nav ul {
     position: absolute;
     height: 100%;
     margin: auto;
     top: 0;
     bottom: 0;
     width: 800px;
     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-block;
    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; }

Make sure you're adding it to the nav parent element. 确保将其添加到导航父元素。

 * { 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: fixed; background-color: #242628; height: 70px; padding-left: 20px; width: 100%; background-color: black; } nav ul { position: relative; height: 100%; margin: auto; top: 0; bottom: 0; } 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: 200px; 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; } article{ height: 500px; } 
 <header> <nav> <img id="logo" src="" 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> <article></article> 

Working code 工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM