How do I put a label to the left of a select box to indicate its purpose?

4 answers

PaulBM 48
3
points
<form>
<fieldset>
<legend>Name<legend>
<ul>
<li>
<label for='fName'>Forename</label>
<input type='text' id='fName' />
</li>
<li>
<label for='sName'>Surname</label>
<input type='text' id='sName' />
</li>
</ul>
</fieldset>
</form>

is roughly how I would markup a label for a textbox.

I usually style labels with text-align:right and a suitable margin to align the label close to the relevant input field.

Answered 11 months ago by PaulBM
2
points

According to W3C you need to use a label element and associate it with the select element. The above answers only mention associating with a input element. But you can do this with select element as well.

Answered 11 months ago by Divya Manian
1
point

Just write your text before the input tag e.g.:

your text here <input type="checkbox" id="chk" name="chk" checked/>
Answered 11 months ago by Ammaroff
  • when i did that it showed up at the top of the screen. now how do i move it the left of it? canyonchase1 11 months ago
1
point

You should use the <label> element and then you can float it left. However, the convention is to have the label on the right side, in which case you would float the <input> element left instead. Also notice that the for attribute in the <label> associates that label text with the input because it matches the input's id. This makes the form accessible for people with screen-readers and also let's you click on the label to toggle the input in most browsers.

I put the float and margin styles inline on the <label> for didactic purposes, but it would be better to use an external stylesheet.

<label for="chk" style="float:left; margin-right:3px;">Click Me!</label>
<input type="checkbox" id="chk" name="chk" />
Answered 11 months ago by Avram Eisner
  • I disagree about the convention being to float the labels to the right of the input. I have never added them to the right and every site I've made has had at least five separate forms on it, all with the labels to the left of the inputs. The only time I would consider putting the labels to the right is when the language was right-to-left. danwellman 11 months ago
  • WCAG Accessibility guidelines require that labels are to the left of their associated form elements for select elements and text, password and file inputs, and to the right of the form element for checkboxes and radio buttons. This is because non HTML-aware screen readers make the above assumptions about text adjacent to form elements. Richard Grevers 11 months ago
Log in to post your answer