Open In App

How to Center a Div using CSS ?

Centering an <div> element both vertically and horizontally can be tricky, but fear not! In this complete guide, we’ll explore various techniques to achieve perfect alignment. This center align div article, Includes using flexbox properties, positioning and translate, and the calc() function.

Approaches to Center a div

1. By Using Basic CSS Properties

In the below Example, we will learn to align a div in the center by CSS properties. We need to create a div with center alignment and include <h2> and <p> tags inside. Check the code below to understand better.

<!DOCTYPE html>
<html lang="en">
    <head>
        <style>
            body {
                width: auto;
                height: 100vh;
            }

            div {
                width: 50%;
                margin: auto;
                height: auto;
                background-color: rgba(
                    20,
                    220,
                    43,
                    0.267
                );
                padding: 1rem;
                border-radius: 0.5rem;
            }
        </style>
    </head>

    <body>
        <div>
            <h2 style="color: green">
                GeeksforGeeks
            </h2>
            <p>
                GeeksforGeeks is the
                most-recommended platform for all
                CS/IT Students.
            </p>
        </div>
    </body>
</html>

Output:

center-a-div-using-basic-css-properties

Center a div using CSS

2. Using Flexbox Properties

body {
display: flex;
align-items: center;
justify-content: center;

width: auto;
height: 100vh;
}

Example: Using CSS flexbox properties to align the <div> to the middle of the HTML page.

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body {
            width: auto;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        div {
            width: 50%;
            height: auto;
            background-color: rgba(20, 220, 43, 0.267);
            padding: 1rem;
            border-radius: 0.5rem;
        }
    </style>
</head>

<body>
    <div>
        <h2 style="color: green;">
            GeeksforGeeks
        </h2>
        
        <p>
            GeeksforGeeks is the most-recommended
            platform for all CS/IT Students.
        </p>
    </div>
</body>
</html>

Output:

Center a div using css flexbox

Center div using flexbox property

Explanation:

3. Using Position and Transform

div {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

Example: Let's use the above-discussed properties and align <div> horizontally and vertically center. 

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body {
            width: 100vw;
            height: 100vh;
            margin: 0;
        }

        div {
            width: 500px;
            height: 120px;
            position: relative;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(20, 220, 43, 0.267);
            padding: 1rem;
            border-radius: 0.5rem;
        }
    </style>
</head>

<body>
    <div>
        <h2 style="color: green;">
            GeeksforGeeks
        </h2>
        
        <p>
            GeeksforGeeks is the most-recommended
            platform for all CS/IT Students.
        </p>
    </div>
</body>
</html>

Output:

cener a div using translate

Center div using position relative

Explanation:

4. Center div using calc() function

div {
position: fixed;
top: calc(50% - 150px/2);
left: calc(50% - 150px/2);
}

Example: In this example, we are using the above-explained approach.

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body {
            width: 100vw;
            height: 100vh;
            margin: 0;
        }

        div {
            position: fixed;
            top: calc(50% - 150px/2);
            left: calc(50% - 150px/2);
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            text-align: center;
            padding: 20px;
            background: lightgreen;
        }
    </style>
</head>

<body>
    <div>
        <h2 style="color: green;">
            GeeksforGeeks
        </h2>

        <p>
            GeeksforGeeks is the most-recommended
            platform for all CS/IT Students.
        </p>
    </div>
</body>
</html>

Output:

Center div using calc() Function

Explanation:


Article Tags :