简体   繁体   English

如何制作 <ul> 始终坚持浏览器页面的顶部?

[英]How to make a <ul> always stick to the top of the browser page?

I have a <ul> at the top of the page showing navigation items. 我在页面顶部有一个<ul> ,显示导航项。 Currently its positioned via position:absolute to stick in place. 当前它的位置是通过position:absolute坚持到位。 Code: 码:

#navigation
{
    text-align: center; 
    position: absolute; 
    float: left;    
    margin: 0;
    padding: 0;
    list-style-type: none;
}

#navigation li
{
    display: inline-block; 
    width: 150px;
    height: 110px;
    cursor: pointer;
}

What I want to do, is to have this navigation bar remain glued to the top of the screen, and not be affected by the user's scrolling underneath the page. 我想要做的是使此导航栏保持粘贴在屏幕顶部,并且不受用户在页面下方滚动的影响。 Kind of like this page: 有点像这个页面:

http://www.google.com/intl/en/enterprise/apps/business/products.html#drive http://www.google.com/intl/en/enterprise/apps/business/products.html#drive

How can this be done? 如何才能做到这一点?

Set the position to "fixed" 将位置设置为“固定”

#navigation {
    text-align: center; 
    position: fixed; 
    top: 0;
    left: 0;
    float: left;    
    margin: 0;
    padding: 0;
    list-style-type: none; }

use top and left property: 使用topleft属性:

#navigation
{
    text-align: center; 
    position: absolute; 
    float: left;    
    margin: 0;
    padding: 0;
    top: 0;
    left: 0;
    list-style-type: none;
}

You also could try to set the fixed position and set the z-index with a value greater than the other elements. 您也可以尝试设置fixed位置,并将z-index设置为大于其他元素的值。 The divs inside this #navigation have to be position: relative to get the same behaviour. #navigation的div必须position: relative才能获得相同的行为。

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

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