Horizontally Center a Div Using CSS: Flexbox approach

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.

Syntax:

parentDiv {
    display: flex;
    justify-content: center;
}
childDiv {
    // Width and height properties
}

Example: The code demonstrates how we can use the display property approach to get a div and event the content inside it to be horizontally centered in the div.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
          How to align div to the middle
        (horizontally/width) of the page?
    </title>
    <style>
        .container {
            display: flex;
            justify-content: center;
           
            /* The properties below are
             for better representation */
            height: 200px;
            width: 100%;
            margin: 0px 15px;
        }
 
        .innerDiv {
            background-color: black;
            width: 400px;
            height: 75px;
            color: azure;
            display: flex;
            justify-content: center;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <div class="innerDiv">
            <p>
                  This div is at the center,
                using display property
              </p>
        </div>
    </div>
</body>
   
</html>


Output:

Horizontally Center a Div Using CSS: Flexbox Method

Explanation:

We used the display: flex on the parent container (.container) to create a flexible layout. The justify-content: center property centers the child <div> horizontally within the parent and adjusts the .innerDiv dimensions and styling as needed.

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

...