Vertically center text in a 100% height div?

后端 未结 13 669
傲寒
傲寒 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:48

    Since it is absolutely positioned you can use top: 50% to vertically align it in the center.

    But then you run into the issue of the page being bigger than you want it to be. For that you can use the overflow: hidden for the parent div. This is what I used to create the same effect:

    The CSS:

    div.parent {
        width: 100%;
        height: 300px;
        position: relative;
        overflow: hidden;
    }
    div.parent div.absolute {
        position: absolute;
        top: 50%;
        height: 300px;
    }
    

    The HTML:

    This is vertically center aligned

提交回复
热议问题