0

i have :

<form id="commentform">
    <p class="class1">
    <p class="class2">
    <p>
        <input id="captcha_code"></input>
    </p>
</form>

i want to style the "p" which does not have nor id neither class, i want to do it in a way that it does not style the other "p" tags. my "p" has a child with id (#captcha_code). i guess, if i can style #captcha_code parent, it will not concern the others... but i do not know how to do it...

Igor Laszlo
  • 714
  • 9
  • 31
  • possible duplicate of [CSS selector - element with this child](http://stackoverflow.com/questions/4220327/css-selector-element-with-this-child) – Mauro Nov 21 '13 at 14:55

3 Answers3

4

There is not currently a way to style an element based on whether or not it contains a particular child. The best way I can think to style the <p> in your case would be:

#commentform p:not([class]){

}

Closing your paragraphs isn't completely necessary, but it's always nicer to read. Additionally, you shouldn't add a closing tag for your input in HTML:

<form id="commentform">
    <p class="class1"></p>
    <p class="class2"></p>
    <p>
        <input id="captcha_code">
    </p>
</form>

JSFiddle

Community
  • 1
  • 1
George
  • 36,413
  • 9
  • 66
  • 103
  • Actually, closing the p elements does not always need to be done in HTML5 and in HTML4.01 it was optional, though I always feel safer in doing it. – Rob Nov 21 '13 at 15:08
  • Reference: http://www.w3.org/html/wg/drafts/html/master/text-level-semantics.html#optional-tags – Rob Nov 21 '13 at 15:14
  • Strangely enough, what you *aren't* allowed to do in HTML is have a closing `` tag. – BoltClock Nov 21 '13 at 15:19
  • Thanks @BoltClock The ironic thing is, the answer I have shared in the question was written by you and I didn't even realise. – George Nov 21 '13 at 15:25
  • @oGeez: Ha. I forgot I even wrote that answer. – BoltClock Nov 21 '13 at 15:26
  • Hello, thanks a lot, it worked !!! Just to know, i closed inout tag for a nicer effect and make it more understandable, i my html, there is no closing tag. – Igor Laszlo Nov 21 '13 at 16:14
0

Try this:

p > input {
  // style for that..
}

Using this, the style will be applied to the parent of the input.

Fiddle for this: http://jsfiddle.net/afzaal_ahmad_zeeshan/WyV7A/

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
0

To keep it simple, this can't be done.

Unlike the child selector >, There is no (and will probably never be) a parent < selector in css.

Have a look at this article to see why.

sitifensys
  • 2,024
  • 16
  • 29