Vertically center a div inside another div

前端 未结 5 447
梦谈多话
梦谈多话 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条回答
  •  -上瘾入骨i
    2020-12-17 18:43

    You can do it this way:

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

    Here a live view: https://jsfiddle.net/w9bpy1t4/

提交回复
热议问题