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

前端 未结 4 1142
迷失自我
迷失自我 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条回答
  •  旧时难觅i
    2020-12-12 04:24

    You can use left and right properties when your elements has position absolute/relative You can center your inner element in both axises using just margin: auto; or on x-axis using margin: 0 auto;.

    .parent{
      width: 200px;
      height: 200px;
      background: red;
    }
    
    .child{
      margin: auto;
      width: 70px;
      height: 70px;
      background: green;
    }
    

提交回复
热议问题