使div填充页面的其余部分(跨浏览器) [英] Make a div fill the rest of the page (cross-browser)

查看:86
本文介绍了使div填充页面的其余部分(跨浏览器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在一些固定文本下方创建一个div,并且希望div恰好填充页面的高度,并且希望它跨浏览器工作...很难相信这样的工作量自然任务需要.

I just want to make a div below some fixed texts, and I want the div to fill exactly the height of the page, and I want it to work cross-browser... It is hard to believe how much work such a nature task requires.

我尝试了此代码,该代码通过jQuery调整了高度.它在Chrome浏览器中效果很好,但在Chrome浏览器中效果不佳:如果向下滚动,我们会看到它不会自动恢复到初始位置.

I tried this code, which adjusts the height by jQuery. It works well in Chrome, but it does not work perfectly in Chrome: if we scroll down, we could see it does not automatically restore to the initial position.

<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <style>
    body {
      margin: 0
    }
    .rb .myME {
      background: grey
    }
  </style>
</head>
<body>
  <div class="rb">
    <div class="top">1<br/>2<br/>3<br/>4<br/></div>
    <div class="myME">abc</div>
  </div>
  <script>
    $(".myME").css({
      height: (($(document).height()) - $(".top").height()) + 'px'
    })
  </script>
</body>
</html>

任何人都有跨浏览器的完美解决方案(CSS或jQuery)吗?

Does anyone have a perfect solution (CSS or jQuery) cross-browser?

推荐答案

对于较新和较旧的浏览器,display:table属性可以满足您的要求.

for older and newer browsers , the display:table properties could match your requirement.

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

.rb {
  display: table;
  width: 100%;
  border-collapse: collapse;
}

.top, .myME {
  display: table-row;
}

.buffer {
  display: table-cell;
}

.top .buffer {
  background: lightblue;
}

.myME .buffer {
  background: tomato;
  height:100%;
}

<div class="rb">
  <div class="top">
    <div class="buffer">
      1<br/>2<br/>3<br/>4<br/>
    </div>
  </div>
  <div class="myME">
    <div class="buffer">
      abc
    </div>
  </div>
</div>

.buffer div是为了确保您的每一行都由单个单元格组成,以避免布局也分成几列.

the .buffer div is to make sure that each of your rows are made of a single cell to avoid layout to be split also in columns.

这篇关于使div填充页面的其余部分(跨浏览器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