Child Selector (CSS selector)

Share this article

Description

This selector matches all elements that are the immediate children of a specified element. The combinator in a child selector is a greater-than sign (>). It may be surrounded by whitespace characters, but if it is, Internet Explorer 5 on Windows will incorrectly treat it as a descendant selector. So the best practice is to eschew whitespace around this combinator.

Consider this HTML fragment:

<ul>
  <li>Item 1</li>
  <li>
    <ol>
      <li>Subitem 2A</li>
      <li>Subitem 2B</li>
    </ol>
  </li>
</ul>

Let’s try to match elements in the above fragment with the selector below:

ul>li {
  ⋮ declarations
}

The child selector above will only match the two li elements that are children of the ul element. It will not match the subitems, because their parent is the ol element.

Example

Here’s an example of the child selector at work:

ul>li {
  ⋮ declarations
}

This selector matches all li elements that are the immediate children of a ul element—that is, all li elements that have a ul element as a parent.

Frequently Asked Questions (FAQs) about CSS Child Selectors

What is the difference between a child selector and a descendant selector in CSS?

In CSS, a child selector targets only the direct children of a specific parent element, while a descendant selector targets all elements that descend from a specific ancestor element, regardless of how many levels deep. The child selector uses the “>” symbol, while the descendant selector uses a space. For example, “div > p” selects only paragraphs that are direct children of a div, while “div p” selects all paragraphs within a div, regardless of their nesting level.

How can I select only the first child of an element using CSS?

To select only the first child of an element in CSS, you can use the “:first-child” pseudo-class. For example, “div > p:first-child” will select only the first paragraph that is a direct child of a div. This is useful when you want to apply styles only to the first child of an element.

Can I use the child selector to target elements based on their attributes?

Yes, you can combine the child selector with attribute selectors to target elements based on their attributes. For example, “div > input[type=’text’]” will select all text input fields that are direct children of a div.

How does the child selector work with class and id selectors?

The child selector can be used in conjunction with class and id selectors to target specific elements. For example, “#myDiv > .myClass” will select all elements with the class “myClass” that are direct children of the element with the id “myDiv”.

Can I use the child selector to target only the last child of an element?

Yes, you can use the “:last-child” pseudo-class in combination with the child selector to target only the last child of an element. For example, “div > p:last-child” will select only the last paragraph that is a direct child of a div.

How does the child selector work in responsive design?

The child selector can be used in responsive design to apply different styles to elements based on their parent’s size or other properties. This can be useful for adjusting layouts and styles based on screen size or device type.

Can I use the child selector with pseudo-elements?

Yes, you can use the child selector with pseudo-elements like “::before” and “::after”. For example, “div > p::before” will select the “::before” pseudo-element of all paragraphs that are direct children of a div.

How does specificity work with the child selector?

In CSS, specificity determines which styles are applied when there are conflicting rules. The child selector increases the specificity of a rule, meaning it will override any conflicting rules with lower specificity. For example, “div > p” has higher specificity than just “p”.

Can I use the child selector to target only odd or even children?

Yes, you can use the “:nth-child” pseudo-class in combination with the child selector to target only odd or even children. For example, “div > p:nth-child(odd)” will select all odd-numbered paragraphs that are direct children of a div.

What is the browser support for the child selector?

The child selector is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. However, it is not supported in Internet Explorer 6 or older versions. Always check the current browser support when using advanced CSS selectors.

Adam RobertsAdam Roberts
View Author

Adam is SitePoint's head of newsletters, who mainly writes Versioning, a daily newsletter covering everything new and interesting in the world of web development. He has a beard and will talk to you about beer and Star Wars, if you let him.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week