CSS中心,固定导航

时间:2013-08-21 16:13:23

标签: css

我的页面宽度为1600px。主要区域只有900px宽。我有一个导航,假设固定在页面的中心(它是)。我的问题是当我打开页面时,页面被固定而不是在打开时居中。当用户访问网站时,我需要做什么才能将900px内的所有内容集中在一起?

代码不准确,因为它很详细,但基本上是这样的:http://jsfiddle.net/wznQk/

<body>
    <div class="container">
        <div class="header">
            <div class="subheader">
                <div class="navigation">
                    <ul>
                        <li>HOME</li>
                        <li>ABOUT</li>
                        <li class="logo"><img src="images/ogsystemslogo.png" /></li>
                        <li>CAREERS</li>
                        <li>CONTACT</li>
                    </ul>
                </div>

                    <div class="undernav">
                        <div class="short">
                            <img src="images/bluemark.png" />
                            <div class="top">TOP OGS NEWS:</div>
                        </div>
                   </div>
            </div>
        </div>
        <div class="content">
        </div>
    </div>
</body>

CSS

.body {
    width: 1600px;
    height: 100%;
    margin: 0 auto;
    padding: 0px;
    border: 0px;
    position: relative;
    text-align: center;
}

.container {
    width: 1600px;
    height: 100%;
    position: relative;
    margin: 0 auto;
    padding: 0px;
    border: 0px;
    text-align: center;
}

.header {
    width: 1600px;
    height: 150px;
    margin: 0 10% 0 10%;
    padding: 0px;
    border: 0px;
    background-color: white;
    position: fixed;
}

.subheader {
    width: 1600px;
    height: 100px;
    margin: 0 auto;
    position: fixed;
    background-color: white;
    top: 0px;
}

.navigation {
    font-family: 'Champagne & Limousines';
    font-size: 20px;
    text-align: left;
    width: 1600px;
    height: 100px;
    padding: 0px;
    margin-left: 0 auto;
    border: 0px;
    list-style: none;
    text-decoration: none;
    display: table-cell;
    vertical-align: middle;
    color: #006699;
    background-color: white;
}

    .navigation ul {
        width: 590px;
        height: 20px;
        list-style: none;
        text-decoration: none;
        position: relative;
        line-height: 55px;
        margin: 0 auto;
        background-color: white;
        padding: 0px;
        border: 0px;

    }

    .navigation ul li {
        width: 70px;
        height: 15px;
        float: left;
        padding-left: 35px;
        background-color: white;
    }

2 个答案:

答案 0 :(得分:0)

请尝试添加此CSS而不是您的CSS。在你的CSS中我发现了许多不需要的CSS标签,但我保留了它们。

body {
    width: 1600px;
    height: 100%;
    margin: 0 auto;
    padding: 0px;
    border: 0px;
    position: relative;
    text-align: center;
}
.container {
    width: 900px;
    height: 100%;
    position: relative;
    margin: 0 auto;
    padding: 0px;
    border: 0px;
    text-align: center;
}
.header {
    width: 1600px;
    height: 150px;
    padding: 0px;
    border: 0px;
    background-color: white;
    position: fixed;
}
.subheader {
    width: 900px;
    height: 100px;
    position: fixed;
    background-color: white;
    top: 0px;
}
.navigation {
    font-family: 'Champagne & Limousines';
    font-size: 20px;
    text-align: left;
    width: 590px;
    height: 100px;
    padding: 0px;
    margin: 0 auto;
    border: 0px;
    list-style: none;
    text-decoration: none;
    vertical-align: middle;
    color: #006699;
    background-color: white;
}
.navigation ul {
    width: auto;
    height: 20px;
    list-style: none;
    text-decoration: none;
    position: relative;
    line-height: 55px;
    background-color: white;
    padding: 0px;
    border: 0px;
}
.navigation ul li {
    width: 70px;
    height: 15px;
    float: left;
    padding-left: 35px;
    background-color: white;
}

答案 1 :(得分:0)

Css:

.header {
    width: 1600px;
    height: 150px;
    left: 50%;
    margin-left: -800px;/*half of your total width*/
    padding: 0px;
    border: 0px;
    background-color: white;
    position: fixed;
}

left: 50%; margin-left: -800px;/*half of your total width*/添加到您的.header类

小提琴:http://jsfiddle.net/avinvarghese/wznQk/3/show/