在父级中 上下左右居中方法

一个div在未知父级width和height时,在父级中“垂直水平居中”的css代码。

父级:body

子级:wrap

.wrap{
    width: 473px;
    height: 484px;
    background-color: red;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}

如果父级是其他的div,记得要加position: relative;

position: relative;

猜你喜欢

转载自blog.csdn.net/Gala_L/article/details/84327471