css
May 25th, 2011

Understand ‘+’, ‘>’ and ‘~’ symbols in CSS Selector

It explains How to use different signs (+,> and ~) in CSS selector and their differences. Before starting, let us take a sample code to understand the signs.

   <div id="container">            
      <p>First</p>
       <div>
           <p>Child Paragraph</p>
       </div>
      <p>Second</p>
      <p>Third</p>      
   </div>

Space:

div#container p{
font-weight:bold;
}

It is the descendant selector. It will target all p tags within container div.

> Sign:

It will target elements which are DIRECT children of a particular element.

div#container > p {
  border: 1px solid black;
}

css selector

It will target all P element which are direct children of container div, not children of child div.

+ Sign:

It is Adjacent sibling combinator. It combines two sequences of simple selectors having the same parent and the second one must come IMMEDIATELY after the first.

div + p {  
   color: green;  
} 

css selector

It will only select the first element that is immediately preceded by the former selector. In our example, it will target to Second ONLY because the owner P element comes just after Div tag.

~ Sign:

It is general sibling combinator and similar to Adjacent sibling combinator. the difference is that the second selector does NOT have to immediately follow the first one means It will select all elements that is preceded by the former selector.

div ~ p{
background-color:blue;
} 


css selector

It will target both second and third.

Hope, you enjoyed this.

  • http://pulse.yahoo.com/_INR7X6Y4M4OMNSILWWHUKXZGKI Robert James

    Hello,
    This is a really good read for me, Must admit that you are one
    of the best blogger I ever saw. Looking forward for your next one.

  • Warren

    Really useful thanks. A lot better than needless class assigning.

  • http://profiles.google.com/dario.malfatti Dario Malfatti

    Thank you so much! Really useful

  • LovelyLines

    Clean and simple. Good job!

    Thanx

  • kill_windows

    Not working in IE 7/8. I do not understand. Why ?

  • Ashok Kumar

    Very Useful Post… Thanks a Lot…

  • http://twitter.com/EWdesigngroup EastWest DesignGroup

    Thanks for a great tutorial! You have really helped. The 3rd party
    software we are working with has some bad code that was producing
    duplicate DIV’s with the same ID, so we had to temporary hide
    one of the containers until there is a permanent fix. The following code
    did the trick => #social + #social {display:none;}

  • http://program-de-slabit.ro Herbalife

    very helpful thank you

  • http://www.ayouhealth.com Dennis Ratemo

    Thank you for such useful information…

  • Sarfaraz Hussain

    very nice explanation

  • ssougnez

    Short and precise. very nice. Thanks

  • http://blog.ansyori.com ansyori

    cool explanation!

  • http://www.binarytides.com/ Silver Moon

    thanks for such a clear explanation of those 3 css symbols

  • sushil kumar

    is there any sign which can be used to select both child and its sub child?