CSS:如何均匀共享剩余的垂直空间?

Pti*_*val 0 css

我整个下午都在尝试实现一个相当简单的设计,首先使用 Flex,然后只使用旧的 div,但仍然卡住了。

HTML 布局相当简单。

<div id="interface">
  <div id="toolbar">
    Toolbar<br/>Toolbar
  </div>
  <div id="main">
    <div id="main-left">
      This should take the full vertical space remaining after the toolbar,
      and half the horizontal space.
    </div>
    <div id="main-right">
      <div id="context">
        This should take half and half the remaining space after the toolbar.
      </div>
      <div id="messages">
        This should take half and half the remaining space after the toolbar.
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

问题是,我希望我的布局具有:

  • #toolbar高度不固定的顶部

  • 屏幕的其余部分#main应垂直分割 50%

    • 在左边,整个高度应该被#main-left

    • 右侧部分#main-right应水平分割,两个部分 (#context和#messages) 占据工具栏后剩余垂直空间的 50%

下面的小提琴或多或少具有适当的元素,但大小错误。

预期结果不应包含黄色。红色区域应占据整个垂直空间。绿色和紫色应占据垂直空间的一半。

不应有顶级滚动条。如果任何红色、绿色、紫色 div 内容变得太大,它应该会在自身内部溢出。

请注意,将高度设置#context为#messages50% 是不正确的,因为它们变得太大(看起来它们变成 的高度的 50% #interface,而不是 的高度#main)

http://jsfiddle.net/59trW/63/

谢谢你的帮助!

我愿意切换到弹性盒,但也遇到了类似的问题。

Ori*_*iol 5

使用弹性盒,

html, body, #interface {
  height: 100%;
  margin: 0;
}
#interface, #main, #main-right {
  display: flex; /* Flex container */
}
#interface, #main-right {
  flex-direction: column; /* Column layout */
}
#main, #main-left, #main-right, #context, #messages {
  flex: 1; /* Distribute space equally among the flex items */
  overflow: auto; /* Use scrollbars if necessary */
}
#toolbar    { background-color: pink;   }
#main       { background-color: yellow; }
#main-left  { background-color: red;    }
#main-right { background-color: blue;   }
#context    { background-color: green;  }
#messages   { background-color: purple; }
Run Code Online (Sandbox Code Playgroud)
<div id="interface">
  <div id="toolbar">
    Toolbar<br/>Toolbar
  </div>
  <div id="main">
    <div id="main-left">
      This should take the full vertical space remaining after the toolbar,
      and half the horizontal space.
    </div>
    <div id="main-right">
      <div id="context">
        This should take half and half the remaining space after the toolbar.
      </div>
      <div id="messages">
        This should take half and half the remaining space after the toolbar.
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)