Put simply the problem is thus:
If the first form element uses 90% of page width why doesn’t the second row of elements, using 35% and 55%, use the same amount of the pages width?
I find I have to tweak the % up by differing amounts depending on the elements/page composition.
More confusingly as browser width changes (narrows) the columns eventually align and going smaller still the problem becomes inverted with the first row narrower than the second.
I’ve tried to search but my problem gets mixed up with people wanting their form elements on the right. So it’s my turn for a bad SO question. 🙂 I simply want my form elements to be neat, the right hand edge of each element to be in line with the others.
A simple example is the following:
<html>
<head>
<title>Form</title>
<style text="text/css">
<!--
html, body {
margin:0; padding:0;
}
form {
display: block;
}
.inputField {
width: 90%;
}
.labelField {
width: 35%;
}
#secondField {
width: 55%;
}
-->
</style>
</head>
<body>
<form>
<legend>This is the form</legend>
<input class="inputField" id="firstField" type="tel" name="cardNo" /><br />
<label class="labelField" for="secondField">Please enter your name:</label>
<input class="inputField" id="secondField" type="tel" name="cv2" /><br />
<input class="inputSubmit" id="submit" type="submit" /><br />
</form>
</body>
</html>
I have tried using dl/dt/dd and horrible table/tr/td and even ul/li type forms. All suffer similar issues. Some browsers, such as firefox are only slightly out, but chrome & iOS highlight the problem well. Ideally a solution that specifically works in android and ios browsers would be good.
The actual form I’m dealing with has the following layout:
Select Input
Text Input
Select Input | Select Input
Label | Text Input
Submit | Label | Checkbox
Well, not to delve too deeply, but your problem really seems to be rooted in how you are structuring your form in general. Creating a form style layout is pretty straightforward once you have it a bit planned out. Remember, browsers like FF and IE render things like padding and such slightly differently. Furthermore, a lot of elements tend to have some built in padding, margins, etc… that can cause havoc with your page, especially when using percentages. In fact, if you use the code below, you will notice that a little bit of extra padding went into positioning the button, to make up for the difference caused by the other elements.
Here is the CSS:
And here is the HTML:
Now, for my part, I tend to not use the “br” tag very much, and try to do a more uniform layout. However, if you really need to use it, or you have various elements in drastically different positions on the page, then be prepared to do some style work.