absolute定位有一个很常用的用途,就是当希望子元素在父元素的正中央时,可以这样操作:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{
            width: 500px;
            height: 500px;
            background: blue;
            position: absolute;
            top: 100px;
            left: 100px;
        }
        .son{
            width: 100px;
            height: 100px;
            background: black;
            position: absolute;
            left: 0;  /* 注意不要漏了上下左右为0,否则不会成功 */
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="son"></div>
    </div>
</body>
</html>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2021-12-15
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
猜你喜欢
  • 2021-12-29
  • 2021-10-05
  • 2022-12-23
  • 2021-11-12
  • 2021-11-29
  • 2021-11-29
相关资源
相似解决方案