5

~ is for the following sibling selector.

How could il select the class .content in reference to the class .select ?

HTML

<ul> <li> <a>content</a> </li> <li> <a class="select">selected li</a> </li> <li> <a>content</a> </li> </ul> <div class="content"> selected content <div> 

CSS (not working)

ul > li > a.select ~ .content { /* something */ } 
4
  • 6
    until CSS4 which has a parent selector - you can't Commented Dec 18, 2013 at 9:51
  • 1
    use jquery if you can. Commented Dec 18, 2013 at 9:52
  • but you could with javascript (note <div class=".content"> the . is not soo good) Commented Dec 18, 2013 at 9:52
  • @Danield: There is no parent selector. There is also no guarantee that the subject selector can be used in CSS. See my comments on the answer to stackoverflow.com/questions/1014861/… Commented Dec 18, 2013 at 9:55

3 Answers 3

1

It's unfortunately not possible with CSS, but you could use JQuery, i.e. something like

<script type="text/javascript"> $(".selected").parent().parent().siblings(".content").css("color", "red"); </script> 
  • $(".selected") you start at 'a' tag
  • .parent() move to parent 'li'
  • .parent() move to parent 'ul'
  • .siblings(".content") matches all siblings of the 'ul' you are currently at with class #content'
  • .css("color", "red") do whatever fancy css you like ;)
Sign up to request clarification or add additional context in comments.

Comments

0

There's currently no way in CSS to select the class .content in reference to the class .select.

However if you change your markup a little you can do something similar to what you're trying to do using the target pseudo-class

FIDDLE

2 Comments

I'm working on ember.js and I have generated .select class. I've planned to use css only, but no way to do that in css3, thanks Danield
You may want to read over your own answer again. It's not pure CSS if you have to change the HTML to accommodate the CSS.
0

You can obtain what you need without using JavaScript and leaving the HTML structure in your document as is by adding a new CSS rule, making use of the :has() relational pseudo-class which is being supported by all major browsers since the end of 2023 at least (see).

ul:has( > li > a.select) ~ .content { background-color: green; }
<ul> <li> <a>content</a> </li> <li> <a class="select">selected li</a> </li> <li> <a>content</a> </li> </ul> <div class="content"> selected content </div>

So here the needed CSS rule is:

ul:has( > li > a.select) ~ .content { ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.