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

<div class="container">
    <div class="content">
        <div class="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: 0px 1px 7px 1px rgba(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"