views:

267

answers:

3

I am trying to style some form labels by selecting them with their 'for' attribute. But nothing is being picked up when I preview it in IE7. I'm doing this because I'd like to style them differently to each other, without adding to the existing markup.

So if my css looks like the following, I get nothing:

<style>
 label[for="foo"] {
 background: blue;
 padding: 1em
}
</style>

<form>
 <label for="foo"/>bar</label>
 <input name="foo" type="text"/>
</form>

But if I change it to this, the styling works.

<style>
 label[fro="foo"] {
 background: blue;
 padding: 1em
}
</style>

<form>
 <label fro="foo"/>bar</label>
 <input name="foo" type="text"/>
</form>

Have you seen this kind of problem before? Is there a problem with the way I'm writing the CSS, IE7, or something else?

+3  A: 

This user seems to have had the same problem you are having: here

He says that because "for" is a reserved word, it can't be used as a property name. But 'htmlFor' is the DOM property name associated with the for attribute

Brandon
It seems that reserved words could be the source of the problem; although haven't seen conclusive evidence of that yet.
Sam Murray-Sutton
A: 

Labels are paired with specific input fields, so is there any reason why you cannot use class instead of creating a multitude of selectors in your CSS for this purpose?

Jayx
Can you clarify your meaning? Do you mean I should add specific classes to each label element?
Sam Murray-Sutton
Will you be using a different style for each label? If yes, then you can use id as well, but that would be no better than trying the method you are currently.What I am saying is that you might consider styling labels similarly and use classes for similarly styled labels.
Jayx
Yes I want to style them differently. What I am trying to do is avoid using additional markup, especially when all the browsers I am targetting should, in theory, support this kind of selector.
Sam Murray-Sutton
On a slight tangent, woudln't it be easier just to select the elements by inheritence(as opposed to classes?), if styling them all the same is your goal?
Sam Murray-Sutton
I agree with you there - I think that the point I'm trying to convey is that it seems like a bit of an overkill to create a selector for every label in your form as suggested in the method you asked about, and that there might be a different, fail safe approach.
Jayx