水平垂直居中

151 阅读1分钟

不需要知道元素宽高

flex

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

position + transform

xx {
    positon: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, 50%)
}

需要知道元素宽高

positon

xx {
    positon: absolute;
    height: 100px;
    width: 100px;
    left: 50%;
    top: 50%;
    margin-top: -50px;
    margin-left: -50px;
}
xx {
    positon: absolute;
    height: 100px;
    width: 100px;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}