CSS combinators

Descendant combinator

Descendant combinator is used to select the elements which are descendant of another element.

Syntax:

selector1 whitespace selector2

HTML snippet:

<div id="mystyle1">
<p>sdfsdsdfdsf</p>
<div>
This is a sample text placed as a placeholder.

This is a sample text placed as a placeholder.
 

<p>Hello world! Welcome to this tutorial page!</p>
 

This is a sample text placed as a placeholder.

This is a sample text placed as a placeholder.
</div>
</div>

 

CSS snippet:

#mystyle1 p{
background-color : #e1e1e1;
width : 400px;
}

 

Output:

Here, the style is applied to both the <p> tag in the html.

 

Child combinator

Child combinator is used to select the direct child of another element.

Syntax:

selector1 > selector2

HTML snippet:

<div id="mystyle1">
<p>sdfsdsdfdsf</p>
<div>
This is a sample text placed as a placeholder.

This is a sample text placed as a placeholder.

 

<p>Hello world! Welcome to this tutorial page!</p>

 

This is a sample text placed as a placeholder.

This is a sample text placed as a placeholder.
</div>
</div>

 

CSS snippet:

#mystyle1>p{
background-color : #e1e1e1;
width : 400px;
}

 

Output:

Here, the style is applied only to <p> tag which is the direct child of the <div> tag with id as “mystyle1”in the html.

 

Adjacent Sibling combinator

Adjacent sibling combinator is used to select an element “selector2” which is present immediately after “selector1” and shares the same parent.

Syntax:

selector1 + selector2

HTML snippet:

<div id="mystyle1">
<h1> Heading 1 </h1>
<p>the combinator “h1 + p” will select this paragraph element</p>
<div>
<p>the style won’t be applied to this element</p>
</div>
</div>

 

CSS snippet:

h1 + p {
background-color : #e1e1e1;
width : 400px;
}

 

Output:

Here, the style is applied only to the first<p> tag.

 

General Sibling combinator

General sibling combinator is used to select an element “selector2” which is present after “selector1” (not necessarily immediately) and shares the same parent.

Syntax:

selector1 ~ selector2

HTML snippet:

<div id="mystyle1">
<h1> Heading 1 </h1>
<h2> sample heading 2</h2>
<p> the combinator “h1 ~ p” will select this paragraph element</p>
<div>
<p>the style won’t be applied to this element</p>
</div>
<p> the combinator “h1 ~ p” will select this paragraph element</p>
</div>

 

CSS snippet:

h1 ~ p {
background-color : #e1e1e1;
width : 400px;
}

 

Output:

Here, the style is applied only to all the<p> tag which shares the same parent as of <h1> element.

 

Posted on July 22, 2014 in CSS

Share the Story

Response (1)

  1. Sisam
    February 17, 2016 at 10:24 pm · Reply

    Hi,The right sidebar is tonhuicg the middle column in the homepage as you can see. I want to change the size same like left sidebar, not tonhuicg the middle column.Thanks for reply.

Leave a Reply to Sisam Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top