8

我试图在我的网站上将两列居中,但存在一些问题。每次更改的结果都是左位置(见图。我究竟做错了什么?这是我的CSS:

body {
    background - image: url("../img/gray.png");
}

#header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100px;
    background: #cc0000;
    text - align: center;
}

#wrapper {
    margin: 100px;
    width: 1280px;
}

#leftcolumn {
    float: left;
    background - image: url("../img/gray.png");
    margin: 10px 0px 10px 0px;
    padding: 10px;
    height: 682px;
    width: 460px;
}

#rightcolumn {
    float: left;
    color: #333;
    border: 1px solid # ccc;
    background: #F2F2E6;
    margin: 10px 0px 10px 0px;
    padding: 10px;
    height: 500px;
    width: 439px;
    display: inline;
    position: relative;
}

谢谢

4

2 回答 2

9

由于两列的宽度小于包装器的宽度(即 959 像素与 1280 像素),因此您需要将两列放置在固定宽度的容器中:

<div id="wrapper">
    <div id="column_container">
        <div id="column1"></div>
        <div id="column2"></div>
    </div>
</div>

然后执行以下操作:

#column_container {
    width: 959px;
    margin: 0 auto;
}
于 2013-02-11T15:16:05.877 回答
3

在没有看到它的情况下,这样的事情应该可以工作:

#wrapper { 
   width: 1280px;
   margin:100px auto ;
}

但正如已经提到的,这只会 center #wrapper。这些列没有填满整个 1280 宽度,因此它们仍然在#wrapper.

于 2013-02-11T15:12:43.050 回答