jerrygarciuh
jerrygarciuh

Reputation: 21998

CSS selector for label of any element with class foo

Code will be like

<label for="bar">Baz: <input type="text" name="bar" id="bar" class="foo"/></label>

I would like to style the label how do I select with CSS?

Upvotes: 2

Views: 2404

Answers (2)

Uncle Iroh
Uncle Iroh

Reputation: 6055

There is currently no way to select a parent element using css alone. This will be possible in the upcoming css 4 (Thanks andyB)

So you really have 2 other options:

  1. Modify the html so that you have a handle on the parent so that you can style it.

  2. Use javascript to select the parent based on the child's css/id/name attribute.

Upvotes: 2

Nix
Nix

Reputation: 5998

Like so:

label[for="bar"] { color: red; }

This will target the label for "bar". It's a CSS2.1 selector, and it works in everything except IE6.

See example on jsFiddle: http://jsfiddle.net/TheNix/kTwDN/

Upvotes: 0

Related Questions