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>
Use the CSS
margin,left&rightpropertyYou can align any absolutely or fixed positioned
<div>element horizontally center using the CSSmarginproperty in combination with theleftandrightposition property.Let's try out the following example to understand how it actually works:
need an explanation for this answer? contact us directly to get an explanation for this answer