Skip to main content

How to Select Elements Inside Another Element in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Put a space between two selectors and the rule styles only the elements that sit inside another element. .today li styles the items of the list with the class today, and no other list items.

Syntax​

Syntax
<outer selector> <inner selector> {
<declarations>
}
PartWhat it does
<outer selector>The element the others must be inside, such as .today.
The spaceThe descendant combinator. It means "inside", at any depth.
<inner selector>The elements to style, such as li.
<declarations>The styles they get.

MDN's Descendant combinator page explains that it matches elements with an ancestor that matches the first selector: a parent, a parent's parent, and so on.

The Example​

The example is two to-do lists, Today and Later. One rule makes the items of the Today list bold and leaves the Later list as it is.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule that makes li elements inside the class today bold, and a body with a Today list of class today holding Call the bank and Buy milk, and a Later list holding Water the plants.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Descendant combinator</title>
<style>
.today li {
font-weight: bold;
}
</style>
</head>
<body>
<p>Today:</p>
<ul class="today">
<li>Call the bank</li>
<li>Buy milk</li>
</ul>
<p>Later:</p>
<ul>
<li>Water the plants</li>
</ul>
</body>
</html>

.today li selects every li inside an element with the class today. The Later list has no class, so its item doesn't match.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: Today: with the bold items Call the bank and Buy milk, then Later: with the item Water the plants in normal weight.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The two Today items are bold. Chromium gave them a font weight of 700, the weight for bold.
  • Water the plants stayed at 400, the normal weight. It's an li, but it isn't inside .today.

Common Mistakes​

MistakeWhat happensDo this instead
Writing a comma instead of the space: .today, liThe comma makes a list of two selectors, so every li on the page turned bold, in both listsUse a space for "inside"
Expecting only the list's own items to matchThe space matches at any depth, so the items of a list nested inside it turn bold tooUse > when only the direct children should match

Troubleshooting​

SymptomLikely causeFix
Items in other lists change tooThe selector has a comma, or no outer selectorWrite the outer selector, a space and the inner one
Items of a nested list change tooThe descendant combinator reaches every levelUse .today > li

FAQ​

Does it match elements nested more than one level down?​

Yes. In a Today item that held a second list, the inner item matched .today li and turned bold.

How do I match only the direct children?​

Use the child combinator, >. The same inner item didn't match .today > li, because its parent is the inner list, not the .today list.

Conclusion​

A space between two selectors means "inside": the rule styles the elements that match the second selector and sit inside an element that matches the first. Use > when only the direct children should match.

Additional Resources​