div + p {}selects all<p>elements that are placed immediately after<div>elementsp ~ divselects every<div>element that is preceded by a<p>element
So what's the difference?
I am trying to make a <div id="bar"> appear when <div id="foo"> is on hover, and in my CSS both #foo:hover ~ #bar and #foo:hover + #bar seem to do the exact same thing.
#foo,
#bar {
height: 200px;
width: 200px;
}
#foo {
background-color: indianred;
cursor: pointer;
}
#bar {
visibility: hidden;
background-color: steelblue;
}
/* Example 1 */
#foo:hover ~ #bar {
visibility: visible;
}
/* Example 2 */
#foo:hover + #bar {
visibility: visible;
}
/* Both example 1 & 2 do the same thing */
<div id="foo">
</div>
<div id="bar">
</div>