Styling Direct Child Elements with the Child Selector
📖 Scenario: You are creating a simple webpage with a list of fruits inside a container. You want to style only the direct child <li> elements of the container differently from nested lists.
🎯 Goal: Build a webpage where only the direct child <li> elements of a <ul> inside a container have a blue background, while nested <li> elements remain unstyled.
📋 What You'll Learn
Create a container
<div> with class fruit-list containing a <ul> with some <li> items.Add a nested
<ul> inside one of the <li> items with its own <li> children.Use the CSS child selector
> to style only the direct child <li> elements of the .fruit-list > ul with a blue background.Ensure nested
<li> elements inside the nested <ul> are not styled with the blue background.💡 Why This Matters
🌍 Real World
Websites often have nested menus or lists. Using the child selector helps style only the top-level items without affecting nested ones.
💼 Career
Understanding CSS selectors like the child selector is essential for front-end developers to create clean, maintainable, and accessible web designs.
Progress0 / 4 steps