仅将DIV置于中心而不是侧边栏

时间:2012-08-12 21:51:22

标签: css html

我有一个标题,一个侧边栏和一个内容div。

标题停靠在顶部并固定在那里。侧边栏固定在左侧。现在我有3/4的屏幕在右边没有空间。我有一个内容div,我想放在那里。它需要以3/4空间为中心。

这是我的侧边栏和内容div的代码。

CSS:

html,body {
    height:100%;
}
#sidebar {
    float: left;
    width: 300px;
    background: #fff;
    background: rgba(225,225,225,0.2);
    height:100%;
}
#sidebarTop {
    height:100%;
    background-color: #eee;
    padding:20px 50px;
    border-bottom:0.15em solid rgb(229,229,229);
    background: rgba(225,225,225,0.25);
    box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
    -o-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
    -webkit-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0px 1px rgba(255,255,255,0.3), inset 0px 0px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset -10px 0px 0px rgba(0,0,0,0.3);
    -moz-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
}
#content {
    margin: 0 auto;
    display:block;
    width:400px;
    margin-top:100px;
    height:900px;
    background:#FFF;
    -webkit-box-shadow: 0px 0px 30px rgba(255, 255, 255, 1);
    -moz-box-shadow:    0px 0px 30px rgba(255, 255, 255, 1);
    box-shadow:         0px 0px 30px rgba(255, 255, 255, 1);
}

这是我的侧边栏和内容div的HTML代码。

<div id="sidebar">
    <div id="sidebarTop">
        ....
    </div>
</div>
<div id="content">
    TEST
</div>

问题来了。会发生的是,不是忽略侧边栏并将内容div推向100px(margin-top:100px),而是将侧边栏降低到相同的水平。

我的目标是在侧边栏后面剩下的3/4屏幕空间中垂直和水平居中显示div,而标题填充其余部分。

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

float:left;添加到#content

#content {
    margin: 0 auto;
    width:400px;
    margin-top:100px;
    height:900px;
    background:#FFF;
    -webkit-box-shadow: 0px 0px 30px rgba(255, 255, 255, 1);
    -moz-box-shadow:    0px 0px 30px rgba(255, 255, 255, 1);
    box-shadow:         0px 0px 30px rgba(255, 255, 255, 1);
}

应该是:

#content
{
    margin:100px auto 0;
    width:400px;
    height:900px;
    background:#fff;
    -webkit-box-shadow:0 0 30px #fff;
    -moz-box-shadow:0 0 30px #fff;
    box-shadow:0 0 30px #fff;
    float:left;
}

更新

<强> CSS

html,
body
{
    height:100%;
}
#sidebar
{
    position:fixed;
    top:0;
    left:0;
    width:300px;
    height:100%;
    background-color:#758;
}
#content
{
    margin-left:-50px;
    position:absolute;
    top:100px;
    left:50%;
    width:400px;
    height:900px;
    background:#758;
}​

<强> HTML

<div id="sidebar">
    "fixed" sidebar
</div>
<div id="content">
    "horizontally centered" content
</div>​

<强> DEMO


更新2:

<强> CSS

#wrapper
{
    position:absolute;
    top:100px;
    left:300px;
    right:0;
    height:900px;
}
#content
{
    margin:0 auto;
    width:400px;
    height:100%;
    background:#758;
}

<强> HTML

<div id="sidebar">
    "fixed" sidebar
</div>
<div id="wrapper">
    <div id="content">
        "horizontally centered" content
    </div>
</div>​​​​​​​​​​

<强> DEMO 2

答案 1 :(得分:0)

你可以这样试试:
CSS:

html,body {
    height:100%;
}
#sidebar {
    border: 2px solid yellow; /*Added to show the box-model*/

    float: left;
    width: 25%;
    background: #fff;
    background: rgba(225,225,225,0.2);
    height:100%;
}
#sidebarTop {
    border: 2px solid black; /*Added to show the box-model*/

    height:100%;
    background-color: #eee;
    padding:20px 50px;
    border-bottom:0.15em solid rgb(229,229,229);
    background: rgba(225,225,225,0.25);
    box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
    -o-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
    -webkit-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0px 1px rgba(255,255,255,0.3), inset 0px 0px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset -10px 0px 0px rgba(0,0,0,0.3);
    -moz-box-shadow: 0 2px 6px rgba(0,0,0,0.5), inset 0 1px rgba(255,255,255,0.3), inset 0 10px rgba(255,255,255,0.2), inset 0 10px 20px rgba(255,255,255,0.25), inset 0 -15px 30px rgba(0,0,0,0.3);
}
#foo {
    border: 2px solid pink; /*Added to show the box-model*/

    margin-top: 100px;
    margin-left: 27%;
    width: 73%;
}
#content {
    border: 2px solid green; /*Added to show the box-model*/

    margin: 0 auto;
    display:block;
    width:400px;
    /*margin-top:100px;*/ /*this can be deleted*/
    height: 100%;
    background:#FFF;
    -webkit-box-shadow: 0px 0px 30px rgba(255, 255, 255, 1);
    -moz-box-shadow:    0px 0px 30px rgba(255, 255, 255, 1);
    box-shadow:         0px 0px 30px rgba(255, 255, 255, 1);
}

和HTML:

<div id="sidebar">
    <div id="sidebarTop">
        ....
    </div>
</div>

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

答案 2 :(得分:0)

#sidebarTop {
    margin: 0 auto;
}

答案 3 :(得分:0)

你可以简单地使用“center”标签:

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