CSS Combinators
In this page:
ancestor descendant { }
parent > child { }
element + adjacent-sibling { }
element ~ general-sibling { }
Descendant Selector (Space)
Descendant selector किसी specified parent container के अंदर कहीं भी nested हर matching element को target करता है, चाहे वे कितनी भी गहराई से nested हों (children, grandchildren, great-grandchildren, वगैरह)। यह selectors के बीच एक simple space से define होता है।
उदाहरण: The Descendant Selector (Space)
<style>
.card p {
color: gray;
}
</style>
<div class="card">
<p>Nested paragraph</p>
<div><p>Deeply nested paragraph</p></div>
</div>
Child Selector (>)
Child selector (> symbol से defined) सिर्फ किसी specified parent container के direct children को target करता है, ज़्यादा गहराई से nested किसी भी element (जैसे grandchildren या great-grandchildren) को नज़रअंदाज़ करते हुए।
उदाहरण: The Child Selector (>)
<style>
.menu > li {
color: blue;
}
</style>
<ul class="menu">
<li>Top-level item</li>
<li><ul><li>Nested item</li></ul></li>
</ul>
Adjacent Sibling Selector (+)
Adjacent sibling selector (+ symbol से defined) उसी layout level पर किसी specified element के ठीक बाद स्थित अगले sibling element को target करता है।
उदाहरण: The Adjacent Sibling Selector (+)
<style>
h2 + p {
font-weight: bold;
}
</style>
<h2>Heading</h2>
<p>Immediately after heading</p>
<p>Not affected</p>
General Sibling Selector (~)
General sibling selector (~ symbol से defined) उसी layout level पर किसी specified element के बाद कहीं भी स्थित सभी matching sibling elements को target करता है, बीच में आने वाले elements चाहे जो भी हों।
उदाहरण: The General Sibling Selector ()
<style>
h2 ~ p {
color: green;
}
</style>
<h2>Heading</h2>
<p>Affected sibling</p>
<p>Also affected sibling</p>
कई Selectors को Combine करना
आप elements को extreme precision से target करने वाले complex target paths बनाने के लिए एक ही selector rule में कई अलग combinators combine कर सकते हैं।
उदाहरण: Combining Multiple Selectors
<style>
.card > h2 + p {
color: purple;
}
</style>
<div class="card">
<h2>Title</h2>
<p>Styled paragraph</p>
</div>
- descendant selectors (space) इस्तेमाल करना जबकि आप सिर्फ direct children को target करना चाहते हैं, जिससे accidental styling leaks होती हैं।
- यह भूल जाना कि adjacent sibling selectors (+) सिर्फ trigger के ठीक बगल में स्थित single element को match करते हैं।
- excessively लंबी, fragile compound selector chains design करना जो HTML structure update करने पर आसानी से टूट जाती हैं।
- Combinators HTML family tree structure में target elements की relationships के आधार पर उन्हें select करते हैं।
- space selector सभी descendants को target करता है, जबकि > selector किसी specified parent के सिर्फ direct children को target करता है।
- + selector trigger के ठीक बगल में स्थित single adjacent sibling को target करता है, जबकि ~ selector उसके बाद कहीं भी स्थित सभी sibling elements को target करता है।
Standard CSS combinators (+, >, ~) हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline