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

CSS कैसे जोड़ें

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

वेब पेज में CSS जोड़ने के तीन तरीके हैं: inline, internal और external। तीनों का काम एक ही है — वे rules को आपके HTML से जोड़ते हैं — लेकिन वे अलग-अलग जगह रहते हैं, और अलग-अलग मौके पर काम आते हैं। असली वेबसाइटें लगभग हमेशा external तरीका इस्तेमाल करती हैं। तीनों सीख लीजिए, फिर सही वाला चुनिए।

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

तीन तरीके

तरीकाकहाँ रहता हैकिसके लिए अच्छा है
Inlineएक element पर style attribute मेंएक element पर एक छोटा-सा तुरंत fix
Internal<head> के अंदर <style> block मेंएक अकेला पेज
Externalअलग .css file में, <link> से जुड़ा हुआपूरी वेबसाइट

सिंटैक्स — तीनों तरीके

Inline: style सीधे element पर बैठता है। यह सिर्फ उसी एक element को प्रभावित करता है।

<h1 style="color: blue;">Hello</h1>

Internal: नियम head के अंदर <style> block में रहते हैं। वे पूरे पेज को प्रभावित करते हैं।

<head>
<style>
  h1 { color: blue; }
</style>
</head>

External: नियम अलग file में रहते हैं, जैसे style.css, और पेज उससे link करता है। एक file सौ पेजों को सजा सकती है।

<head>
<link rel="stylesheet" href="style.css">
</head>
Note: <link> tag <head> के अंदर ही होना चाहिए, और href असली file की ओर इशारा करना चाहिए। अगर path गलत है — एक folder भी गलत — तो पेज बिना किसी styling के लोड होता है, और browser कोई error message भी नहीं देता। जब कोई पेज अचानक सादा दिखने लगे, तो सबसे पहले stylesheet का link जाँचिए।
Tip: पहले दिन से ही अपना CSS अलग file में रखिए, सीखते समय भी। यह छोटी आदत बाद में बड़ी गड़बड़ी से बचाती है: आपका HTML structure के बारे में रहता है, CSS दिखावट के बारे में रहता है, और एक रंग बदलने के लिए आपको पेजों में ढूँढना नहीं पड़ता।

Cascade — कौन-सा नियम जीतता है?

जब दो नियम एक ही element के बारे में असहमत हों, तो browser cascade का पालन करता है — tie-breakers का एक सेट, इस क्रम में:

  1. Inline styles जीतते हैं internal और external styles पर।
  2. बाद वाले नियम जीतते हैं पहले वालों पर, जब वे बराबर मज़बूत हों।
  3. ज़्यादा specific selectors जीतते हैं कम specific वालों पर। Id class को हराता है, class element को हराता है। इसे specificity कहते हैं।

तो #title { color: red; }, .heading { color: blue; } को हराता है, जो h1 { color: green; } को हराता है — भले ही h1 वाला नियम सबसे बाद में आए। Browser हर selector की ताकत गिनता है, सिर्फ क्रम नहीं।

!important — आपातकालीन override

एक चीज़ ऐसी है जो सबको हरा देती है: value के बाद !important जोड़ देना।

p { color: red !important; }

यह नियम inline styles, ids और बाकी सब पर जीतता है। यह आपातकाल के लिए है — जैसे जब आपको किसी ऐसी library का style बदलना हो जिसे आप edit नहीं कर सकते। लेकिन इसका इस्तेमाल लगभग कभी मत कीजिए। एक बार project में !important आ जाए, तो लोग पहले वाले को हराने के लिए और जोड़ने लगते हैं, और जल्द ही कोई नहीं जानता कि कौन-सा नियम जीतता है। !important से भरी stylesheet इस बात का संकेत है कि selectors को ठीक करने की ज़रूरत है, ज़्यादा ज़ोर लगाने की नहीं।

आम गलतियाँ

  • Stylesheet को गलत path से link करना — file का नाम style.css है और आपने href="styles.css" लिखा, या file किसी और folder में है।
  • हर जगह inline styles इस्तेमाल करना — शुरू में तेज़, बाद में सँभालना नामुमकिन। एक रंग बदलने का मतलब है हर element को हाथ से बदलना।
  • यह उम्मीद करना कि internal नियम inline style को हरा देगा — inline हमेशा जीतता है, जब तक internal नियम !important न इस्तेमाल करे।
  • हर नियम पर !important छिड़क देना — यह cascade को खत्म कर देता है और आगे के बदलावों को लड़ाई बना देता है।
  • यह भूल जाना कि बाद वाला नियम तभी जीतता है जब specificity बराबर हो — बाद वाला element selector भी पहले वाले id selector से हार जाता है।

