【问题标题】:Set div height to height of browser using CSS ONLY仅使用 CSS 将 div 高度设置为浏览器的高度
【发布时间】:2014-04-28 04:34:38
【问题描述】:

*我正在使用 CSS 制作布局。

我正在尝试让header div 占用一定数量的高度像素,例如 100px,然后content div 占用浏览器窗口的其余高度。

即浏览器窗口高度减去标题 div 的高度。

所以HTML如下,

<div id="header"></div>

<div id="content"></div>

....这是我目前得到的 CSS。

html, body {margin: 0; padding: 0; height: 100%;}

#header {width: 100%; height: 100px; background-color: #333;}

#content {position: relative; top: 10px; width: 100%; /*height: auto !important;*/  min-height: 100%; background-color: #999; }

我知道,因为我已经做到了,这可以通过 JS/Jquery 来根据浏览器的高度设置内容 div 的高度,但我很想知道是否有办法做到这一点只是使用CSS。

从下面的 Fiddle 可以看出,当前的 CSS 只是让内容 div 在底部从屏幕上射出。

http://jsfiddle.net/PysLY/

【问题讨论】:

  • 为什么将高度自动放入 cmets 内容中会占用整个剩余高度

标签: html css height


【解决方案1】:

我得到了这个结果:http://jsfiddle.net/ZC7BT/

html, body {margin: 0; padding: 0; height: 100%;}

#header {width: 100%; height: 100px; background-color: #333;}

#content {width: 100%; min-height: calc(100% - 100px); background-color: #999; overflow:auto; }

【讨论】:

  • 谢谢@rafaeldefazio。从来不知道css calc,但从你的小提琴的外观来看,就可以了!不错。
猜你喜欢
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2013-10-29
  • 1970-01-01
  • 2013-11-21
  • 2015-02-26
相关资源
最近更新 更多