简体   繁体   English

滚动时如何使导航栏保持在页面顶部?

[英]How do I make my navigation bar remain on top of page while scrolling?

Here is the HTML code 这是HTML代码

<div id="header" class="grid_12">
<h6>DCHOST - Brought to you by <a href="http://www.dt.co.uk" target="_blank">
DataConnectivity.co.uk
</a></h6>
            <ul>
                <li>Home</li>
                <li>Hosting</li>
                <li>Domain Names</li>
                <li>Lates News</li>
                <li>About us</li>
                <li>Contact Us</li>
            </ul>
        </div> <!-- ends header grid 12 -->

CSS CSS

#header {
color: #848484;
height: 70px;
width: 100%;
font-size: small;
font-style: oblique;
text-align: center;
background: #333333;}

#header h6 {
max-height: 3px;
font-size: 10px;
text-align:left;
}

#header li {
font-weight: 800;
color: #FFFFFF;
font-style: normal;
display: inline}

I would like my nav bar to remain fixed on top of page. 我希望我的导航栏保持固定在页面顶部。 However, every time I change position to fixed, the bar completely disappears. 但是,每次我将位置更改为固定时,该条都会完全消失。 I have no idea why its doing this? 我不知道为什么要这么做? Thanks in advance for your help! 在此先感谢您的帮助!

You need to position the header with top: 0; 您需要将标题放置在top: 0; and set the z-index to be higher than all other elements. 并将z-index设置为高于所有其他元素。

#header {
    position: fixed;
    top: 0;
    z-index: 999;
}

This will also add compatibility between browsers. 这还将增加浏览器之间的兼容性。

Demo 演示

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

相关问题 如何让我的导航栏在滚动时保持在页面顶部? - How do I make my nav bar remain on top of page while scrolling? 滚动时如何将导航栏固定在顶部? - How do I keep my navigation bar fixed on top while scrolling? 如何使我的导航栏保持静态(在向下滚动时固定在页面顶部)? - How do i make my nav bar static (fixed at the top of the page when scrolling down)? 滚动时如何使此导航栏浮动在顶部? - How to make this navigation bar floating on top while scrolling? 仅滚动时,如何使导航菜单停留在页面顶部? - How to make navigation menu stay at the top of the page while scrolling only? 如何使响应式导航菜单保留在页面顶部? - How to make a responsive navigation menu remain at the top of a page? 如何将导航栏居中,使其位于页面顶部的中间? - How do I center my navigation bar so it will be placed in the middle at the top of my page? 即使放大或缩小,如何使页面的顶部与主体保持不同的颜色? - How do I make the top portion of my page remain a different color than the body even when I zoom-in or out? 当用户滚动一定量时,如何使导航栏自身“固定”在页面顶部? - How do I make a navigation bar “affix” itself to the top of the page when a user scrolls a certain amount? 是什么使我的导航栏在滚动时停留在顶部? - What is making my navigation bar sticking on top while scrolling?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM