0

quick question.Take this html

<form id="the_form">
    <input type="checkbox" value="ideal" name="ideal_drive"  id="ideal"><label for="ideal">This is a test only only</label><br>
...

Say I want to select the text in the label and give it a different colour. I tried this rule

#the_form label[for='ideal']{
    color:yellow;
}

It works ok but not in IE7. SO, I was thinking to use the :first-child selector:

#the_form label:first-child{
    color:yellow;
}

but it doesn't work at all.

Any idea what I can use to make sure it works on every browser?
thanks

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by Violet_82
0

I dont have IE 7 handy to test, but have you tried using jQuery?

<!DOCTYPE html>
<html>
<head>
 <title>Demo</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>

<body>
<form id="the_form">
    <input type="checkbox" value="ideal" name="ideal_drive" id="ideal">
       <label for="ideal">This is a test only only</label><br />
</form>  

<script>
  $('label[for="ideal"]').css('color', 'yellow');
</script>

</body>
</html>
1

no I was hoping to keep it in the css to be honest, that's why I didn't use jquery : - )

0

The problem is that :first-child is a pseudo-class that is not supported by anything less than IE8.

The first solution you gave is an attribute selector, which should be supported by IE7, you have to ensure you specify a doctype at the top your html file though.

If that doesn't work, your only pure CSS option would be to give the first label in the form it's own class or id, and define it's styles based on that selector.

0

If you want to use the same color for all labels, simply use:

#the_form label{
color:yellow;
}

This makes the text of all labels yellow. If you want to use different colors or background colors, you have to use different classes.

0

If you have no jQuery integration in the project, i wouldnt include the overhead just for this issue either.

0

hi thanks all. gavinflud, can a label tag have an id? Sorry it didn't occur to me!

@Baradaran, no it's just one lable

@orgeM yes I include jquery in my project but I just wanted to use as much css as possible that's why I didn't want to use jquery to achieve this

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.