WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to Center a DIV Horizontally and Vertically Using Flexbox in CSS

Topic: HTML / CSSPrev|Next

Answer: Use CSS justify-content and align-items Property

You can simply use the CSS justify-content and the align-items properties with the value center to horizontally and vertically center a <div> using flexbox in CSS.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flexbox: Center Horizontally and Vertically</title>
<style>
    .flex-container {
        margin: 30px;
        min-height: 300px;	
        display: flex;
        border: 1px solid black;
        justify-content: center; /* align item horizontally */
        align-items: center; /* align item vertically */
    }
    .item {
        width: 100px;
        padding: 20px;
        background: #b4bac0;
    }
</style>
</head>
<body>
    <div class="flex-container">
        <div class="item">Centered Item</div>
    </div>
</body>
</html>

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties