Wednesday 28 September 2016

CSS Selectors - difference between and when to use ">", "+" or " "




When using CSS, I can query elements in the following ways:



div > .class  
div .class
div + .class



However I can't quite tell the exact difference between each of these DOM queries. Do they all point to child elements? I know that ">" and " " (space) do.



But under what circumstances would I use each?


Answer



In CSS these are called Combinators and means three different things:




  1. div > .class: is called Child selector and will select all elements that are direct children of a div and have the class .class.


  2. div .class: is called Descendant selectors and will select all elements inside a div and having the class .class.



  3. div + .class: is called Adjacent sibling selector and will match any element that immediately follows a div and have the class .class.




Example:



In the following example:




A title






Demo:





No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...