← Back to CSS Course | Chapter 6: Selectors & Styling | Lesson 1 of 22

CSS Combinators

एक family tree draw करने की कल्पना करें। आपके पास grandparents, parents, children, और siblings हैं। अगर आप inheritance rules छोड़ना चाहें, तो आप कह सकते हैं: 'हर child को एक house मिलेगा' (descendants), 'सिर्फ मेरे direct children को एक car मिलेगी' (child), या 'मेरे छोटे sibling को मेरी watch मिलेगी' (adjacent sibling)। CSS combinators वही family tree relationships हैं। ये आपको इस आधार पर elements target करने देते हैं कि वे आपके page पर दूसरे elements के relative कैसे nested और situated हैं।
Syntax
css
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 होता है।

Note: किसी specific card container के अंदर सभी links या text blocks पर general styles लागू करने के लिए descendant selectors इस्तेमाल करें।
Warning: क्योंकि descendant selectors हर nested match को target करते हैं, ये आसानी से sub-sections में गहराई से nested elements की styles को overwrite कर सकते हैं।

उदाहरण: The Descendant Selector (Space)

css
<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) को नज़रअंदाज़ करते हुए।

Note: किसी dropdown menu के top-level list items को sub-menus को असर किए बिना style करने के लिए child selector इस्तेमाल करें।
Warning: अगर आप बाद में अपने child elements के चारों ओर container wrappers (जैसे एक div) जोड़ते हैं, तो child selector उन्हें target करना बंद कर देगा।

उदाहरण: The Child Selector (>)

css
<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 करता है।

Note: heading tags के ठीक बाद आने वाले paragraphs को style करने के लिए adjacent sibling selectors इस्तेमाल करें।
Warning: अगर आप बाद में अपने target siblings के बीच कोई असंबंधित element (जैसे एक image) डालते हैं, तो adjacent sibling selector match करने में fail हो जाएगा।

उदाहरण: The Adjacent Sibling Selector (+)

css
<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 चाहे जो भी हों।

Note: किसी specific header के नीचे बैठी checkboxes या custom inputs की lists को style करने के लिए general sibling selectors इस्तेमाल करें।
Warning: यह selector सिर्फ specified element के बाद स्थित sibling elements को target करता है; यह पहले वाले siblings को target नहीं कर सकता।

उदाहरण: The General Sibling Selector ()

css
<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 कर सकते हैं।

Note: अपने compound selector paths को जितना हो सके छोटा रखें ताकि stylesheet bloat रोका जा सके और debugging आसान हो।
Warning: बहुत लंबी, specific selector chains fragile होती हैं और अगर आप बाद में अपने HTML structure को थोड़ा भी बदलें तो आसानी से टूट सकती हैं।

उदाहरण: Combining Multiple Selectors

css
<style>
.card > h2 + p {
  color: purple;
}
</style>
<div class="card">
  <h2>Title</h2>
  <p>Styled paragraph</p>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. descendant selectors (space) इस्तेमाल करना जबकि आप सिर्फ direct children को target करना चाहते हैं, जिससे accidental styling leaks होती हैं।
  2. यह भूल जाना कि adjacent sibling selectors (+) सिर्फ trigger के ठीक बगल में स्थित single element को match करते हैं।
  3. 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 हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.