How to align absolutely positioned element to center?

后端 未结 7 1531
遇见更好的自我
遇见更好的自我 2020-12-22 17:51

I am trying to stack two canvas together and make it a double layers canvas.

I\'ve saw an example here:

&l
7条回答
  •  -上瘾入骨i
    2020-12-22 18:38

    If you want to center align an element without knowing it's width and height do:

    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    

    Example:

    *{
      margin:0;
      padding:0;
    }
    section{
      background:red;
      height: 100vh;
      width: 100vw;
    }
    div{  
      width: 80vw;
      height: 80vh;
      background: white;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    Popup

提交回复
热议问题