SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
पाठ 2, कुल 30 में

CSS सिंटैक्स

4 मिनट पढ़ें · मुफ़्त · बिना लॉगिन

हर CSS नियम एक ही आकार का होता है। एक बार यह आकार सीख लिया, तो आप दुनिया का कोई भी CSS पढ़ सकते हैं, क्योंकि हर नियम — सबसे साधारण रंग बदलने से लेकर पूरे पेज की बनावट तक — उन्हीं टुकड़ों से बना होता है।

Diagram — देखें, यह सब कैसे जुड़ता है
Diagram: CSS सिंटैक्स

नियम के हिस्से

नियम के दो हिस्से होते हैं। पहले आता है selector — यह बताता है कि पेज के किस हिस्से को सजाना है। फिर आता है declaration block, घुँघराले कोष्ठकों { } के अंदर — यह बताता है कि वह हिस्सा कैसा दिखना चाहिए।

कोष्ठकों के अंदर declarations होते हैं। हर declaration में एक property होती है (क्या बदलना है, जैसे color) और एक value होती है (किसमें बदलना है, जैसे red)। Property और value colon : से अलग होते हैं, और हर declaration semicolon ; पर खत्म होता है।

सिंटैक्स — नियम का आकार

selector {
  property: value;
  property: value;
}

यहाँ एक असली नियम है, जिसका हर हिस्सा बताया गया है:

p {
  color: darkblue;
  font-size: 18px;
}

Selector है p — पेज का हर paragraph। पहला declaration, color: darkblue;, text को गहरा नीला बनाता है। दूसरा declaration, font-size: 18px;, text को 18 pixel ऊँचा बनाता है। हर declaration के बाद semicolon browser को बताता है कि "यह निर्देश खत्म हुआ, अगला यहाँ से शुरू होता है।"

Note: CSS को spaces और line breaks से कोई फर्क नहीं पड़ता। p{color:red;} और साफ़-सुथरे तरीके से लिखा version browser के लिए बिल्कुल एक ही मतलब रखते हैं। लेकिन इंसान spaced version को कहीं ज़्यादा आसानी से पढ़ता है — इसलिए हमेशा एक declaration एक line में लिखें, और हर colon के बाद एक space दें।
Tip: Block के आखिरी declaration को सख्ती से semicolon की ज़रूरत नहीं होती, क्योंकि closing brace उसे खत्म कर देता है। फिर भी उसे लिखिए। जिस दिन आप उसके नीचे एक और declaration जोड़ेंगे और semicolon भूल जाएँगे, नया वाला चुपचाप टूट जाएगा — और आप दस मिनट तक सोचते रहेंगे कि क्यों।

CSS में Comments

कभी-कभी आप code में अपने लिए एक note छोड़ना चाहते हैं — एक ऐसा note जिसे browser को नज़रअंदाज़ करना है। CSS comments /* से शुरू होते हैं और */ पर खत्म होते हैं। इनके बीच जो कुछ भी है, वह नज़रअंदाज़ कर दिया जाता है, चाहे वह कई lines में ही क्यों न हो।

/* This rule styles the main heading */
h1 {
  color: green; /* green means "fresh" on our site */
}

Comments मुफ्त होते हैं: उनकी कोई कीमत नहीं होती, और वे भविष्य के आपको बहुत उलझन से बचाते हैं। उनका इस्तेमाल यह समझाने के लिए कीजिए कि नियम क्यों है, न कि क्या करता है — code पहले से ही बता देता है कि वह क्या करता है।

आम गलतियाँ

  • Property और value के बीच colon भूल जाना — color red; कुछ नहीं करता। यह color: red; होना चाहिए।
  • Semicolon भूल जाना — अगला declaration पिछले वाले से चिपक जाता है और दोनों टूट जाते हैं।
  • Closing brace } भूल जाना — उसके बाद के हर नियम काम करना बंद कर देते हैं, क्योंकि browser सोचता है कि वह अभी भी पहले नियम के अंदर है।
  • : की जगह = इस्तेमाल करना — CSS में colon चलता है, equals sign नहीं। color = red CSS नहीं है।
  • Property के नाम capital letters में लिखना — CSS के लिए Color और color अलग हैं। हमेशा lowercase इस्तेमाल करें।

