0

I need to vertically align my form in the middle of my first div. I have it centered horizontally but I can't get it to align in the middle vertically as well. Basically even distance from the top and bottom.

<div id="login" style="position:absolute; width: 300px; height: 100px; z-index: 15; top: 50%; left: 50%; margin: -50px 0 0 -150px; background-color: #d0e9fc; border: 1px solid #B5BCC7;">
  <div align="center" style="vertical-align: middle;">
    <form th:action="@{/login}" method="post">
      <table>
        <tr>
          <td>Username:</td>
          <td>
            <input type="text" name="username" id="username" />
          </td>
        </tr>
        <tr>
          <td>Password:</td>
          <td>
            <input type="password" name="password" id="password" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input class="sButton" type="submit" value="Sign in" label="Sign In" id="submitButton" />
          </td>
        </tr>
      </table>
    </form>
  </div>
</div>
Patrick Murphy
  • 2,311
  • 14
  • 17
Schwarz
  • 395
  • 4
  • 18

3 Answers3

2

Use this

#login {
    display: table;
}
#login > div {
    display: table-cell;
    vertical-align: middle;
}
Jacob
  • 2,212
  • 1
  • 12
  • 18
0

Try this I removed the align="center" part as it is depreciated.

I changed your css from using absolute positioning to just center the div using margins.

I changed the height from always being 100px, to the height of the form plus 10px on each top and bottom. Vertically centering it.

I alsp added the margin:auto centering to the table to center the table in side the form.

<div id="login" style="width: 300px; margin:auto; background-color: #d0e9fc; border: 1px solid #B5BCC7;">
  <div style="margin: auto; padding-top:10px; padding-bottom:10px;">
    <form th:action="@{/login}" method="post">
      <table style="margin:auto">
        <tr>
          <td>Username:</td>
          <td>
            <input type="text" name="username" id="username" />
          </td>
        </tr>
        <tr>
          <td>Password:</td>
          <td>
            <input type="password" name="password" id="password" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input class="sButton" type="submit" value="Sign in" label="Sign In" id="submitButton" />
          </td>
        </tr>
      </table>
    </form>
  </div>
</div>
Patrick Murphy
  • 2,311
  • 14
  • 17
0

I removed the inline styling and added a class with the following properties: http://codepen.io/anon/pen/WvMNZE

.c {
  width: 300px;
  height: 100px;
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
  background-color: #d0e9fc; border: 1px solid #B5BCC7;
}

body {
  margin: 0;
}
Spade
  • 591
  • 3
  • 20