Position by center point, rather than top-left point

后端 未结 6 2096
鱼传尺愫
鱼传尺愫 2021-02-01 01:14

Is it possible to tell the code to position by the center point of an element, rather than by the top-left point? If my parent element has

width         


        
6条回答
  •  故里飘歌
    2021-02-01 01:25

    Here's one way to center a text element in the middle of the container (using a header as an example

    CSS:

    .header {
         text-align: center;
         top: 50%;
         left: 50%;
         transform: translate(-50%, -50%);
         position: absolute;
    }
    

    It centers by a middle anchor point.

提交回复
热议问题