How to Vertically Center a <div>

Building a vertically and horizontally centered, fixed-width, flexible height content-box is the best solution for vertically centering a div for all browsers. It will work for all newest versions of Firefox, Chrome, Opera, and Safari.

Let’s try to do it together.

Create HTML

  • Set three HTML <div> tags and give them "external", "middle" and "internal" class names.
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <div class="external">
      <div class="middle">
        <div class="internal">
          <h1>
            How to Vertically Center the HTML tag 
          </h1>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod 
            tempor incididunt ut labore et dolore magna aliqua.
          </p>
        </div>
      </div>
    </div>
  </body>
</html>

Create CSS

Let’s style the first <div>

  • Use the CSS display property with its “table” property, so that the element can behave like HTML <table> element.
  • Use the CSS position property to set the first <div> element’s position. Add the “absolute” value.
  • Set the CSS top and left properties to define the element’s top and left positions.
  • With the help of the CSS width and height properties put the sizes of your <div> element.
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .external {
        display: table;
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div class="external">
      <div class="middle">
        <div class="internal">
          <h1>
            How to Vertically Center the HTML tag 
          </h1>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
          </p>
        </div>
      </div>
    </div>
  </body>
</html>

Start styling the second <div>.

  • Here we use the “table-cell” value of the display property, due to which the table will behave like HTML <td> element.
  • Put the “middle” value of the CSS vertical-align property. It will align the vertical the box’s vertical center with the baseline of the parent box plus half the x-height of the parent.
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .middle {
        display: table-cell;
        vertical-align: middle;
      }
    </style>
  </head>
  <body>
    <div class="external">
      <div class="middle">
        <div class="internal">
          <h1>
            How to Vertically Center the HTML tag 
          </h1>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
          </p>
        </div>
      </div>
    </div>
  </body>
</html>

And finally, style the third <div>.

Use the default “auto” value of the CSS margin-left and margin-right properties to set the left and right margins of the <div> element.

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .internal {
        margin-left: auto;
        margin-right: auto;
        width: 400px;
      }
    </style>
  </head>
  <body>
    <div class="external">
      <div class="middle">
        <div class="internal">
          <h1>
            How to Vertically Center the HTML tag 
          </h1>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
          </p>
        </div>
      </div>
    </div>
  </body>
</html>

Now let’s see the result.

Example of a vertically centered <div> element:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .external {
        display: table;
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
      }
      .middle {
        display: table-cell;
        vertical-align: middle;
      }
      .internal {
        margin-left: auto;
        margin-right: auto;
        width: 400px;
      }
    </style>
  </head>
  <body>
    <div class="external">
      <div class="middle">
        <div class="internal">
          <h1>
            How to Vertically Center the HTML tag 
          </h1>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
          </p>
        </div>
      </div>
    </div>
  </body>
</html>

Result

How to Vertically Center the HTML tag

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.