固定顶部横幅+导航

4
我想知道如何使我的网站拥有固定的顶部横幅和导航栏,这样当人们浏览网站时,横幅和导航将保持在原位,而内容将像往常一样滚动。
HTML代码:
<!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8" />
        <title>Play - Learn - Grow</title>
        <link rel="stylesheet" href="main.css">
    </head>

    <body class="body">

        <span class="banner_h">
            <img src="Images\Top_Banner_4.png" alt="Banner" height="150" width ="1240" />
        </span>

        <nav>
            <ul class="nav">
                <li><a href="index.html">Home</a></li>
                <li><a href="about.html">About Us</a></li>
                <li><a href="contact.html">Contact Us</a></li>
                <li><a href="membership.html">Become a Member</a></li>
                <li><a href="borrow.html">Borrow Toys</a></li>
                <li><a href="policies.html">Our Policies</a></li>
                <li><a href="sitemap.html">Site Map</a></li>
            </ul>
        </nav>

        <span class="banner_l">
            <img src="Images\Side_Banner.jpg" alt="Banner" />
        </span>

        <span class="banner_r">
            <img src="Images\Side_Banner.jpg" alt="Banner" />
        </span>

        <h2 class="headers">Welcome to the Home Page!</h2>

        <div class="container">

            Our aim is to provide the children of the community with an ever-changing variety of educational and fun toys to enhance
            their cognitive, social, emotional and physical development in the important first six years of their lives.

            <br><br><span class="Links">Be sure to check out our Wikispace site with more information <a href="http://mysocialmediatools-pn.wikispaces.com/">here</a>!</span>

        </div>

        <div id="content"></div>

        <div id="footer">
            Copyright &copy 2013
        </div>


      </body>

</html>

CSS(层叠样式表):
/* Entire Document CSS */
html{
    height: 100%;
}
/* Header CSS */
.headers{
    color: #FFD89A;
    text-align: center;
    padding: 10px;
}
/* Body CSS */
.body{
    background-color: #61B329;
    height: 50%;
    color: #FFFFFF;
}
.container{
    margin: 0 auto 0 auto;
    width: 50em;
    text-align: center;
    padding-bottom: 500px;
    height: 50%;
}
/* Navigation CSS */
.nav {
    display: inline-block;
    background-color: #00B2EE;
    border: 1px solid #000000;
    border-width: 1px 0px;
    margin: 0;
    padding: 0;
    min-width: 1000px;
    width: 100%;
}
.nav li {
    list-style-type: none;
    width: 14.28%;
    float: left;
}
.nav a {
    display: inline-block;
    padding: 10px 0;
    width: 100%;
    text-align: center;
}
/* Banner / Picture CSS / Text in Images */
.banner_l{
    float: left;
}
.banner_r{
    float: right;
}
.banner_h{
    display: block;
    width: 100%;
}
.banner_h img{
    width: 100%;
}
/* Footer CSS */
#footer {
 clear: both;
 position: relative;
 z-index: 10;
 height: 3em;
 margin-top: -3em;
}
#content {
    padding-bottom: 3em;
}
/* Link CSS */
a:link{
    color: #FFFFFF;
    text-decoration: none;
}
a:visited{
    color: #FFFFFF;
    text-decoration: none;
}
a:hover{
    background-color: #028482;
    color: #FFFFFF;
    text-decoration: underline;
}
a:active{
    background-color: #FCDC3B;
    color: #AA00FF;
    text-decoration: overline;
}
.Links A:hover{
    color: #028482;
    background-color: transparent;
    text-decoration: underline overline;
}

谢谢!


1
固定定位(position: fixed)怎么样? - hugo der hungrige
那并没有完全完成整个工作,但还是谢谢! - Deadpool
1个回答

8
你需要将横幅和导航都包裹在一个固定位置的元素中。
HTML(仅包含头部)。
<header>
    <span class="banner_h">
        <img src="Images\Top_Banner_4.png" alt="Banner" height="150" width ="1240" />
    </span>

    <nav>
        <ul class="nav">
            <li><a href="index.html">Home</a></li>
            <li><a href="about.html">About Us</a></li>
            <li><a href="contact.html">Contact Us</a></li>
            <li><a href="membership.html">Become a Member</a></li>
            <li><a href="borrow.html">Borrow Toys</a></li>
            <li><a href="policies.html">Our Policies</a></li>
            <li><a href="sitemap.html">Site Map</a></li>
        </ul>
    </nav>
</header>

CSS:

 header {
     position:fixed;
     top: 0;
     left: 0;
     width: 100%;
 }

不幸的是,整个“页眉”现在只覆盖了主要内容,整个页面(除页眉外)已经上移,并且现在被页眉遮盖住了。 - Deadpool
2
你需要在body标签顶部添加填充来补偿固定高度的页眉。例如 - body {padding-top: 100px;} - jabs83
啊,那似乎非常好地解决了问题!非常感谢! - Deadpool
我想知道如何创建一个类似于Stack Overflow上的交互式顶部横幅。我正在寻找完整的指南、模板或书籍等。 - mtleis
我实际上将我的动态菜单叠加在自身之上,因此它在固定和页面上占用相同的空间。这个方法很好用,但在iPhone上如果你滚动太快就会暴露这个技巧。 - PRMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接