CSS Selectors क्या हैं?

 CSS में Selectors का उपयोग HTML Elements को चुनने (Select) करने के लिए किया जाता है ताकि उन Elements पर Styling लागू की जा सके।
Selectors CSS का सबसे महत्वपूर्ण भाग होते हैं क्योंकि इनके बिना हम किसी HTML Element पर Style लागू नहीं कर सकते।

सरल शब्दों में:

CSS Selector यह तय करता है कि कौन सा HTML Element Style होगा।

उदाहरण:

p{
   color:red;
}

ऊपर दिए गए उदाहरण में p एक Selector है जो सभी Paragraph Tags को Select कर रहा है।

CSS Selector का Syntax

selector{
   property:value;
}

उदाहरण:

h1{
   color:blue;
   font-size:40px;
}

यहाँ:

  • h1 = Selector
  • color = Property
  • blue = Value

CSS Selectors क्यों जरूरी हैं?

CSS Selectors की मदद से:

  • Multiple Elements को Style कर सकते हैं
  • Specific Elements को Target कर सकते हैं
  • Website Design बेहतर बना सकते हैं
  • Responsive Design बना सकते हैं
  • Clean और Reusable Code लिख सकते हैं

CSS Selectors के प्रकार

CSS में कई प्रकार के Selectors होते हैं:

  1. Element Selector
  2. ID Selector
  3. Class Selector
  4. Universal Selector
  5. Group Selector
  6. Descendant Selector
  7. Child Selector
  8. Adjacent Sibling Selector
  9. General Sibling Selector
  10. Attribute Selector
  11. Pseudo Class Selector
  12. Pseudo Element Selector

अब हम सभी Selectors को विस्तार से समझेंगे।

1. Element Selector

Element Selector किसी HTML Tag को Select करता है।

Example

<p>This is paragraph</p>
p{
   color:red;
}

Output

सभी Paragraph Red Color में दिखाई देंगे।

Multiple Element Selector Example

h1{
   color:blue;
}

p{
   color:green;
}

2. ID Selector

ID Selector किसी Unique Element को Select करता है।

ID हमेशा Unique होनी चाहिए।

Syntax

#idname{
   property:value;
}

Example

<h1 id="title">Welcome</h1>
#title{
   color:red;
}

ID Selector की विशेषताएं

  • एक Page में केवल एक बार उपयोग करें
  • # Symbol से शुरू होता है
  • High Priority होती है

3. Class Selector

Class Selector Multiple Elements को Select कर सकता है।

Syntax

.classname{
   property:value;
}

Example

<p class="text">Paragraph One</p>
<p class="text">Paragraph Two</p>
.text{
   color:blue;
}

Class Selector के फायदे

  • Reusable होता है
  • Multiple Elements पर Apply होता है
  • Responsive Design में उपयोगी

4. Universal Selector

Universal Selector सभी Elements को Select करता है।

Syntax

*{
   property:value;
}

Example

*{
   margin:0;
   padding:0;
}

Universal Selector का उपयोग

यह Reset CSS में बहुत उपयोग किया जाता है।

5. Group Selector

एक साथ Multiple Elements को Select करने के लिए उपयोग किया जाता है।

Example

h1,p,div{
   color:green;
}

Group Selector के फायदे

  • Code छोटा होता है
  • Time बचता है
  • Repetition कम होती है

6. Descendant Selector

Parent Element के अंदर मौजूद Child Elements को Select करता है।

Syntax

parent child{
   property:value;
}

Example

<div>
   <p>Hello</p>
</div>
div p{
   color:red;
}

Descendant Selector की विशेषताएं

  • Nested Elements को Target करता है
  • Deep Level Elements को भी Select कर सकता है

7. Child Selector

Direct Child को Select करता है।

Syntax

parent > child{
   property:value;
}

Example

div > p{
   color:blue;
}

Child Selector और Descendant Selector में अंतर

Descendant Selector Child Selector
सभी अंदर के Elements को Select करता है केवल Direct Child को Select करता है

8. Adjacent Sibling Selector

किसी Element के तुरंत बाद वाले Sibling को Select करता है।

Syntax

element + element{
   property:value;
}

Example

h1 + p{
   color:red;
}

9. General Sibling Selector

सभी Sibling Elements को Select करता है।

Syntax

element ~ element{
   property:value;
}

Example

h1 ~ p{
   color:green;
}

10. Attribute Selector

Attribute के आधार पर Element Select करता है।

Syntax

[attribute]{
   property:value;
}

Example

<input type="text">
[type]{
   border:2px solid red;
}

Specific Attribute Value Selector

[type="text"]{
   background:yellow;
}

Attribute Selector के प्रकार

Starts With

a[href^="https"]

Ends With

a[href$=".pdf"]

Contains

a[href*="google"]

11. Pseudo Class Selector

Special State को Select करता है।

Syntax

selector:pseudo-class{
   property:value;
}

Hover Pseudo Class

button:hover{
   background:red;
}

जब Mouse Button पर जाएगा तब Color बदलेगा।

