绝对布局怎么水平居中

不同屏幕尺寸下,绝对布局,这个问题困扰了我很久,先分享我摸索的方法

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .imgs{
      /* 相对于.imgs */
      position: relative;
    }
    .img{
      width: 100%;
    }
    img {
      width: 100%;
    }
    .bar span{
      display: inline-block;
      list-style: none;
      width: 2px;
      height: 2px;
      border-radius: 50%;
      border: 1px solid #555555;
      background: #555555;
    }
    .bar_outer {
      /* 绝对布局,相对于.imgs */
      position: absolute;
      bottom: 10px;
      width: 100%;
    }
    .bar{
      /* 关键代码!! */
      margin: auto;
      display: block;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="imgs">
    <div class="img">
      <img src="https://via.placeholder.com/360x360.png" />
    </div>
    <div class="bar_outer">
        <span class="bar">
          <span></span>
          <span></span>
          <span></span>
          <span></span>
        </span>
    </div>
  </div>
</body>
</html>

下面就是效果:底部的几个小圆点无论屏幕有多大都会居中显示了

绝对布局怎么水平居中

然后分享一篇文章:https://www.zhangxinxu.com/wordpress/2013/11/margin-auto-absolute-%E7%BB%9D%E5%AF%B9%E5%AE%9A%E4%BD%8D-%E6%B0%B4%E5%B9%B3%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD/