{"id":14196,"date":"2011-09-06T20:04:48","date_gmt":"2011-09-07T03:04:48","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=14196"},"modified":"2020-05-14T07:58:53","modified_gmt":"2020-05-14T14:58:53","slug":"general-sibling","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/g\/general-sibling\/","title":{"rendered":"General sibling"},"content":{"rendered":"\n
The general sibling combinator (~) in CSS looks like this in use:<\/p>\n\n\n\n
.featured-image ~ p {\n font-size: 90%;\n}<\/code><\/pre>\n\n\n\nIn that example, you would be selecting all paragraphs in an article that come after the featured image (an element with a class name of “featured-image”) and making them of slightly smaller font-size<\/code>.<\/p>\n\n\n\nThis selects elements at the same hierarchy level. In this example .featured-image<\/code> and the p<\/code> elements are at the same hierarchy. If the selector continued past the p<\/code> or before .featured-image<\/code>, the normal rules apply. So .featured-image ~ p span<\/code> still would select spans that are descendents of whatever .featured-image ~ p<\/code> matches.<\/p>\n\n\n\nThe spec for selectors level 4 calls these “Following Sibling Combinators”<\/a>.<\/p>\n\n\nDemo<\/h3>\n\n\n
Here\u2019s another example that highlights all of the p<\/code> elements that follow an img<\/code>:<\/p>\n\n\n\nimg ~ p {\n background-color: #FEF0B6;\n padding: 5px;\n}<\/code><\/pre>\n\n\n\nWhich will result in the following:<\/p>\n\n\n\n