poorvank
poorvank

Reputation: 7602

How to align form at the center of the page in html/css

I am new to html/css. i am trying to build up my own form and i am trying to align it in the center. I used the align property in css but its not working.

Html code:

<!DOCTYPE HTML>
<head>
    <title>Web Page Having A Form</title>
    <link rel = "stylesheet" type="text/css" href="Form.css">
</head>
<body>
    <h1>Create a New Account</h1>
      <form >
       <table>
        <tr>
         <td>Name :</td> <td><input type="text" name="name"></td><br>
        </tr>
        <tr>
         <td>Email :</td> <td><input type="text" name="email"></td><br>
        </tr>
        <tr>
         <td>Password :</td> <td><input type="password" name="pwd"></td><br>
        </tr>
        <tr>
         <td>Confirm Password :</td> <td><input type="password" name="cpwd"><br>
        </tr>
        <tr>
         <td><input type="submit" value="Submit"></td>
        </tr>
       </table>
      </form>
</body>

Css Code:

    body
{
    background-color : #484848;
}
h1
{
    color : #000000;
    text-align : center;
    font-family: "SIMPSON";
}
form
{
    align:"center";
}

How should i change my code to align the entire form in the center?

Upvotes: 15

Views: 221264

Answers (6)

Hai Dinh
Hai Dinh

Reputation: 1513

  1. Wrap the element inside a div container as a row like your form here or something like that.
  2. Set css attribute:
    • width: 30%; (or anything you want)
    • margin: auto; Please take a look on following picture for more detail.enter image description here

Upvotes: 1

Lucky
Lucky

Reputation: 17345

Wrap the <form> element inside a div container and apply css to the div instead which makes things easier.

#aDiv{width: 300px; height: 300px; margin: 0 auto;}
<html>

<head></head>

<body>
  <div>
    <form>
      <div id="aDiv">
        <table>
          <tr>
            <td>Name :</td>
            <td>
              <input type="text" name="name">
            </td>
            <br>
          </tr>
          <tr>
            <td>Email :</td>
            <td>
              <input type="text" name="email">
            </td>
            <br>
          </tr>
          <tr>
            <td>Password :</td>
            <td>
              <input type="password" name="pwd">
            </td>
            <br>
          </tr>
          <tr>
            <td>Confirm Password :</td>
            <td>
              <input type="password" name="cpwd">
              <br>
          </tr>
          <tr>
            <td>
              <input type="submit" value="Submit">
            </td>
          </tr>
        </table>
      </div>
    </form>
  </div>
</body>

</html>

Upvotes: 3

Puvipavan
Puvipavan

Reputation: 298

This is my answer. I'm not a Pro. I hope this answer may help you :3

<div align="center">
<form>
.
.
Your elements
.
.
</form>
</div>

Upvotes: 8

ashish0810
ashish0810

Reputation: 11

Or you can just use the <center></center> tags.

Upvotes: 1

Ruddy
Ruddy

Reputation: 9923

I would just use table and not the form. Its done by using margin.

table {
  margin: 0 auto;
}

also try using something like

table td {
    padding-bottom: 5px;
}

instead of <br />

and also your input should end with /> e.g:

<input type="password" name="cpwd" />

Upvotes: 2

Falguni Panchal
Falguni Panchal

Reputation: 8981

Like this

demo

css

    body {
    background-color : #484848;
    margin: 0;
    padding: 0;
}
h1 {
    color : #000000;
    text-align : center;
    font-family: "SIMPSON";
}
form {
    width: 300px;
    margin: 0 auto;
}

Upvotes: 26

Related Questions