• Post category:CSS

How to Center Horizontally using position: absolute

How to Center Horizontally in Position: absolute

In this article, we will see about how to center horizontally using position: absolute property.

To center absolute position content, we should set the following properties correctly:

  • margin-left: auto;
  • margin-right: auto;
  • left: 0;
  • right: 0;

Example code

<!DOCTYPE html>
<html>
<head>
</head>
<style type="text/css">
   div{
      background: red;
      height: 100px;
      width: 100px;
      position: absolute;
        margin-left: auto;
        margin-right: auto;
        left: 0;
        right: 0; }
</style>
<body>
<div></div>
</body>
</html>

Output:

position: absolute center horizontally

Leave a Reply