Thursday 12 September 2013

Complex Selectors - Part2

Sibling Selectors

Knowing how to select children of an element is largely beneficial, and quite commonly seen. However sibling elements, those elements that share a common parent, may also need to be selected. These sibling selections can be made by way of the general sibling and adjacent sibling selectors.

General Sibling Selector

The general sibling selector allow elements to be selected based on their sibling elements, those which share the same common parent. They are created by using the tilde character, ~, between two elements within a selector. The first element identifies what the second element shall be a sibling with, and both of which must share the same parent.
The h2 ~ p selector is a general sibling selector that looks for p elements that follow, and share the same parent, of anyh2 elements. In order for a p element to be selected it must come after any h2 element.
The paragraphs on lines 5 and 9 are selected as they come after the heading within the document tree and share the same parent as their sibling heading, thus are selected and marked in bold.
CSS
  1. h2 ~ p {...}
HTML
  1. <p>...</p>
  2. <section>
  3. <p>...</p>
  4. <h2>...</h2>
  5. <p>...</p>
  6. <div>
  7. <p>...</p>
  8. </div>
  9. <p>...</p>
  10. </section>

Adjacent Sibling Selector

Occasionally a little more control may be desired, including the ability to select a sibling element that directly follows after another sibling element, which is where the adjacent sibling element comes in. The adjacent sibling selector will only select sibling elements directly following after another sibling element. Instead of using the tilde character, as with general sibling selectors, the adjacent sibling selector uses a plus character, +, between the two elements within a selector. Again, the first element identifies what the second element shall directly follow after and be a sibling with, and both of which must share the same parent.
Looking at the adjacent sibling selector h2 + p only p elements directly following after h2 elements will be selected. Both of which must also share the same parent element.
The paragraph on line 5 is selected as it directly follows after its sibling heading along with sharing the same parent element, thus selected and marked in bold.
CSS
  1. h2 + p {...}
HTML
  1. <p>...</p>
  2. <section>
  3. <p>...</p>
  4. <h2>...</h2>
  5. <p>...</p>
  6. <div>
  7. <p>...</p>
  8. </div>
  9. <p>...</p>
  10. </section>

Sibling Selectors Example

HTML
  1. <input type="checkbox" id="toggle">
  2. <label for="toggle"></label>
  3. <nav>
  4. <ul>
  5. <li><a href="#">Home</a></li>
  6. <li><a href="#">About</a></li>
  7. <li><a href="#">Services</a></li>
  8. <li><a href="#">Contact</a></li>
  9. </ul>
  10. </nav>
CSS
  1. input {
  2. display: none;
  3. }
  4. label {
  5. background: #f5f5f5;
  6. background: linear-gradient(#fff, #eee);
  7. border: 1px solid #ccc;
  8. border-radius: 6px;
  9. cursor: pointer;
  10. }
  11. label:hover {
  12. color: #f7941d;
  13. }
  14. input:checked + label {
  15. background: #f5f5f5;
  16. box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15);
  17. color: #8c9198;
  18. }
  19. nav {
  20. max-height: 0;
  21. overflow: hidden;
  22. transition: all .2s linear;
  23. }
  24. input:checked ~ nav {
  25. max-height: 500px;
  26. }

Demo

Sibling Selectors Overview

ExampleClassificationExplanation
h2 ~ pGeneral Sibling SelectorSelects an element that follows anywhere after the prior element, in which both elements share the same parent
h2 + pAdjacent Sibling SelectorSelects an element that follows directly after the prior element, in which both elements share the same parent