Does 'position: absolute' conflict with Flexbox?

后端 未结 4 387
长情又很酷
长情又很酷 2020-12-07 13:07

I want to make a div stick on the top of the screen without any influence to other elements, and its child element in the center.

4条回答
  •  离开以前
    2020-12-07 13:48

    You have to give width:100% to parent to center the text.

     .parent {
       display: flex;
       justify-content: center;
       position: absolute;
       width:100%
     }
    text

    If you also need to centre align vertically, give height:100% and align-itens: center

    .parent {
       display: flex;
       justify-content: center;
       align-items: center;
       position: absolute;
       width:100%;
       height: 100%;
     }
    

提交回复
热议问题