Why magin:auto is not enough to center position absolute or fixed?

前端 未结 4 1138
迷失自我
迷失自我 2020-12-12 03:44

I am trying to center the contents of a div, my parent container is set to Relative.

I am using this on the inner div

    position: absolute;
    mar         


        
4条回答
  •  情话喂你
    2020-12-12 04:21

    The fix is absolute centering. You were almost there:

    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    

    You need to set all four directions to 0 and your child div will be perfectly centered within the parent.

提交回复
热议问题