Introduction
Complex selector combinations help you style specific parts of a webpage by combining simple selectors. This lets you target exactly what you want to change.
Jump into concepts and practice - no test required
selector1 selector2 { property: value; }
selector1 > selector2 { property: value; }
selector1 + selector2 { property: value; }
selector1 ~ selector2 { property: value; }nav button { background-color: blue; }ul > li { list-style-type: square; }h2 + p { margin-top: 0; }.card ~ .card { border-color: gray; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Complex Selector Demo</title> <style> nav button { background-color: lightblue; border: none; padding: 0.5rem 1rem; margin: 0.25rem; cursor: pointer; } ul > li { list-style-type: square; color: darkgreen; } h2 + p { font-weight: bold; color: darkred; } .card ~ .card { border: 2px solid gray; padding: 1rem; margin-top: 0.5rem; } </style> </head> <body> <nav> <button>Home</button> <button>About</button> </nav> <section> <h2>Title</h2> <p>This paragraph is right after the title and is bold red.</p> <p>This paragraph is normal.</p> </section> <ul> <li>First item</li> <li>Second item</li> <div> <li>Not direct child, no square bullet</li> </div> </ul> <div class="card">Card 1</div> <div class="card">Card 2 with gray border</div> <div class="card">Card 3 with gray border</div> </body> </html>
<li> elements that are direct children of <ul> elements?> means direct child in CSS selectors.ul > li selects only li elements that are immediate children of ul.<p> element immediately following an <h2> element?+ selector targets the element immediately after the first sibling.h2 + p selects the p that comes right after an h2.<div> <p class="intro">Hello</p> <span>World</span> <p>Again</p> </div>
<p> element inside the <div>?div, there are two p elements separated by a span.div > p + p selects a p immediately following another p as a direct child of div. Here, the second p is not immediately after the first p (there is a span in between), so this selector matches nothing.div p ~ p selects any p siblings after a p inside div. The second p is a sibling after the first p, so this matches the second p.div p ~ p correctly selects the second p element.section > article + p<p> elements that are direct children of section?section > article + p means: select any p element that is immediately after an article which is a direct child of section.p elements that are direct children of section. This selector only selects p elements that come immediately after an article child, missing other p children.<li> elements that are inside a <ul> with class menu, but only if the <li> is not the first child. Which CSS selector achieves this?li elements inside ul.menu but exclude the first child li.ul.menu > li:not(:first-child) selects all direct li children of ul.menu except the first one.ul.menu li + li selects every li immediately following another li, which also works but only for adjacent siblings.ul.menu > li ~ li selects all li siblings after the first li as direct children, which also works.ul.menu > li:not(:first-child).