CSS图上居中很好实现,但万恶的浏览器之间各不相让,搞的不兼容,还好我们有让其兼容的办法,那就是结合JS来实现,这样各个浏览器都听话多了。本例就是CSS结合JavaScript实现的图片垂直、水平方向都居中,也不是上下左右居中,位于网页的正中央,代码如下:

<body onresize="myLoad()" onload="myLoad()">
<div /></div>
<SCRIPT language="javascript" type=text/javascript>
function myLoad()
{
document.getElementById('xuanma').style.top= document.body.clientHeight/2-document.getElementById('ximage').height/2;
document.getElementById('xuanma').style.left= document.body.clientWidth/2-document.getElementById('ximage').width/2;
}
</SCRIPT>
</body>

复制代码保存成HTML页面,双击打开可测试效果。

相关文章:

  • 2021-12-05
  • 2021-08-29
  • 2022-01-16
  • 2021-12-14
  • 2021-06-25
  • 2021-07-13
  • 2021-08-26
猜你喜欢
  • 2021-11-27
  • 2022-02-10
  • 2022-12-23
  • 2021-11-27
  • 2021-10-12
  • 2021-11-07
  • 2021-05-28
相关资源
相似解决方案