注册 登录
编程论坛 WEB前端(UI)

div竟要这样水平居中和垂直居中?

静夜思 发布于 2004-12-29 00:36, 1198 次点击

一直想用纯CSS定位DIV的居中找了找,用ID定义的:

<html> <head><script language=javascript> <!-- function SetPos() { window.DivIndex.style.pixelLeft=(document.body.offsetWidth - window.DivIndex.style.pixelWidth)/2; //定义水平居中 window.DivIndex.style.pixelTop=(document.body.offsetHeight - window.DivIndex.style.pixelHeight)/2; //定义垂直居中 } --> </script> </head> <body onload="SetPos();"> <div id=DivIndex style="POSITION: absolute;">居中显示的内容</div> </body> </html>

是不是有些变态

[此贴子已经被作者于2004-12-29 00:45:10编辑过]

2 回复
#2
voro2005-01-05 14:56
不是有点,是超变态,你是怎么找到的这种方法啊 !?
#3
makebest2005-03-18 16:24
有一个方法,用 100% 宽、高的 DIV,嵌套 100% 宽、高的一行一列表格能实现,但是这样会造成浏览器中只看得见这个 DIV,其他都被盖住。
1