怎样让DIV定位在页面中间?就像
这样的定位?

nhgayukai 2002-07-14 05:25:03
怎样让DIV定位在页面中间?就像<center>这样的定位?
...全文
1188 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2002-07-14
  • 打赏
  • 举报
回复
<HTML>
<script>
function divcenter(){
document.all.mxh.style.left=(Math.round((document.body.scrollWidth-200)/2)).toString()+"px"
}
</script>
<body onload="divcenter()" onresize=divcenter()>
<table width=100%>
<tr>
<td>test
</td>
</tr>
</table>
<div id=mxh style="position:absolute;left:200px;top:30px;width:200px;height:200px;background-color:orange;border:2px">不随窗口变化,永远居中的层</div>
</HTML>

孟子E章 2002-07-14
  • 打赏
  • 举报
回复
<HTML>
<script>
function divcenter(){
document.all.mxh.style.left=(Math.round((document.body.scrollWidth-200)/2)).toString()+"px"
}
</script>
<body onload="divcenter()" onresize=divcenter()>
<table width=100%>
<tr>
<td>test
</td>
</tr>
</table>
<div id=mxh style="position:absolute;left:200px;top:30px;width:200px;height:200px;background-color:navy;border:2px">测试文字</div>
</HTML>

dugucan 2002-07-14
  • 打赏
  • 举报
回复
对,就这样
llrock 2002-07-14
  • 打赏
  • 举报
回复
<div id=maindiv style="position:absolute;width:300;height:200;background:red"></div>
<script>
<!--
maindiv.style.left=(document.body.clientWidth-maindiv.clientWidth)/2;
maindiv.style.top=(document.body.clientHeight-maindiv.clientHeight)/2
//-->
</script>
希默软件 2002-07-14
  • 打赏
  • 举报
回复
用相对定位

<div id=maindiv style="position:absolute;width:300;height:200;background:red"></div>
<script>
<!--
function pos(){
document.all.maindiv.style.left=(parseInt(document.body.offsetWidth)-parseInt(document.all.maindiv.style.width))/2;
document.all.maindiv.style.top=(parseInt(document.body.offsetHeight)-parseInt(document.all.maindiv.style.height))/2;
}
pos();
//-->
</script>
dylanOK 2002-07-14
  • 打赏
  • 举报
回复
<div align=center>hello</div>
or use span:
<span align=center></span>

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