Q:

How to center align absolute positioned div using CSS

0

How to center align absolute positioned div using CSS

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the CSS marginleft & 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 Align Absolutely Positioned Elements with CSS</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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

HTML / CSS Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to apply border to an element on mouse hover w... >>
<< How to create drop caps effect using CSS...