0
0
CSSmarkup~5 mins

Complex selector combinations in CSS

Choose your learning style9 modes available
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.
You want to style only buttons inside a navigation bar.
You need to change the color of all paragraphs inside a section but not outside.
You want to style list items that are direct children of a menu.
You want to style elements that have a certain class and are inside a specific container.
You want to style an element only if it is next to another specific element.
Syntax
CSS
selector1 selector2 { property: value; }
selector1 > selector2 { property: value; }
selector1 + selector2 { property: value; }
selector1 ~ selector2 { property: value; }
A space between selectors means 'descendant' - any level inside.
The '>' means direct child only.
The '+' means the next sibling element immediately after.
The '~' means any sibling elements after.
Examples
Styles all buttons inside any <nav> element, no matter how deep.
CSS
nav button { background-color: blue; }
Styles only list items that are direct children of a <ul>.
CSS
ul > li { list-style-type: square; }
Styles the paragraph that comes immediately after an <h2>.
CSS
h2 + p { margin-top: 0; }
Styles all .card elements that come after another .card sibling.
CSS
.card ~ .card { border-color: gray; }
Sample Program
This page shows different complex selector combinations in action. Buttons inside nav have a blue background. Only direct li children of ul have square bullets. Paragraph immediately after h2 is bold and red. Cards after the first card have gray borders.
CSS
<!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>
OutputSuccess
Important Notes
Using complex selectors helps keep your CSS organized and precise.
Be careful: too many complex selectors can slow down the browser a bit.
Remember the difference between descendant (space) and child (>) selectors.
Summary
Complex selectors combine simple selectors to target elements precisely.
Use space for any descendant, > for direct child, + for next sibling, and ~ for any following siblings.
They help style parts of a page without adding extra classes or IDs.