tags:

views:

499

answers:

3

i need selector for selecting A element that directly followed by B element

<style>
    /* B that directly preceded by A*/
    A+B {

    }

    /* what the selector for selecting A that directly followed by B ????? */
</style>

<a>some text</a>
<b>some text</b>
+3  A: 

You can´t in css.

Edit: To be a bit more helpful, if you use for example jQuery (javascript), you can use .prev(): http://docs.jquery.com/Traversing/prev

jeroen
it seams the only solution's using JavaScript
Mostafa Farghaly
I think so, unless you can change the order of A and B...
jeroen
A: 

You could, but the support is still buggy. The name of that is the adjacent sibling selector

http://reference.sitepoint.com/css/adjacentsiblingselector

Dhana
That is for selecting an element B that is _preceded_ by an element A. But the question is "selector for selecting A element that _directly followed_ by B element", i.e. the other way around.
VolkerK
He already knew that, but he needs the inverse. The answer below is correct.
Dykam
That is the selector the OP has. He wants one which selects an element based on its next sibling, not its previous sibling.
David Dorward
He wants the sibling AFTER to be a specific type, while still styling the first of the two elements being tested. The `+` selector selects the second sibling (the one after.)
Blixt
That´s not an answer to his question, he already mentioned that in his example (A+B).
jeroen
+9  A: 

Do you mean to style A given that it has a B element directly inside or followed? Like this:

<A>
    <B>
    </B>
</A>

// OR

<A>
</A>
<B>
</B>

You can't do such a thing in CSS (yet). Eric Meyer states that this kind of selector has been discussed quite a few times on the CSS mailing list, and isn’t doable. Dave Hyatt, one of the core WebKit developers, comments with a good explanation of why it can’t be done.

Check out: Shaun Inman's blog post and the comment by Eric Meyer.
David Hyatt weighs in, too.

Nick Presta
i mean the second one, i understand the issue thank you nick :)
Mostafa Farghaly