0

everyone! I am quite new to HTML and CSS. I learned about 2 weeks ago. On my final website, I wanted to add a drop-down menu at the top. I want that when I hover over the 'store' tab, the 'shopping cart' tab slides down. I want to do it in only CSS. How do I reference one HTML element from another one from CSS code? What is the difference between > + ~ and other combinators. Sample code:

#store > #shoppingCart{}
Adrift
  • 58,167
  • 12
  • 92
  • 90

4 Answers4

3

In this instance, #store > #shoppingCart{} is a little useless, #shoppingCart should be unique, so you don't need to reference any parents or anything, which is what > essentially does.

Child Combinator or >

#a > #b means a b element that is a child (but not a grandchild, and so on) of a, eg:

   <div id="a">
     <div id="b"></div>     // selects this element
   </div>

Adjacent Sibling Combinator or +

#a + #b means a b element that comes directly after a in the dom, eg:

   <div id="a"></div>
   <div id="b"></div>       // selects this element

General Sibling Combinator or ~

#a ~ #b is similar to +, but it is any sibling after it in the dom, so the following could work:

   <div id="a"></div>
   <div>
     <a></a>
   </div>
   <div id="b"></div>       // selects this element

However, as for your question, you probably want something like this:

#store:hover ~ #shoppingCart {
  // mouse is hovered over #store and we want to affect a sibling
  // with id #shoppingCart that is "later" in the dom
}

More info on selectors can be found here

Andy
  • 14,427
  • 3
  • 52
  • 76
2

In CSS, the symbols you mention are:

  • > is the the child combinator;
  • + is the adjacent sibling combinator;
  • ~ is the general sibling combinator.

The difference between them is this:

#store > .shoppingCart { background-color: red; }

<div class=shoppingCart>This will not be targeted</div>
<div id=store>
  <div class=shoppingCart>This WILL be targeted</div>
  <div class=shoppingCart>This WILL ALSO be targeted</div>
  <div class=inner>
    <div class=shoppingCart>This will not be targeted</div>
  </div>
</div>

<div class=shoppingCart>This will not be targeted either</div>
<div class=shoppingCart>Neither will this</div>

The > combinator selects only direct child elements (elements directly underneath the first one) of the selector specified, so, in this case, only the one immediately underneath #store.

#store + .shoppingCart { background-color: red; }

<div class=shoppingCart>This will not be targeted</div>

<div id=store>
  <div class=shoppingCart>This will not be targeted</div>
  <div class=shoppingCart>This will not be targeted either</div>
  <div class=inner>
    <div class=shoppingCart>This will not be targeted either</div>
  </div>
</div>

<div class=shoppingCart>This WILL be targeted</div>
<div class=shoppingCart>But this one won't be</div>

The + combinator selects an element that is the immediate next sibling (elements that are on the same level, ie. next to one another) of the first element, so if there is a .shoppingCart that is a sibling after a #store, it will be selected - but only if that element is AFTER the first one; a previous sibling cannot be selected in this way.

#store ~ .shoppingCart { background-color: red; }

<div class=shoppingCart>This will not be targeted</div>

<div id=store>
  <div class=shoppingCart>This will not be targeted</div>
  <div class=shoppingCart>This will not be targeted either</div>
  <div class=inner>
    <div class=shoppingCart>This will not be targeted either</div>
  </div>
</div>

<div class=shoppingCart>This WILL be targeted</div>
<div class=shoppingCart>This WILL be targeted as well</div>

The ~ combinator selects an element that is any following sibling of the first element, so if there is a .shoppingCart that follows as a sibling of #store, it will be selected.

Adam Hepton
  • 674
  • 1
  • 6
  • 12
  • 1
    Your definitions of `+` and `~` are not quite correct: both only target siblings that come *after* the element selected by the first part of the combinator; the difference between the two is that `+` only selects the immediate next element (if it matches) whereas `~` selects any next elements (immediate or otherwise). There are no combinators to select arbitrary (previous or next) kinds of siblings. – Raphael Schweikert Jun 24 '13 at 14:58
  • You're absolutely right, of course. I don't know what I was thinking. Thanks. – Adam Hepton Jun 26 '13 at 07:34
0

E > F -> Matches any F element that is a child of an element E.
E + F -> Matches any F element immediately preceded by a sibling element E.
E ~ F -> an F element preceded by an E element

You will find the full specification for CSS selectors here: http://www.w3.org/TR/css3-selectors/

Additional learning resource: http://css.maxdesign.com.au/selectutorial/

Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62
  • 2
    `E[foo~="warning"]` That's not a combinator. The latest specification can be found here: http://www.w3.org/TR/css3-selectors where you can find a `~` combinator. – BoltClock Jun 24 '13 at 14:38
0

This is the link you are probably looking for: http://www.w3schools.com/cssref/css_selectors.asp

Let's do a few examples:

#store > #shoppingCart {

   //code

}

This will select all elements that have the ID shopping cart and have a parent of ID store.

Another example:

p~ul {

    //code

}

Selects every ul element that are preceded by a p element

Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158