0

I want to make any elements inside of div to be centered vertically and horizontally.

Here's my HTML :

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div class="login">
    <form action="prosesLogin.php" method="post">
        <h1>Login</h1>
        <table>
            <tbody>
                <tr><td>Username</td><td><input type="text" name="username"></td></tr>
                <tr><td>Password</td><td><input type="password" name="password"></td></tr>
                <tr><td colspan="2" align="right"><input type="submit" value="Login"> <input type="reset" value="Batal"></td></tr>
                <tr><td colspan="2" align="center">Belum memiliki akun ? <a href="register.php"><b>Daftar</b></a></td></tr>
            </tbody>
        </table>
    </form>
</div>
</body>
</html>

and this is my CSS :

.login{
   height: auto;
   margin: 0 auto;
   border:1px solid blue;
}

But it doesn't work, please tell how to make it. Thank you.

Johannes
  • 64,305
  • 18
  • 73
  • 130
brown26
  • 89
  • 1
  • 3
  • 10

3 Answers3

1

You can achieve this by using a flexbox display in your CSS (docs: https://www.w3schools.com/css/css3_flexbox.asp):

.login{
   height: auto;
   margin: 0 auto;
   border:1px solid blue;
   display: flex;
   align-items: center;
   justify-content: center;
   flex-direction: column;
   padding-top: 10px;
   padding-bottom: 10px;
}

.login h1 {
   margin-top: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div class="login">
    <form action="prosesLogin.php" method="post">
        <h1>Login</h1>
        <table>
            <tbody>
                <tr><td>Username</td><td><input type="text" name="username"></td></tr>
                <tr><td>Password</td><td><input type="password" name="password"></td></tr>
                <tr><td colspan="2" align="right"><input type="submit" value="Login"> <input type="reset" value="Batal"></td></tr>
                <tr><td colspan="2" align="center">Belum memiliki akun ? <a href="register.php"><b>Daftar</b></a></td></tr>
            </tbody>
        </table>
    </form>
</div>
</body>
</html>
Larpee
  • 800
  • 1
  • 7
  • 18
0
<div id="parent">
    <div id="child">body</div>
</div>

<style>
#parent {display: table;}
#child {
    display: table-cell;
    vertical-align: middle;
}
</style>

Another variants Vertical alignment of elements in a div

Community
  • 1
  • 1
Oleg Borodko
  • 106
  • 1
  • 10
0

The Internet already has lots of answer to this question...

Given that, if needed, you can handle the table as a normal block container by just using the display:block css property.

Community
  • 1
  • 1
Shalen
  • 272
  • 3
  • 14