How to Select Elements Inside Another Element in CSS
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
<outer selector> <inner selector> {
<declarations>
}
| Part | What it does |
|---|---|
<outer selector> | The element the others must be inside, such as .today. |
| The space | The 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:

<!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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Writing a comma instead of the space: .today, li | The comma makes a list of two selectors, so every li on the page turned bold, in both lists | Use a space for "inside" |
| Expecting only the list's own items to match | The space matches at any depth, so the items of a list nested inside it turn bold too | Use > when only the direct children should match |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Items in other lists change too | The selector has a comma, or no outer selector | Write the outer selector, a space and the inner one |
| Items of a nested list change too | The descendant combinator reaches every level | Use .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
- Descendant combinator, on MDN.
- Child combinator, on MDN.
