垂直水平居中

<div class="parent">
  <div class="child"></div>
</div>

1

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

flex 教程

2

.parent {
    display: grid;
}
.child {
    justify-self: center;
    align-self: center;
}

grid 教程

3

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

Last updated