3

Possible Duplicate:
Complex CSS selector for parent of active child

How would I make a css selector with that applies style to a table cell that has an input child element?

Community
  • 1
  • 1
Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • Jeremy, can you clarify: You want to style the td element based on whether it contains an input element. You're not trying to style the input element. Is that right? – Adam Bellaire Feb 13 '09 at 16:03
  • How are the cells generated, can you apply a class to only the cells with an input? If not, you´d have to do it dynamically using something like jquery (as far as I know you can´t select the parent of something...). – jeroen Feb 13 '09 at 16:07

3 Answers3

7

Unfortunately, you can't target parent elements in CSS yet (I believe it will come in CSS3)(see Sam's answer). If you want to do this, you'll either have to a) use javascript or b) add a class name to the td containing the input.

Here is a nice reference of the selectors you can use currently to target children, siblings, types, etc.

TJ L
  • 23,914
  • 7
  • 59
  • 77
4

According to Wikipedia:

Selectors are unable to ascend

CSS offers no way to select a parent or ancestor of element that satisfies certain criteria. A more advanced selector scheme (such as XPath) would enable more sophisticated stylesheets. However, the major reasons for the CSS Working Group rejecting proposals for parent selectors are related to browser performance and incremental rendering issues.

And for anyone searching SO in future, this might also be referred to as an ancestor selector.

Sam Hasler
  • 12,344
  • 10
  • 72
  • 106
4

If you're using jQuery:

$("td input").each(function() { $(this).parent("td").css("style", "value"); });
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Bryan A
  • 3,598
  • 1
  • 23
  • 29