1、 CSS 都必须是 absolute,不然没法给height
<style type="text/css">
#divLogin
{
height: 275px;
width: 400px;
position: absolute;
background-color:#f3f3f3;
}
#divFrame
{
margin:0;
padding:0;
position:absolute;
background-color:Silver;
}
</style>
2、js
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//当窗体被放大、缩小时执行的事件
$(window).resize(function () {
//给外框添加css
$('#divFrame').css({
position: 'absolute',
height: $(window).height()-10,
width:$(window).width()-10
});
//给内框添加css
$('#divLogin').css({
position: 'absolute',
left: ($(window).width() - $('#divLogin').width()) / 2,
top: ($(window).height() - $('#divLogin').height()) / 2
});
});
//初始时运行这个方法
$(window).resize();
});
</script>
3 、html
<div style="text-align: center; width: 100%;" id="divFrame" >
<div id="divLogin">
dddddddddddddddddddddddddddddddddd
</div>
</div>