I’m writing a webapp that has a sidebar. The webapp fits in a div that fills the entire screen (absolutely positioned, 100% height/width). The sidebar has a title and logo at the top (fixed height), and a div below that containing a list of items. I’d like to set this list of items to overflow: auto if they overrun the visible sidebar. I’ve tried using max-height: 100% for the sidebar, but this doesn’t seem invoke the scrollbars. How can I have the div fill the remaining vertical space of the sidebar, and display scrollbars if the content overruns the visible area?
+--------------------------------------------------------------------------+
| #app_pane |
| +------------------+ |
| | #sidebar | |
| | +--------------+ | |
| | | #logo | | |
| | | | | |
| | | height: 50px | | |
| | | | | |
| | +--------------+ | |
| | +--------------+ | |
| | | #list | | |
| | | | | |
| | | | | |
| | | <----+-+-----------o fill remaining height |
| | | | | and display scrollbars if necessary |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | +--------------+ | |
| +------------------+ |
+--------------------------------------------------------------------------+
edit:
Well it looks like I can do it in CSS3 with calc(), but I’d prefer a solution that legacy browsers support.
You can achieve this layout using the flexible box layout properties, but these are only currently supported in Firefox (since version 1), Safari (since version 2 or 3) and Chrome (since version 1).
If you set
#sidebartodisplay: -webkit-box, then you can use the-webkit-box-flexproperty on#listto make it take up all the space not used by the other#sidebarchildren. If you then applyoverflow-y:scrollto#list, it’ll get a scroll bar if its contents don’t fit inside it.Here’s some info on the flexible box properties:
I don’t know of any equivalent properties for Internet Explorer and Opera, although IE 9 does support
calc().