= = centered transform = =


   .wrap{           / / father
    position: relative;
    border: 1px solid red;
    width: 300px;
    height: 300px;
  }

  .child {			/ / child
    position: absolute;
    width: 100px;
    height: 100px;
    background-color: green;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
Copy the code

absolute +margin auto

		
		
.child {
    position: absolute;;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

Copy the code

absolute + calc

This approach also requires that the width and height of the center element be fixed


.wp {
    position: relative;
}
.box {
    position: absolute;;
    top: calc(50% - 50px);  // 50% for the top percentile and 50px for the half width of the sublevel
    left: calc(50% - 50px);

Copy the code

Absolte + negative margin


 
/* Location code */
.father {
    position: relative;
}
.son {
    position: absolute;;
    top: 50%;
    left: 50%;
    margin-left: -50px;
    margin-top: -50px;
}
 
 
Copy the code

lineheigth


.father {
    line-height: 300px;
    text-align: center;
    font-size: 0px;
}
.son {
    font-size: 16px;
    display: inline-block;
    vertical-align: middle;
    line-height: initial;
    text-align: left; /* Modify text */
}

Copy the code

flex


.father {
    display: flex;
    justify-content: center;
    align-items: center;
}

Copy the code

grid


.father {
    display: grid;
}
.son {
    align-self: center;
    justify-self: center;
}

Copy the code

= = = =

From www.jianshu.com/p/907f99004…