Skip to content Skip to sidebar Skip to footer

How To Vertically And Horizontally Center A Div Of Unknown Height

I tried using the flexbox method, table method, and some other methods for vertically centering a div of unknown height, but my div is not getting centered correctly. I want the wi

Solution 1:

Using a flexbox, here's all the code you need:

HTML

<divclass="container"><divclass="content"><divclass="title-class">Hello there</div></div></div>

CSS

html, body { height: 100%; }

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: violet;
    height: 100%;
}

.content {
    background-color: violet;
    width: 50%;
    text-align: center;
    box-shadow: 0px1px7px1pxrgba(0, 0, 0, 1);

}

DEMO

As an alternative, here's the table method:

Post a Comment for "How To Vertically And Horizontally Center A Div Of Unknown Height"