general sibling selector css code example

Example 1: css select all siblings after element

/*To match to any sibling element after the selector on the left,
  use the tilde symbol '~'. This is called the general sibling combinator. */

h1 ~ p {
  color: black;
}

Example 2: general sibling selector

/* The general sibling combinator (~) separates
two selectors and matches the second element 
only if it follows the first element 
(though not necessarily immediately), 
and both are children of the same parent element. */

/* Paragraphs that are siblings of and
   subsequent to any image will be red */

img ~ p {
  color: red;
}

<img src="myimg.png"/>
<p>This will be red!</p>

Tags:

Css Example