将div扩展到页面底部(仅限HTML和CSS)

woo*_*666 27 html css position

这是一个非常简单的问题,但我似乎无法在网上找到合适的解决方案.我希望在自然流中有一个元素列表,最后一个元素扩展到页面底部.所以,如果我有

<div id="top" style="height:50px;"></div>
<div id="bottom" style="background:lightgrey;"></div>
Run Code Online (Sandbox Code Playgroud)

我需要元素bottom从页面的底部延伸top到底部.任何只使用html和css的解决方案都是受欢迎的,你可以添加容器div或任何东西,只要我可以动态调整bottomdiv的大小

编辑:我不想硬编码任何值bottom,因为我想bottom调整是否top调整大小

这里有一个小提琴,满足您所有的需求:http://jsfiddle.net/8QnLA/

Dan*_*eld 53

解决方案:#1:css表:

html, body {
  height: 100%;
  width: 100%;
}
body {
  display: table;
  margin: 0;
}
#top, #bottom {
  width: 100%;
  background: yellow;
  display: table-row;
}
#top {
  height: 50px;
}
#bottom {
  background: lightgrey;
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

html, body {
  height: 100%;
  width: 100%;
}
body {
  display: table;
  margin: 0;
}
#top, #bottom {
  width: 100%;
  background: yellow;
  display: table-row;
}
#top {
  height: 50px;
}
#bottom {
  background: lightgrey;
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<div id="top" style="height:50px;"><span>A header</span></div>
<div id="bottom" style="background:lightgrey;"><span>The content area - extends to the bottom of the page</span></div>
Run Code Online (Sandbox Code Playgroud)

Codepen#1(小内容)

Codepen#2(很多内容)

解决方案#2:使用calc和视口单元

#top {
  height: 50px;
  background: yellow;
}
#bottom {
  background: lightgrey;
  min-height: calc(100vh - 50px);
}
Run Code Online (Sandbox Code Playgroud)

body {
  margin: 0;
}
#top {
  height: 50px;
  background: yellow;
}
#bottom {
  background: lightgrey;
  min-height: calc(100vh - 50px);
}

Where `min-height: calc(100vh - 50px);` means:

'Let the height of the content div be **at least** 100% of the viewport height minus the 50px height of the header.'
Run Code Online (Sandbox Code Playgroud)
<div id="top" style="height:50px;"><span>A header</span></div>
<div id="bottom" style="background:lightgrey;"><span>The content area - extends to the bottom of the page</span></div>
Run Code Online (Sandbox Code Playgroud)

Codepen#1(小内容)

Codepen#2(很多内容)

解决方案#3 - Flexbox

body {
  margin: 0;
  min-height: 100vh;
}
body {
  display: flex;
  flex-direction: column;
}
#top {
  height: 50px;
  background: yellow;
}
#bottom {
  background: lightgrey;
  flex: 1;
}
Run Code Online (Sandbox Code Playgroud)

body {
  margin: 0;
  min-height: 100vh;
}
body {
  display: flex;
  flex-direction: column;
}
#top {
  height: 50px;
  background: yellow;
}
#bottom {
  background: lightgrey;
  flex: 1;
}
Run Code Online (Sandbox Code Playgroud)
<div id="top" style="height:50px;"><span>A header</span></div>
<div id="bottom" style="background:lightgrey;"><span>The content area - extends to the bottom of the page</span></div>
Run Code Online (Sandbox Code Playgroud)

Codepen#1 - 内容很少

Codepen#2 - 很多内容

  • 哇谢谢你超越,但桌面方法对我来说是最好的! (2认同)