Emanuel Olsson
Emanuel Olsson

Reputation: 193

Trouble aligning contact form

I have been trying to edit this easy form to just look good for 3 hours now and Im still not quite there. I want the input fields to be on the same row as the labels naturally, but somehow the inputs are a bit lower than the labels and I cant seem to edit them with margins. What am I doing wrong?

Heres a snaggy picture of what the form looks like:

http://snag.gy/oPRNy.jpg

// CONTACT FORM

<label for="name"><p>Name:</p></label>
<input type="text" name="name" id="name" tabindex="1" />
<br/>
<label for="email"><p>Email:</p></label>
<input type="text" name="email" id="email" tabindex="2" />
<br/>
<label for="subject"><p>Subject:</p></label>
<input type="text" name="subject" id="subject" tabindex="3" />
<br/>
<label for="comments"><p>Comments:</p></label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="4"></textarea>
<br/>
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" tabindex="5" />
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Clear" tabindex="6" />

// CSS

    label {
    float: none;
    font-size: 100%;
    width: 250px; /* just this width evens out input box placement */
    font-weight: bold;
}
input { /*I think these just fall in because they are naturally following the labels!*/
    width: 250px;
    padding:5px;
    margin-top: -10px;
}
textarea {
    width: 250px;
    height: 150px;
    resize:none;
}
guestbook {
    margin-top:50px;
    text-align:center;
    font-size:26px;
    color:#05924b;
    font-family:Gisha;

}
gb p {
    color:#05924b;
    font-family:Gisha;
    text-align:left;
    margin-left:85px;
    margin-top:0px;
    margin-bottom:0px;
}

// SOLUTION: Removed the "p" paragraph from within the form and adjusted the rest from CSS, added float:left to the different inputs fields and rows, lowered the label width so the input came closer, then calculated and put the correct margin-right to both input{} and textarea{} and last to #submit to get everything in nice order. Heres a screenshot from the new code: http://snag.gy/PwpbQ.jpg

// CSS

    /* Input */
label {
    float: left;
    font-size: 100%;
    width: 50px; /* just this width evens out input box placement */
    font-weight: bold;
    margin: 2px 0;
    padding:5px;
    font-family: Gisha;
    font-style: normal;
    font-variant: normal;
    text-decoration: none;
    text-align: left;
}
input { /*I think these just fall in because they are naturally following the labels!*/
    width: 300px;
    padding:5px;
    margin: 5px 0;
    font-size:24px;
    margin-right:192px;
}
textarea {
    width: 300px;
    height: 150px;
    resize:none;
    margin:5px 0;
    padding:5px;
    margin-right:192px;
}
#submit {
    margin-right:225px; 
}
/* End of input */

Upvotes: 0

Views: 3097

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94489

Remove all of the <p> tags from the labels. A <p> tag is a block level element, therefore it should not be nested within the inline element <label>. Block level elements also clear, meaning they do not allow content on either side (unless floated). I believe this is causing your issue.

<label for="name">Name:</label>
<input type="text" name="name" id="name" tabindex="1" />
<br/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" tabindex="2" />
<br/>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" tabindex="3" />
<br/>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="4"></textarea>
<br/>
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" tabindex="5" />
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Clear" tabindex="6" />

Once the markup has been adjusted the label and input tags have no vertical spacing. To add vertical spacing you can add a margin to both elements.

label {
    font-size: 100%;
    width: 250px;
    font-weight: bold;
    margin: 2px 0;
}

input {
    width: 250px;
    padding:5px;
    margin: 2px 0;
}

Example: http://jsfiddle.net/KZrXD/

Upvotes: 1

Related Questions