Common Pseudo Classes

Pseudo Class उपयोग
:hover Mouse Hover
:focus Input Focus
:first-child पहला Child
:last-child अंतिम Child
:nth-child() Specific Child
:checked Checked Input
:disabled Disabled Input

first-child Example

p:first-child{
   color:red;
}

nth-child Example

li:nth-child(2){
   color:blue;
}

Even और Odd Example

tr:nth-child(even){
   background:#eee;
}

tr:nth-child(odd){
   background:#ccc;
}

12. Pseudo Element Selector

Element के Specific Part को Select करता है।

Syntax

selector::pseudo-element{
   property:value;
}

Common Pseudo Elements

Pseudo Element उपयोग
::before Content Add करना
::after Content Add करना
::first-letter पहला अक्षर
::first-line पहली लाइन
::selection Selected Text

before Example

p::before{
   content:"Note:";
}

after Example

p::after{
   content:"✔";
}

first-letter Example

p::first-letter{
   font-size:40px;
   color:red;
}

CSS Combinators

CSS में Combinators Elements के बीच Relationship दिखाते हैं।

Combinator अर्थ
Space Descendant
> Child
+ Adjacent Sibling
~ General Sibling

Advanced CSS Selectors

अब हम कुछ Advanced Selectors समझते हैं।

Multiple Class Selector

.btn.primary{
   background:blue;
}

Nested Selector

nav ul li a{
   color:red;
}

Not Selector

p:not(.test){
   color:black;
}

Empty Selector

div:empty{
   display:none;
}

Target Selector

:target{
   background:yellow;
}

Language Selector

p:lang(en){
   color:blue;
}

CSS Specificity क्या है?

जब Multiple Styles एक ही Element पर Apply होती हैं तब Browser तय करता है कि कौन सी Style लागू होगी।

इसे Specificity कहते हैं।

Specificity Priority

Selector Priority
Inline CSS Highest
ID Selector High
Class Selector Medium
Element Selector Low

Example

p{
   color:red;
}

.text{
   color:blue;
}

#title{
   color:green;
}

यदि सभी एक Element पर लागू हों तो Green Color दिखाई देगा क्योंकि ID Selector की Priority अधिक होती है।

CSS Selector Best Practices

1. Meaningful Class Names उपयोग करें

गलत:

.a1{}

सही:

.header-title{}

2. Unnecessary Nesting Avoid करें

गलत:

div ul li a span{}

3. Reusable Classes बनाएं

.btn{}
.card{}
.container{}

4. ID का अधिक उपयोग न करें

Class Selector अधिक Flexible होता है।

5. CSS को Organized रखें

  • Comments उपयोग करें
  • Sections बनाएं
  • Proper Indentation रखें

Real Project Example

HTML

<div class="card">
   <h2>CSS Tutorial</h2>
   <p>Learn CSS Selectors</p>
   <button>Read More</button>
</div>

CSS

.card{
   border:1px solid #ccc;
   padding:20px;
}

.card h2{
   color:blue;
}

.card p{
   color:#444;
}

.card button:hover{
   background:black;
   color:white;
}

CSS Selectors सीखने के फायदे

  • Professional Website Design
  • Better User Interface
  • Faster Development
  • Clean Coding
  • Responsive Websites
  • Advanced Styling Control

Beginners द्वारा की जाने वाली गलतियां

1. गलत Selector उपयोग करना

#box{}

जबकि HTML में:

<div class="box"></div>

2. Over Specificity

बहुत ज्यादा Nested Selectors उपयोग करना।

3. Duplicate Classes बनाना

एक जैसी Styling बार-बार लिखना।

4. Universal Selector का अधिक उपयोग

यह Performance को प्रभावित कर सकता है।

Interview Questions on CSS Selectors

CSS Selector क्या होता है?

CSS Selector HTML Elements को Select करने के लिए उपयोग होता है।

ID और Class Selector में अंतर क्या है?

ID Class
Unique Multiple Use
# Symbol . Symbol

Pseudo Class और Pseudo Element में अंतर

Pseudo Class Pseudo Element
State Select करता है Part Select करता है

CSS Selectors का Future

Modern CSS में नए Selectors लगातार आ रहे हैं जैसे:

  • :is()
  • :where()
  • :has()

ये Selectors CSS को और Powerful बना रहे हैं।

:is() Example

:is(h1,h2,h3){
   color:red;
}

:where() Example

:where(section,article){
   margin:20px;
}

:has() Example

div:has(img){
   border:2px solid red;
}

Conclusion

CSS Selectors वे Tools हैं जिनकी मदद से हम HTML Elements को Target करके Styling लागू करते हैं। यदि आप Web Development सीखना चाहते हैं तो CSS Selectors की अच्छी समझ होना बहुत जरूरी है।

इस Guide में हमने Basic से लेकर Advanced CSS Selectors तक सभी Concepts को विस्तार से समझा। यदि आप नियमित Practice करेंगे तो आसानी से Professional CSS Coding कर पाएंगे।

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *