Vertically center a div inside another div

前端 未结 5 459
梦谈多话
梦谈多话 2020-12-17 18:10

I\'m trying to vertical-align: middle a div inside another div, but for some reason it\'s not working properly. What am I doing wrong?

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 18:39

    Here is how I normally do this.

    #wrapper {
    border: 1px solid red;
    width: 500px;
    height: 500px;
    position: relative;
    }
    #block {
    border: 1px solid blue;
    width: 500px;
    height: 250px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    }

    Easy way to make it dynamic.

提交回复
热议问题