याद रखें

  • नियम है selector { property: value; } — selector चुनता है, declarations सजाते हैं।
  • Colon property और value को अलग करता है; semicolon हर declaration को खत्म करता है।
  • घुँघराले कोष्ठक { } declaration block को घेरते हैं — हर opening brace को closing brace चाहिए।
  • Comments /* this */ जैसे दिखते हैं और browser उन्हें नज़रअंदाज़ कर देता है।
  • Spacing और line breaks से मतलब नहीं बदलता — उन्हें code को पढ़ने लायक बनाने के लिए इस्तेमाल करें।

उदाहरण समझिए

  • h1 { color: darkgreen; } — एक नियम, एक declaration। पेज का हर h1 गहरा हरा हो जाता है।
  • p { color: #333; font-size: 18px; line-height: 1.6; } — एक नियम, तीन declarations। सभी paragraphs को गहरा भूरा text, 18 pixel ऊँचाई और lines के बीच अच्छी जगह मिलती है।
  • .highlight { background-color: yellow; } — dot का मतलब है "class"। सिर्फ class="highlight" वाले elements को पीली पृष्ठभूमि मिलती है।
  • #special { font-weight: bold; } — hash का मतलब है "id"। सिर्फ id="special" वाला एक element bold बनता है।
  • /* Welcome message */ — एक comment। Browser इसे पूरी तरह नज़रअंदाज़ कर देता है; यह सिर्फ code पढ़ने वाले इंसान के लिए है।

उदाहरण

Example
<!DOCTYPE html>
<html>
<head>
<title>CSS Syntax</title>
<style>
/* Welcome message */
h1 { color: darkgreen; }
p { color: #333; font-size: 18px; line-height: 1.6; }
.highlight { background-color: yellow; }
#special { font-weight: bold; }
</style>
</head>
<body>

<h1>Welcome to KAMALDNP Academy</h1>
<p>We teach HTML, CSS and JavaScript in simple words.</p>
<p class="highlight">Admissions are open for the new batch.</p>
<p id="special">Call us today to book your seat.</p>

</body>
</html>

परिणाम

परिणाम — ब्राउज़र में यह कैसा दिखता है
परिणाम: CSS सिंटैक्स

खुद करके देखें — लाइव एडिटर

नीचे दिया गया कोड बदलें और चलाएँ दबाएँ — परिणाम तुरंत दिखेगा। कोई फाइल सहेजनी नहीं है, कोई लॉगिन नहीं चाहिए।

परिणाम

Check yourself — did you get it?

Finished the lesson? Test yourself with 5 quick questions — you get the answer instantly, with the reason for each one.

1. What separates a property from its value in a declaration?

The property and the value are separated by a colon — color: red;.

2. What ends each declaration?

Each declaration ends with a semicolon — it tells the browser "this instruction is finished, the next one starts here".

3. What does /* ... */ mean in CSS?

CSS comments start with /* and end with */ — anything between them is ignored by the browser, even across many lines.

4. What happens if you forget a closing brace }?

Without the closing brace, the browser thinks it is still inside the first rule, so every rule after it stops working.

5. Which of these is a correct declaration?

CSS uses a colon, not an equals sign — and property names are always lowercase: color: red;.

Interview questions — can you answer these?

Read the question first, try to answer it in your own words, then open it to check. These are the questions interviewers actually ask.

1. What are the parts of a CSS rule?

A selector and a declaration block. The selector picks which part of the page to style, and the declaration block — inside curly braces { } — holds the declarations. Each declaration is a property and a value, separated by a colon and ended with a semicolon: selector { property: value; }.

2. What does the semicolon do in CSS, and what happens if you forget it?

The semicolon ends one declaration and tells the browser the next one starts. If you forget it, the next declaration gets glued to the previous one and both break. The last declaration in a block does not strictly need one, but write it anyway — the day you add another declaration below it, the new one will silently break.

3. How do you write a comment in CSS?

With /* and */. Anything between them is ignored by the browser, even across many lines. Comments cost nothing and save future-you a lot of confusion — use them to explain why a rule exists, not what it does, because the code already says what it does.

Try it yourself — build this

Reading is not enough — now build it yourself. Write the code in the live editor, run it, check the hint if you get stuck, and open the solution only after you have tried.

1. Write a clean rule

Create a page with a <style> block that has four rules: h1 in dark green, p in dark grey (#333) with font-size 18px and line-height 1.6, a .highlight class with a yellow background, and a #special id that is bold. Add one CSS comment /* Welcome message */ above the first rule. Remember: colon between property and value, semicolon after every declaration, and every { needs a }.

Hint: One declaration per line, with a space after each colon. The class selector starts with a dot (.highlight), the id selector starts with a hash (#special). The comment /* ... */ goes before the rule it explains.
Open the live editor
Show solution
<!DOCTYPE html>
<html>
<head>
<title>CSS Syntax</title>
<style>
/* Welcome message */
h1 { color: darkgreen; }
p { color: #333; font-size: 18px; line-height: 1.6; }
.highlight { background-color: yellow; }
#special { font-weight: bold; }
</style>
</head>
<body>

<h1>Welcome to KAMALDNP Academy</h1>
<p>We teach HTML, CSS and JavaScript in simple words.</p>
<p class="highlight">Admissions are open for the new batch.</p>
<p id="special">Call us today to book your seat.</p>

</body>
</html>

खुद क्यों सीखें — AI से पूछने के बजाय?

AI आपके लिए कोड लिख सकता है, लेकिन वह आपके लिए सोच नहीं सकता। जब आप HTML खुद सीखते हैं, तो आपका दिमाग असली कौशल बनाता है: आप समझते हैं कि पेज ऐसा क्यों दिखता है, टूटने पर उसे ठीक कर सकते हैं, और परीक्षा या इंटरव्यू में बिना मदद के जवाब दे सकते हैं। यही कौशल नौकरी दिलाता है — कॉपी किया हुआ जवाब नहीं।

सर्टिफिकेट पाएँ — सिर्फ ₹10

सभी 30 पाठ पूरे करें, टेस्ट पास करें (80%), ₹10 दें और अपना सर्टिफिकेट पाएँ।

टेस्ट दें

देखकर सीखना पसंद है? वीडियो कोर्स में वही विषय एक-एक करके सिखाए जाते हैं।

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Website Design Using WordPress 3 Days (Basic to Advanced)
₹199
View
Core Python Programming – 3 Month Complete Course
₹700
View
Advanced Cyber Security Industrial Training
₹5,000
View
Browse all courses
CSS — Lessons
1. CSS परिचय 2. CSS सिंटैक्स 3. CSS सेलेक्टर्स 4. CSS कैसे जोड़ें 5. CSS रंग 6. CSS बैकग्राउंड 7. CSS बॉर्डर 8. CSS मार्जिन 9. CSS पैडिंग 10. CSS ऊँचाई और चौड़ाई 11. CSS बॉक्स मॉडल 12. CSS टेक्स्ट 13. CSS फ़ॉन्ट्स 14. CSS लिंक्स 15. CSS लिस्ट्स 16. CSS टेबल्स 17. CSS डिस्प्ले 18. CSS पोज़िशन 19. CSS ओवरफ़्लो 20. CSS फ़्लोट 21. CSS स्यूडो-क्लास 22. CSS स्यूडो-एलिमेंट 23. CSS फॉर्म 24. CSS नेविगेशन बार 25. CSS वेबसाइट लेआउट 26. CSS Flexbox — लचीला लेआउट 27. CSS Grid — दो-आयामी लेआउट 28. CSS Responsive — हर screen पर अच्छा पेज 29. CSS Transitions और Animations 30. CSS Variables — नाम देकर value सहेजो
अंतिम टेस्ट — सर्टिफिकेट ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →