【问题标题】:Fixed size div which sits to the right of a main div that fills up remainder of screen固定大小的 div,它位于填充屏幕其余部分的主 div 的右侧
【发布时间】:2011-01-19 18:57:37
【问题描述】:

我看过很多问题,但似乎没有一个能完全解决我的问题。

我有两个 div:mainbody 和 rightpanel。我希望右面板是固定大小(210px),并位于主体 div 的右侧,它填满了窗口的所有剩余空间。

所以它会像这样调整大小:

+----------------------------------------+
|                             |          |
|                             |          |
|                             |          |
|       mainbody              |rightpanel|
|                             |          |
|                             |          |
|                             |          |
+----------------------------------------+

+-------------------------------+
|                    |          |
|                    |          |
|                    |          |
|  mainbody          |rightpanel|
|                    |          |
|                    |          |
|                    |          |
+-------------------------------+

我当前的解决方案如下,但如果将窗口调整到大约 900 像素以下,则右侧栏会被推到底部,因为 25% 的剩余空间太小而无法容纳,所以我强制窗口以此宽度水平滚动。

#wrap {
    width:97%;
    margin:0 auto;
}

#mainbody {
    float:left;
    width: 75%;
    margin-right: 10px;
    margin-left: 10px;
}

#rightpanel  {
    float:right;
    width: 22%;
    min-width: 210px;
    max-width: 210px;
}

【问题讨论】:

  • @sunn0 这正是我一直在寻找的,它似乎应该可以工作,但是右侧面板仍然被推到主体下方,所以 -210px 位实际上是在推开当我使用这个布局时,屏幕在左边!

标签: css layout html


【解决方案1】:

如果你浮动它,你需要把右边的面板放在主要内容之前:

<div id="rightpanel">blah blah blah</div>
<div id="mainbody">blah blah blah
    blah blah blah
    blah blah blah
</div>

<style type="text/css">
    #rightpanel {
        width:210px;
        float:right;
        background-color:red;
    }
    #mainbody {
       margin-right:210px;
       background-color:blue;
    }
</style>

即使调整了页面大小,这也会使右侧面板保持正确对齐。

【讨论】:

    【解决方案2】:

    这应该可以工作

    <div id="container">
        <div id="left" style="background: #ff00ff; ">
            Left
            <div id="right" style="width: 210px; float: right; background: #ff0000;">
                Right
            </div>
        </div>
    </div>
    

    【讨论】:

    • 这个让左边填满窗口的整个宽度,右边被推到左边下面,坐在窗口的右边,但从页面底部开始!
    【解决方案3】:
    #mainbody {
    float: left;
    }
    #rightpanel {
    width: 210px;
    }
    

    你尝试过这样的事情吗?

    【讨论】:

    • 这使得主体填满页面的整个宽度,右侧面板位于页面的左侧,在主体后面,尽管它被拉伸以便内容被推到下面主体完成的地方。
    【解决方案4】:

    看看我的网站:http://www.mcohenlawyer.com/
    是你想要的吗?

    如果是,这是 CSS 代码:
    (所有主流浏览器都支持)

    div.menubar {
        position: fixed; /*can be absolute too*/
        width: 158px;
        /*what comes latter is not relevant for your question*/
        /*......*/
    }
    .body { /*the main body*/
        position: absolute;
        right: 180px;/*can be 159px, if you don't want space between divs*/
        min-height: 870px;
        /*what comes latter is not relevant for your question*/
        /*......*/
    }
    

    【讨论】:

    • 这正是我想要的,但您的 CSS 使右侧面板位于屏幕左侧,位于主体顶部上方。我开始觉得我的页面中一定有其他东西搞砸了,因为这些解决方案都不起作用,我认为它们应该是!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2017-12-15
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多