Vertically center text in a 100% height div?

后端 未结 13 684
傲寒
傲寒 2020-12-05 17:22

I am working with a div that is 100% of the parent divs height.

The div only contains a single line of text.

The div cannot have a fixed height.

So m

13条回答
  •  青春惊慌失措
    2020-12-05 17:39

    Even though this question is pretty old, here's a solution that works with both single and multiple lines that need to be centered vertically (could easily be centered both vertically & horizontally as seen in the css in the Demo.

    HTML

    Text that needs to be vertically centered


    CSS

    .parent {
        position: relative;
        height: 400px;
    }
    
    .child {
        position: absolute;
        top: 50%;
        -webkit-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        transform: translateY(-50%);
    }
    

提交回复
热议问题