tags:

views:

1575

answers:

5

Hello

If I have a html form like the following:

<form name="statusForm" action="post.php" method="post" enctype="multipart/form-data">
Test:
<input name="checkboxes[]" value="Test" type="checkbox">
<br>
TestTestTest:
<input name="checkboxes[]" value="Test" type="checkbox">
<br>
TestTestTestTestTestTest:
<input name="checkboxes[]" value="Test" type="checkbox">
<br>
TestTestTestTestTestTestTestTestTestTestTest:       
<input name="checkboxes[]" value="Test" type="checkbox">
<br>
<input name="Submit" value="submit" type="submit">
</form>

Is it possible to align the checkboxes so they are in union, without using a table or css but pure html? Otherwise, which css should be used?

+3  A: 

Yup. Surround each label with a <label> tag:

<label for="checkboxes1">Test:</label>
<input id="checkboxes1" name="checkboxes[]" value="Test" type="checkbox">

Then give the label a width:

label {
    display: inline-block; /* try "block" instead if this fails in IE */
    min-width: 5em;
}

That should pad out the text boxes nicely. As an added bonus, clicking on the label should now place the browser focus into the textbox.

Samir Talwar
Didn't he say "without CSS"?
cletus
This does use the minimal amount of CSS, (or any other form of markup). It also makes semantic sense. ++Samir
Diodeus
Label elements are inline, so the width property does not apply to them (unless you make them display: block, float them or similar)
David Dorward
Yes, to be strict, this approach requires display: inline-block.
Jan Zich
Doing this sort of thing without CSS is foolish. Why would you want to? The point of CSS is to style web pages. The point of HTML is to markup content. Don't use one in place of another.
Samir Talwar
Added that to the CSS. Thanks, Jan.
Samir Talwar
@Samir you're certainly right about your approaching being the right way, but if the question is "How do I do this without X" and the answer is not "use minimal X."
jskulski
A: 

I don't see why you want to do that.

It doesn't meet your no css instruction, but you could use inline styles if you really just want no external css.

Perhaps you could use &nbsp;

CSS (and to a lesser extent- tables) are tools you are looking for.

Edit: Another way you could do this is with ghost pixel images. images that are a 1x1 alpha transparent png and you use the height and width attributes in to tell it how much you want to space. You'd might need some inline css to make sure things clear correctly.

jskulski
Non-breaking spaces (as opposed to non-spacing breaks) are not designed for layout. Although it is theoretically possible to use them in a hacky way for layout (assuming you can guarentee the fonts being used, which you can't).
David Dorward
I was wondering why it didn't render :) You're right that it's not a good way to go and certainly not what they are designed for, however the question was "How do I do layout without using the tools for layout". Beats me why, but that is what he asking for and I think you could get pretty far with the right font set and nbsp's.
jskulski
A: 

The simplest way would simply be to align them all to the right. I'm not sure if the "align" attribute works on the form element but you could try that, or wrap your code in a div or p element with align="right").

CSS is a better solution. Put a class on the form then use the CSS rule text-align: right; or simply add style="text-align: right" to the form element directly.

DisgruntledGoat
A: 

The article Applying CSS to forms has some examples of syling labels to cause inputs to the right to line up along a vertical edge.

That said, it is a convention in user interface design to place labels to the right or radio buttons and checkboxes. If you follow that convention, then they will line up by themselves (since all the checkboxes will share a width).

David Dorward
+1  A: 

You could just put your labels and inputs in an unorderded list. In order to get the alignment, the text would have to go on the right of the input/

<ul>
    <li>
        <label><input /> Some Text</label>
    </li>
</ul>

or

<ul>
    <li>
        <input /><label for="">Some Text</label>
    </li>
</ul>

Rich

kim3er