2

我有一个标题、一个侧边栏和一个内容 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

4 回答 4

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>​

演示


更新 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>​​​​​​​​​​

演示 2

于 2012-08-12T22:04:20.030 回答
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>
于 2012-08-12T22:18:09.033 回答
0
#sidebarTop {
    margin: 0 auto;
}
于 2012-08-12T22:21:32.877 回答
0

您可以像这样简单地使用“中心”标签:

<div id="content">
<center>
    TEST
</center>
</div>
于 2012-08-13T03:10:34.843 回答