问: 怎样让DIV定位在页面中间?就像<center>这样的定位? ______________________________________________________________________________________________ 答1: <div align=center>hello</div> or use span: <span align=center></span> ______________________________________________________________________________________________ 答2: 用相对定位
<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> ______________________________________________________________________________________________ 答3: <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>
______________________________________________________________________________________________ 答4: 对,就这样 ______________________________________________________________________________________________ 答5: <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>
______________________________________________________________________________________________ 答6: <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>
(编辑:网站学习网)
|