WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to center align absolute positioned div using CSS

Topic: HTML / CSSPrev|Next

Answer: Use the CSS margin, left & right property

You can align any absolutely or fixed positioned <div> element horizontally center using the CSS margin property in combination with the left and right position property.

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Center Alignment of Absolutely Positioned Elements</title>
<style>
    .box {
        width: 50%;
        position: absolute;
        z-index: 99;
        margin: 0 auto;
        left: 0;
        right: 0;
        padding: 20px;
        background: #f0e68c;
    }
</style>
</head>
<body>
    <div class="box">
        <h1>This is a heading</h1>
        <p>This is a paragraph.</p>
    </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