याद रखें

  • Inline = style attribute, internal = head में <style>, external = .css file से <link>।
  • External professional तरीका है — एक file पूरी साइट को सजाती है।
  • Cascade का क्रम: inline जीतता है, फिर ज़्यादा specific selector, फिर बाद वाला नियम।
  • Specificity: id class को हराता है, class element को हराता है।
  • !important सबको हराता है — सिर्फ असली आपातकाल में इस्तेमाल करें।

उदाहरण समझिए

  • h1 { color: green; } — <style> block का internal नियम। यह पेज के हर h1 को हरा कर देता है।
  • <h1 style="color: blue;"> — पहली heading पर inline style। Inline internal नियम पर जीतता है, इसलिए यह heading हरी नहीं, नीली है।
  • <h1>Our Courses</h1> — दूसरी heading पर कोई inline style नहीं है, इसलिए internal नियम लागू होता है और वह हरी है।
  • .note { color: grey; } — उसी internal block का class नियम। यह class="note" वाले paragraph को style करता है।
  • <!-- <link rel="stylesheet" href="style.css"> --> — comment external तरीका दिखाता है। असली साइट में यह line (comment के निशान हटाकर) internal block की जगह shared stylesheet लोड करेगी।

उदाहरण

Example
<!DOCTYPE html>
<html>
<head>
<title>How to Add CSS</title>
<style>
h1 { color: green; }
.note { color: grey; }
</style>
<!-- In a real site, use this instead of the style block above:
<link rel="stylesheet" href="style.css"> -->
</head>
<body>

<h1 style="color: blue;">Welcome to KAMALDNP</h1>
<h1>Our Courses</h1>
<p class="note">The first heading is blue because its inline style beats the internal rule. The second heading is green because only the internal rule applies to it.</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. Which way of adding CSS is best for a whole website?

External CSS lives in a separate .css file linked with <link> — one file can style a hundred pages.

2. Where must the <link> tag for a stylesheet sit?

The <link> tag must sit inside the <head>, and its href must point to the real file.

3. When an inline style and an internal rule disagree, which one wins?

Inline styles win over internal and external styles in the cascade.

4. Which selector wins in a fight: #title, .heading, or h1?

Specificity: an id beats a class, and a class beats an element — #title beats .heading, which beats h1.

5. What does !important do?

Adding !important after a value beats everything — inline styles, ids, all of it. Use it only in a real emergency.

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 three ways to add CSS to a page, and which one should you use for a real website?

Inline (a style attribute on one element), internal (a <style> block in the <head>, for one page), and external (a separate .css file linked with <link>). For a real website, use external — one file styles the whole site, so a colour change in one place updates every page.

2. When two CSS rules disagree, which one wins?

The browser follows the cascade, in this order: inline styles win over internal and external styles; then the more specific selector wins — an id beats a class, a class beats an element (this is specificity); and when two rules are equally specific, the later one wins.

3. What does !important do, and why should you almost never use it?

!important after a value beats everything — inline styles, ids, all of it. It exists for emergencies, like overriding a style from a library you cannot edit. But use it almost never: once it appears, people add more of it to beat the first one, and soon nobody can tell which rule wins. A stylesheet full of !important means the selectors need fixing, not more force.

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. Three ways, one winner

Create a page that shows the three ways to add CSS AND the cascade. Put an internal rule in a <style> block (h1 green, .note grey). Give the first <h1> an inline style that makes it blue — inline beats internal, so it must end up blue while the second <h1> stays green. Then add an HTML comment in the <head> showing the external way: the <link rel="stylesheet" href="style.css"> line, commented out.

Hint: Inline style goes right on the element: <h1 style="color: blue;">. The internal rule h1 { color: green; } styles every h1 that has no inline style. The <link> line goes inside an HTML comment <!-- ... --> so it does not run.
Open the live editor
Show solution
<!DOCTYPE html>
<html>
<head>
<title>How to Add CSS</title>
<style>
h1 { color: green; }
.note { color: grey; }
</style>
<!-- In a real site, use this instead of the style block above:
<link rel="stylesheet" href="style.css"> -->
</head>
<body>

<h1 style="color: blue;">Welcome to KAMALDNP</h1>
<h1>Our Courses</h1>
<p class="note">The first heading is blue because its inline style beats the internal rule. The second heading is green because only the internal rule applies to it.</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.

AI Prompt Engineering Webinar 2026
View
AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
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 →