javascript - 如何设置固定的 div 高度 100%

标签 javascript jquery html css

我有3个

  • 标题
  • 内容
  • 页脚

他们都设置位置固定

如何自动设置(内容)高度为100%?

问题是内容部分的最后一个文本被页脚 div 隐藏了。

如何设置内容 div高度自动计算100%-页脚高度?

html,body { height:100%; }
  .wrapper { position:relative; width:100%; height:100%}
  .box1 { position:fixed; top:0;left:0; width:100%; height:30px; background:red}
  .box2 { position:fixed; top:30px;left:0; width:100%; height:100%; overflow-y:auto; background:gray}
  .box3 { position:fixed; bottom:0;left:0; width:100%; height:30px; background:blue}
<div class="wrapper">
  <div class="box1">head</div>
  <div class="box2">content>last text</div>
  <div class="box3">foot</div>
</div>

最佳答案

添加高度:calc(100% - 60px);到 box2 减去页眉和页脚的高度并将 overflow-y:hidden 设置为 html,body 这样你就可以在没有滚动的情况下获得固定的 div 100% 高度

html,body { height:100%; overflow-y:hidden; }
.wrapper { position:relative; width:100%; height:100%}

.box1 { position:fixed; top:0;left:0; width:100%; height:30px; background:red}
.box2 { position:fixed; top:30px;left:0; width:100%; height:calc(100% - 60px); overflow-y:auto; background:gray}
.box3 { position:fixed; bottom:0;left:0; width:100%; height:30px; background:blue}
<div class="wrapper">

<div class="box1">head</div>
<div class="box2">content<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>last text</div>
<div class="box3">foot</div>

</div>

关于javascript - 如何设置固定的 div 高度 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54068767/

相关文章:

javascript - 如何使用 json api auth 处理 angularJS 中的 session 状态

javascript - Javascript 可以知道用户是否选择了禁用警报的框(防止页面创建额外的对话框)

javascript - 如何用json数据动态构造html标签属性?

php - 如何修复mysql从till到日期和时间的读取?

javascript - jQuery 轮播/画廊无法正常工作

javascript - 如何在 AngularJS 中模拟构造函数(特别是 Date() 构造函数)

javascript - 值未保存在 orientdb 类中

javascript - for 循环增量,小数点停止在 1.42,而不是继续直到计算完成

javascript - 突出显示 HTML 页面中的重复单词

javascript - 无法在 WebView(网页)中以编程方式单击按钮