Center a Div Horizontally with Margin Auto CSS Styling

Here to make the div horizontally centered the margin in the x-axis needs to be auto which means the margin to the left and right of the div to be the same so put it in the center. Also if you want to make the div vertically centered then make the margins in the x and y axis auto and it will put the div in the center of the parent container.

Syntax:

div {
    margin: 0 auto;
    // Add your code
}

Example: The code demonstrates how we can use the margin auto technique to center the div horizontally.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
          How to align div to the middle
        (horizontally/width) of the page?
    </title>
    <style>
        .container {
            margin: 0 auto;
            background-color: darkolivegreen;
            width: 300px;
            height: 75px;
            color: azure;
        }
 
        p {
            /* The inner text of the
               div is centered */
            padding-top: 1.5rem;
            text-align: center;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <p>
            This div is at the center
              using Margin Auto
        </p>
    </div>
</body>
</html>


Output:

Center a Div Horizontally with Margin Auto

Explanation:

We utilized the browser capability by using the CSS margin property, we set the margin value to auto so the browser can adjust with the available space and place the div into the center of the viewport.

How to Align a Div to Middle (horizontally/width) of Page using CSS ?

We are going to see how we can align a div horizontally using CSS. Centering content both vertically and horizontally is one of the most important functions that needs to be done. Here horizontally align refers to the same amount of space to the left and right of the div inside the parent div. Three approaches can be used to achieve this layout and both of them are listed and demonstrated below.

But first, Let’s create a div to place the div into the middle (horizontally) we need to create a fixed-size div and give a background color so that it can be visible. Set the main container width to 100% so the div can be moved around into the container.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
          How to align div to the middle
        (horizontally/width) of the page?
    </title>
    <style>
        .container {
            height: 200px;
            width: 100%;
            margin: 0px 15px;
        }
 
        .innerDiv {
            background-color: black;
            width: 400px;
            height: 75px;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <div class="innerDiv">
            <p> Example to center div horizontally
              </p>
        </div>
    </div>
</body>
   
</html>


Now that we have seen the structure of the code, let’s understand the different approaches to center the div horizontally.

Table of Content

  • Horizontally Center a Div Using CSS: Flexbox approach
  • Center a Div Horizontally with Margin Auto CSS Styling
  • Horizontal Centering div using Position Property

Similar Reads

Horizontally Center a Div Using CSS: Flexbox approach

...

Center a Div Horizontally with Margin Auto CSS Styling

This approach involves the usage of one of the display properties of CSS which is flex. The parent div which will contain the child div which will be centered needs to have its display set to flex and then must have its content justified to center using the justify-content: center property....

Horizontal Centering div using Position Property

...