简体   繁体   English

修复屏幕顶部的“粘性”导航栏

[英]Fix “sticky” navigation bar on top of the screen

So, I've been trying to create a navigation bar, using HTML and CSS that will "stick" to the top of the page, even when the user scrolls down. 所以,我一直在尝试创建一个导航栏,使用HTML和CSS“粘贴”到页面顶部,即使用户向下滚动。 Used the position: sticky tag to do it. 使用position: sticky标签来做到这一点。 The problem is, I am not able to fix the div at the top of the page. 问题是,我无法修复页面顶部的div。

That's my CSS: 这是我的CSS:

.bar {
  position:sticky;
  top:0;
  color: white;
  background-color: black
}

p {
  display: inline;
}

... and my HTML code: ...和我的HTML代码:

<nav class="bar">
  <p>Home</p>
  <p>Info</p>
  <p>Contact Us</p>
</nav>

The sticky propriety works. sticky礼仪有效。 The problem is, bar is not "fixed" at the top of the page. 问题是, bar不是“固定”在页面顶部。 I know. 我知道。 Not the best word used for the explanation. 不是用于解释的最佳单词。 I just want it to be there, without any margins. 我只是希望它在那里,没有任何边缘。 Tried to reduce from the body's padding/margins but didn't work. 试图减少身体的填充/边缘,但没有奏效。 What can I do? 我能做什么?

Another question: what does really top:0 do? 另一个问题:真正top:0是什么top:0呢? The position:sticky propriety doesn't work without it and I want to know why. 这个position:sticky如果没有它, position:sticky适当就行不通了,我想知道为什么。

sticky positioning will be treated as relative positioned until you start scrolling down. 粘贴定位将被视为相对定位,直到您开始向下滚动。 so if the parent of the sticky element has some margin/padding it will affect the element. 所以如果粘性元素的父元素有一些边距/填充,它将影响元素。

you might want to try fixed positioning so that the element is always fixed at the top without any margin. 您可能想要尝试固定定位,以便元素始终固定在顶部而没有任何边距。

of course, fixed element removes the gap of where the element was supposed to be so you content might overlap and will need to add another element with the same height as your fixed element to fix overlapping content. 当然,fixed元素会删除元素所在位置的间隙,因此内容可能会重叠,并且需要添加另一个与固定元素具有相同高度的元素来修复重叠内容。

see below for example. 见下面的例子。

 .bar { position:fixed; top:0; color: white; background-color: black; width:100%; left:0; } p { display: inline; } 
 <nav class="bar"> <p>Home</p> <p>Info</p> <p>Contact Us</p> </nav> <div style="height:1000px"> </div> 

To solve the margin issue on your navbar, it can be a good idea to put in a reset at the top of your CSS code to override the browser defaults. 要解决导航栏上的边距问题,最好在CSS代码顶部进行重置以覆盖浏览器默认值。 Here I've set the margins and paddings on all the elements to 0. Since it's at the top, any styles following it will override the reset, and you can customise to your heart's content. 在这里,我将所有元素的边距和填充设置为0.由于它位于顶部,因此它后面的任何样式都将覆盖重置,您可以根据自己的内容进行自定义。

The Top attribute is necessary for the position sticky to work because the browser needs to determine the final location of the element. 由于浏览器需要确定元素的最终位置,因此Top属性对于粘滞位置是必要的。 Left, Right and Bottom attributes will also do the job. Left,Right和Bottom属性也可以完成这项工作。

 * { margin: 0px; padding: 0px; } .bar { position: -webkit-sticky; position: sticky; top: 0; color: white; background-color: black } p { display: inline; } .demoFiller { background-color: coral; } 
 <nav class="bar"> <p>Home</p> <p>Info</p> <p>Contact Us</p> </nav> <div class="demoFiller"> 0<br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> 0 <br> 1 <br> 2 <br> 3 <br> 4 <br> 5 <br> 6 <br> 7 <br> 8 <br> 9 <br> </div> 

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

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