yhb199

通常在制作网站首页时需要使页面内容在水平和垂直方向上都居中。
表格布局时代常用的方法是内容之前添加换行。
实际上通过CSS有更方便的办法。

<style>
#warp {
position: absolute;
width:500px;
height:200px;
left:50%;
top:50%;
margin-left:-250px;
margin-top:-100px;
border: solid 3px red;
}
</style>
<body>
<div id=warp>Test</div>
</body>
centered-div.png

原理很简单:将left和top设置为50%来定位div到浏览器中央,再将margin-left和margin-top值设置为宽和高的一半,使div居中显示。

分类:

技术点:

相关文章:

  • 2021-12-01
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-11-27
  • 2021-12-05
  • 2021-12-13
猜你喜欢
  • 2021-06-28
  • 2021-10-29
  • 2022-01-05
  • 2021-11-21
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案