Search code examples
htmlformsalignment

How to align input forms in HTML


I'm new to HTML and I'm trying to learn how to use forms.

The biggest issue I am having so far is aligning the forms. Here is an example of my current HTML file:

<form>
 First Name:<input type="text" name="first"><br />
 Last Name:<input type="text" name="last"><br />
 Email:<input type="text" name="email"><br />
</form>

The problem with this is, the field box after 'Email' is drastically different in terms of spacing compared to first, and last name. What is the 'proper' way to make it so that they 'line-up' essentially?

I am trying to practice good form and syntax...a lot of people might do this with CSS I am not sure, I have only learned the very basics of HTML so far.


Solution

  • Another example, this uses CSS, I simply put the form in a div with the container class. And specified that input elements contained within are to be 100% of the container width and not have any elements on either side.

    .container {
      width: 500px;
      clear: both;
    }
    
    .container input {
      width: 100%;
      clear: both;
    }
    <html>
    
    <head>
      <title>Example form</title>
    </head>
    
    <body>
      <div class="container">
        <form>
          <label>First Name</label>
          <input type="text" name="first"><br />
          <label>Last Name</label>
          <input type="text" name="last"><br />
          <label>Email</label>
          <input type="text" name="email"><br />
        </form>
      </div>
    </body>
    
    </html>