CSS COMBINATORS SELECTORS

css3There are four different combinator selectors in CSS:

  1. descendant selector (space)
  2. child selector (>)
  3. adjacent sibling selector (+)
  4. general sibling selector (~)

DESCENDANT SELECTOR

The descendant selector matches all elements that are descendants of a specified element. The figure shows the paragraphs that are the children of a div element. The descendant selector – typically represented by a single space character () – combines two selectors such that elements corresponding to the second selector are selected if they have an ancestor element (parent, parent of parent, parent of parent of parent, etc.) which corresponds to the first selector.

descendand selector

CHILD SELECTOR

The child selector selects all elements that are children of a specified element. In this figure, the blocks with green color are the blocks affected by the child selector. As we can see, there is only the selection of those paragraph elements which are direct children of the div element. The child selector (>) is positioned between two CSS selectors. Matches only those elements that match the second selector that are direct children of the elements that match the first. The lowest descendants in the hierarchy do not match.

child selector

ADJACENT SIBLING SELECTOR

The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element and “adjacent” means “follow immediately. In this figure, the blocks with green color are the selected blocks affected after using the adjacent sibling selector. There is paragraph selection which comes immediately after another paragraph element. The adjacent sibling selector having a (+) symbol is placed between two CSS selectors. Matches only those elements that match the second selector which are the next sibling of the first selector.

Adjacent sibling selector

GENERAL SIBLING SELECTOR

The general sibling selector selects all elements that are successive siblings of a specified element. In this figure, the blocks with green color are the selected blocks affected after using the general sibling selector. If you want to select the siblings of an element even if they are not directly adjacent, you can use this combinator (~).

general-sibling-selector

CSS PSEUDO CLASSES

A pseudo-class is used to define a special state of an element. For example, it can be used for:

  • Style an element when a user hovers over it with the mouse
  • Give a different style to visited and unvisited links
  • Style when an element gets focus

selectors

CSS PSEUDO-ELEMENTS

A CSS pseudo-element is used to style specified parts of an element.

For example, it can be used for:

  • Define the style of the first letter, or line, of an element
  • Insert content before or after an element

CSS ATTRIBUTE SLECTORS

  • The [attribute] selector is used to select elements with a specific attribute.
  • [attribute = “value”] is used to select elements with a specified attribute and value.
  • [attribute ~ = “value”] is used to select elements with an attribute value containing a specified word.
  • [attribute | = “value”] is used to select elements with the specified attribute from the specified value. Note: the value must be a whole word, alone, like class = “top”, or followed by a hyphen (-), like class = “top-text”!
  • [attribute ^ = “value”] is used to select elements whose attribute value begins with a specified value. Note: the value does not have to be a whole word!
  • [attribute $ = “value”] is used to select elements whose attribute value ends with a specified value. Note: the value does not have to be a whole word!
  • [attribute * = “value”] is used to select elements whose attribute value contains a specified value. Note: the value does not have to be a whole word!

STYLING FORMS

Attribute selectors can be useful for styling modules without a class or ID:

Copy to Clipboard
Copy to Clipboard

CSS :nth-child() Selector

The :nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent. n can be a number, a keyword, or a formula.

Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of a particular type, of its parent.

Copy to Clipboard

CSS :nth-last-child() Selector

The :nth-last-child(n) selector matches every element that is the nth child, regardless of type, of its parent, counting from the last child. n can be a number, a keyword, or a formula.

Tip: Look at the :nth-last-of-type() selector to select the element that is the nth child, of a specified type, of its parent, counting from the last child.

Copy to Clipboard

CSS :nth-last-of-type() Selector

The :nth-last-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent, counting from the last child. n can be a number, a keyword, or a formula.

Tip: Look at the :nth-last-child() selector to select the element that is the nth child, regardless of type, of its parent, counting from the last child.

Copy to Clipboard

CSS :nth-of-type() Selector

The :nth-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent. n can be a number, a keyword, or a formula.

Tip: Look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.

Copy to Clipboard

LINKS TO PREVIOUS POST

PREVIOUS POST LINKS

LINK TO THE CODE ON GITHUB

GITHUB