CSS General Sibling Selector

CSS general sibling selector is almost similar to the adjacent sibling selector except that it selects all siblings of the given elements instead of the adjacent sibling only.

To select all the general siblings of an element, put a tilde symbol(~) between the element and the sibling. For example, div ~ p, ul ~ p, etc.

In the example given below, the general sibling selector selects all the <p> elements that are siblings of the <div> element and applies the same styles to each of them.

Example:

div ~ p{
   background: yellow;
   border: 2px solid blue;
   padding: 5px;
}

The general sibling selector in the example below selects all the <p> elements that are siblings of the <ul> element.

Example:

ul ~ p{
   background: yellow;
   border: 2px solid blue;
   padding: 5px;
}

CSS General Sibling Selector Syntax

The general sibling selector has the following syntax:

element ~ general_sibling{
   CSS styles
}

Related Pages

CSS Tutorial: CSS Selectors

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts