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.
| 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; } |
Combinators explain the relationship between two or more selectors based on the Document Object Model (DOM) tree hierarchy:
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;
}
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;
}
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;
}
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;
}
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>.
<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.
Boost your CSS and front-end development proficiency by exploring these closely connected topics:
Understand margins, borders, padding, content and the universal box-sizing fix.
Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.
Build powerful two-dimensional grid layouts with template areas and fr units.
Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.