简体   繁体   English

有可变大小的标题时,如何使div填充剩余高度?

[英]How do I make a div fill remaining height when there's a variable sized header?

Any way to get the red area to fill the rest of the viewable area but not extend into the footer like it is now? 有什么方法可以使红色区域填充可见区域的其余部分,但不能像现在这样扩展到页脚中? I also need the infoContent part to be scrollable. 我还需要infoContent部分可滚动。 The height of the header portion is variable. 标头部分的高度是可变的。

I found a bunch of couple year old answers which said to use JavaScript, but are there any techniques that can be used in modern browsers to avoid that? 我发现了很多使用JavaScript的答案,但是现代浏览器中可以使用任何技术来避免这种情况吗?

HTML: HTML:

<div id="page">
<aside id="infoBar">
        <header>Variable Sized Header</header>
        <div id="infoContent">
            <div>First Content Item</div>
            <div>Second Content Item</div>
            <div>Third Content Item</div>
        </div>
    </aside>
    <footer id="footer">Footer</footer>
</div>

CSS: CSS:

#footer { position:fixed; bottom: 0; height: 50px; background-color: rgba(0, 0, 255, 0.5); width: 100%;}

#infoBar { position: fixed; right:0; top: 0; bottom: 50px; padding: 5px; border: 1px solid black; width: 200px; }
#infoBar > header { height: 50px; }

#infoContent { height: 100%; background-color: red; overflow-y: auto; }
#infoContent > div { margin: 5px; height: 50px; background-color: yellow; }

Here's a fiddle to play around with: http://jsfiddle.net/gWmtD/ 这是一个玩弄的小提琴: http : //jsfiddle.net/gWmtD/

Using a table was the first thing that came to mind: http://jsfiddle.net/gWmtD/9/ 首先想到一个表: http : //jsfiddle.net/gWmtD/9/

I used inline CSS because it was easier for me to prototype with and you can easily see the changes I've made. 我使用内联CSS是因为它使我更容易进行原型设计,并且您可以轻松地看到所做的更改。

<div id="page">
    <aside id="infoBar" style="overflow-y: auto;">
        <table style="height:100%; width:100%;">
            <tr>
                <td>
                    <header>
                        Variable Sized Header
                    </header>
                </td>
            </tr>
            <tr>
                <td style="height:100%; width:100%;">
                    <div id="infoContent">
                        <div>First Content Item</div>
                        <div>Second Content Item</div>
                        <div>Third Content Item</div>
                    </div>
                </td>
            </tr>
        </table>
    </aside>
    <footer id="footer">Footer</footer>
</div>



Edit: To enable the scrollbar for the aside when you scrunch the page down in FireFox, add the following property: 编辑:在FireFox中向下压缩页面时,要在一边启用滚动条,请添加以下属性:

overflow-y: auto;

Which will make the y scrollbar appear only when it's needed. 这将使y滚动条仅在需要时显示。 This happens by default in Chrome, and can be turned off in Chrome by setting: 在Chrome中,默认情况下会发生这种情况,可以通过设置以下设置在Chrome中将其关闭:

overflow-y: none;

A quick google search reveals the overflow method for the scroll bar: http://www.w3schools.com/cssref/pr_pos_overflow.asp 谷歌快速搜索显示了滚动条的溢出方法: http : //www.w3schools.com/cssref/pr_pos_overflow.asp

You should look into css wrapper classes for your other problem as a start. 首先,您应该研究css包装器类中的其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM