After seeing the different selectors available (as of CSS3), the difference between the +
and ~
seem to be nearly the same. And it also appears there is no selector with opposite functionality to ~
.
Taken from www.w3schools.com:
div + p
: Selects all<p>
elements that are placed immediately after<div>
elements.p ~ ul
: Selects every<ul>
element that are preceded by a<p>
element.
The effect of p ~ ul
can be rewritten as:
Selects all
<ul>
elements that are placed after a<p>
element.
Because preceded by a <p>
means the <p>
is before the <ul>
.
This leaves the only difference between these operators being that +
only select elements immediately after (I assume all consecutive occurrences), whereas ~
selects elements anywhere after (but still with the same parent).
- Is my understanding of the difference correct?
Originally I thought the two operators were opposites due to the somewhat confusing english. So, my follow-up questions is this:
- How can I select every element placed before (or immediately before) another element?
I'm working with this case scenario:
I have 2 div's side-by-side in the same parent div.
<div id="container">
<div id="column_left">
</div>
<div id="column_right">
</div>
</div>
When I hover either div, both should gradually become more opaque using CSS transitions. When I'm not hovering either, they should become more transparent.
So, to select the right column when the left is hovered I would using the selector:
#column_left:hover+column_right {
opacity: 0.9;
transition: opacity 300ms;
-webkit-transition: opacity 300ms;
}
Now, how can I select the left column when the right is hovered?
Here is my CSS so-far:
div {
border: 1px solid black;
background: #262626;
}
#left {
float: left;
width: 200px;
height: 200px;
margin: 0;
transition: background 300ms;
-webkit-transition: background 300ms;
}
#right {
display: inline-block;
width: 200px;
height: 200px;
transition: background 300ms;
-webkit-transition: background 300ms;
}
#left:hover,#left:hover~#right {
background: #909090;
transition: background 300ms;
-webkit-transition: background 300ms;
}
<div id=left>
</div>
<div id=right>
</div>