5

I have used label for with both input textboxes:

<label for="Username">Username</label>
<input id="Username"  type="text" value="">

and checkboxes/radioboxes

<label for="Live">System is Live</label>
<input id="Live" name="Live" type="checkbox" value="false">

The trouble I have is how do I specify different css for the labels for different input types.

If I do a generic label css:

label {
    color: #00a8c3;
    line-height: 20px;
    padding: 2px;
    display: block;
    float: left;
    width: 160px;
}

I find I either end up with unaligned checkboxes or badly positioned textboxes.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chris Nevill
  • 5,922
  • 11
  • 44
  • 79

3 Answers3

15

You could add classes to the labels. For example:

<label for="Username" class="textbox-label">Username</label>
<input id="Username" type="text" value="">

<label for="Live" class="checkbox-label">System is Live</label>
<input id="Live" name="Live" type="checkbox" value="false">

Then use the class values in CSS:

label.textbox-label {
 /*some styles here*/
}

label.checkbox-label {
 /*some styles here*/
}

Alternatively, if you had the labels after the inputs, you could select like this:

input[type="text"] + label { 
  /*some styles here*/
}

input[type="checkbox"] + label { 
  /*some styles here*/
}
CaribouCode
  • 13,998
  • 28
  • 102
  • 174
6

You could write css selector like this will work:

label[for=Username]{ /* ...definitions here... / } 

label[for=Live] { / ...definitions here... */ }
DilumN
  • 2,889
  • 6
  • 30
  • 44
Anil Sharma
  • 125
  • 1
  • 6
0

You could write your css selector like this:

label[for=Username] 
label[for=Live]

You may also have a look at this thread:
CSS Selector for selecting an element that comes BEFORE another element?

Community
  • 1
  • 1
Matthias Holdorf
  • 1,040
  • 1
  • 14
  • 19