CSS Selectors & Combinators Complete Guide

CSS Selectors are the targeting mechanisms that tell the web browser which HTML elements should receive styling declarations. Mastering selectors allows you to style elements precisely without adding redundant classes to your HTML markup.

1. Fundamental Basic Selectors

Selector Type Syntax Targeting Rule Example Snippet
Universal Selector * Matches every single element on the entire page. * { box-sizing: border-box; }
Element (Type) element Matches all elements matching the specified HTML tag name. p { line-height: 1.6; }
Class Selector .classname Matches all elements having the specified class="..." attribute. Multiple elements can share the same class. .btn { padding: 10px 20px; }
ID Selector #idname Matches the single element on the page having the specified id="..." attribute. Must be unique per page. #header { background: #d22b2b; }
Grouping Selector sel1, sel2 Applies the same declaration block to multiple selectors separated by commas. h1, h2, h3 { font-family: serif; }

2. CSS Combinator Selectors

Combinators explain the relationship between two or more selectors based on the Document Object Model (DOM) tree hierarchy:

A. Descendant Selector (Space)

Matches all target elements that are descendants (children, grandchildren, great-grandchildren) of the specified parent element anywhere inside it.

/* Matches ANY <a> element inside <nav>, no matter how deeply nested */
nav a {
  color: #d22b2b;
  text-decoration: none;
}

B. Child Selector ( > )

Matches only direct children of the parent element. It does NOT style deeper nested grandchildren.

/* Matches only direct <li> children of <ul class="menu"> */
ul.menu > li {
  border-bottom: 1px solid #e2e8f0;
}

C. Adjacent Sibling Selector ( + )

Matches an element that is the immediate next sibling sharing the exact same parent.

/* Matches the very first <p> immediately following an <h2> */
h2 + p {
  font-size: 18px;
  font-weight: 500;
  color: #0f172a;
}

D. General Sibling Selector ( ~ )

Matches all sibling elements that follow the specified element, provided they share the same parent.

/* Matches ALL <p> elements that follow an <h2> under the same parent */
h2 ~ p {
  color: #334155;
}

BCA University Exam Difference Question

Question: Distinguish between the Descendant Selector (space) and Child Selector (>) in CSS with an example.

Answer: The Descendant selector (div p) styles every <p> element nested at any level inside a <div> (children, grandchildren, etc.). The Child selector (div > p) strictly styles only those <p> elements that are direct, first-level children of the <div>.

Dry Run Practice: Which elements are colored red?

<div class="box">
  <p>Paragraph 1</p>
  <section>
    <p>Paragraph 2</p>
  </section>
</div>

If we apply .box > p { color: red; }, which paragraph turns red?

Answer: Only Paragraph 1! Paragraph 2 is wrapped inside <section>, making it a grandchild, not a direct child of .box.

Explore Related CSS Tutorials & Next Steps

Boost your CSS and front-end development proficiency by exploring these closely connected topics:

CSS Box Model & border-box

Understand margins, borders, padding, content and the universal box-sizing fix.

CSS Flexbox Complete Guide

Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.

CSS Grid 2D Layout Matrix

Build powerful two-dimensional grid layouts with template areas and fr units.

CSS Specificity & Cascade

Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. Can an HTML element have both a class and an ID?

Yes. An HTML element can have both, such as

Q2. Why is using ID selectors in CSS discouraged in modern front-end development?

ID selectors carry a very high specificity weight (0,1,0,0). When you style with IDs, overriding those styles later requires even higher specificity or !important, leading to messy, unmaintainable stylesheets. Modern methodologies (like BEM) advocate styling exclusively with classes.

Q3. What does the selector 'div.card' mean?

It is a compound selector that matches only
elements that have the class 'card'. It will not match a
.

Q4. Can you combine multiple combinators in one selector?

Yes. For example, 'header > nav ul li a' combines child and descendant combinators to navigate the DOM tree from header down to the link.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!