css3中居中的主流做法,css中居中

1、绝对定位 + translate

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .parent1 {
    
    
      position: relative;
      width: 200px;
      height: 200px;
      background-color: antiquewhite;
    }
    .son1 {
    
    
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      width: 20px;
      height: 20px;
      background-color: aqua;
    }
  </style>
</head>
<body>
  <div class="parent1">
    <div class="son1"></div>
  </div>
</body>
</html>

2、绝对定位和负边距

 .parent1 {
    
    
      position: relative;
      width: 200px;
      height: 200px;
      background-color: antiquewhite;
    }
    .son1 {
    
    
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left:-10px;
      margin-top:-10px;
      width: 20px;
      height: 20px;
      background-color: aqua;
    }

3、display:flex

.box2{
    
    

    display: flex;

    justify-content:center;

    align-items:Center;

}

猜你喜欢

转载自blog.csdn.net/weixin_43131046/article/details/123036166
今日推荐