Home > UI/UX Development > CSS > CSS selectors

CSS selectors

This post was most recently updated on September 26th, 2017

In CSS, Selectors play main role to selecting a particular content. CSS selectors are very brief topic but for simplicity we categorize it in following easy way

  1. Universal Selectors
  2. Element type
  3. ID selectors
  4. Class selectors
  5. Descendant selectors
  6. Child selectors
  7. Sibling selectors
  8. Adjacent sibling selectors
  9. Attribute selectors
  10. Pseudo class
  11. Pseudo element

Universal Selectors

Universal selectors are like a wild card, it select all html elements on DOM. Declaration is simply as follows here we used asterisk symbol (*).

Element Type

Element type selectors will select html element have same name.

ID selectors

ID selectors selects html element that have id attribute have specified name.  It declared with hash symbol (#).

Class Selectors

Class selectors selects all html element that have same class name. It declared with dot symbol (.).

Descendant selectors

Descendant selectors selects more accurately html elements. It specified with more than one classes, ids etc.

Above css only select div that have class container which present in mainWrapper div only.

Child selectors

Child selectors is similar to Descendant combinatory selectors, however it select on immediate child. It declared with right arrow symbol (>).

Above css only works on div floatedBox that have immediate child of container class so it select only 1st div.

Sibling selectors

Sibling selectors matches elements based on siblings relationship. It declared with tilde symbol (~). 

Above css will works on 1st three and last paragraph because they are on same level. And as other paragraph are under box class, the css will not applied.

Adjacent sibling selectors

Adjacent sibling selectors matches elements based on siblings relationship. The Siblings must be an immediate sibling and not a general sibling. It declared with plus symbol (+).

Above css selects only immediate paragraph of div so it select only one paragraph 1 and 6 only no matter how may paragraph is sits at same level.

Attribute selectors

Attribute selectors targets html elements that matches attribute values. It declared with square symbol ( [ ] ).

Above css selects link tag which target attribute value is _blank.

Attribute Selector Table

Pseudo class

Pseudo class select html element which match a pseudo state. It declared with colon symbol ( : ).

Above css selects link tag when user mouse over on link.

Pseudo elements

Pseudo elements select html element which match a pseudo state. It declared with double colon symbol ( : 🙂

Above css selects div element and add content “- Remember this” after div.


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">