Open In App

Difference Between Descendant and Child Selectors in CSS

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Descendant selector and child selector in CSS are both used to target specific elements within the document structure, but they differ in their level of specificity and targeting.

Descendant Selector

It uses a space to select all descendants of a particular ancestor. In the below example, It selects all matching elements, regardless of their depth in the hierarchy. It’s less specific and more broad-reaching.

Syntax:

/* Selecting all paragraphs inside a div */
div p {
/* styles */
}

Child Selector

It uses the > symbol to select direct children of a particular parent. In the below example It specifically selects elements that are direct children of the specified parent, excluding nested descendants.

Syntax:

/* Selecting only direct children paragraphs inside a div */
div > p {
/* styles */
}

Differences

  • Specificity: Child selectors are more specific than descendant selectors because they only target direct children.
  • Scope: Descendant selectors have a broader scope, affecting all matching descendants. Child selectors narrow the scope to direct children only.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads