首页 > 解决方案 > 有没有办法将 html 元素绝对定位但仍将其放置在页面的中心?

问题描述

 .container{
   position: absolute;
   margin: 0 auto;// this doesn't position it in the centre anymore.
 }
<div class="container">
   this is a container
</div>

“这应该放在中心......但是当我将它定位为绝对时,它又回到了网页的顶部。有没有办法绕过这个。”

标签: htmlcssdebugging

解决方案


这将做到:

 .container{
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%,-50%);
 }

推荐阅读