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

CSS रंग

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

रंग वह पहली चीज़ है जो visitor पेज पर देखता है, और CSS आपको रंग बताने के कई तरीके देता है। सभी का नतीजा एक ही होता है — screen पर एक रंग — लेकिन कुछ तरीके पढ़ने में आसान हैं, कुछ बदलने में आसान हैं, और एक तरीका रंग को आर-पार भी बना देता है।

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

रंग के formats

Formatउदाहरणमतलब
Named colourred140 built-in नामों में से एक, जैसे red, tomato, skyblue।
HEX#ff0000लाल, हरा और नीला — हर एक दो hex digits में: #RRGGBB।
RGBrgb(255, 0, 0)लाल, हरा और नीला, हर एक 0 से 255 तक।
RGBArgba(255, 0, 0, 0.5)RGB + alpha — 0 पूरी तरह आर-पार, 1 पूरी तरह ठोस।
HSLhsl(0, 100%, 50%)Hue (0–360°), saturation, lightness — जैसे इंसान रंग के बारे में सोचता है।
HSLAhsla(0, 100%, 50%, 0.5)HSL + alpha, RGBA जैसा ही विचार।

सिंटैक्स — एक ही लाल रंग, छह तरीके

h1 { color: red; }
h2 { color: #ff0000; }
h3 { color: rgb(255, 0, 0); }
h4 { color: rgba(255, 0, 0, 0.5); }
h5 { color: hsl(0, 100%, 50%); }
h6 { color: hsla(0, 100%, 50%, 0.5); }

छहों headings लाल हैं। फ़र्क सिर्फ इतना है कि आप उसे कैसे बताते हैं। नाम पढ़ने में सबसे आसान हैं। HEX असली projects में सबसे आम है — designers और tools सभी HEX बोलते हैं। RGB सबसे साफ़ है जब आपको shade निकालना हो। HSL सबसे आसान है जब आप कहना चाहें "वही रंग, पर हल्का" — बस lightness का number बढ़ा दीजिए।

Note: HEX का छोटा रूप भी होता है। जब हर जोड़े के दोनों digits एक जैसे हों, तो आप हर एक में एक digit लिख सकते हैं: #ff0000 बन जाता है #f00, #ffffff बन जाता है #fff, #000000 बन जाता है #000। दोनों रूपों का मतलब बिल्कुल एक ही रंग है।

Opacity — आर-पार elements

opacity property पूरे element को आर-पार बना देती है, 0 (अदृश्य) से 1 (पूरी तरह ठोस) तक। यह RGBA से अलग है: RGBA सिर्फ रंग को आर-पार बनाता है, जबकि opacity पूरे element को — text, पृष्ठभूमि और उसके अंदर की हर चीज़ को — आर-पार बना देता है।

.faded { opacity: 0.5; }
.glass { background-color: rgba(255, 255, 255, 0.5); }

.faded box और उसका सारा text आधा पारदर्शी हो जाता है। .glass box को आधी पारदर्शी सफ़ेद पृष्ठभूमि मिलती है, लेकिन उसका text पूरी तरह ठोस और पढ़ने में आसान रहता है। जब आपको आर-पार पृष्ठभूमि के साथ पढ़ने लायक text चाहिए, तो RGBA लगभग हमेशा सही चुनाव है।

Tip: एक छोटी palette चुनिए और उसी पर टिके रहिए — एक मुख्य रंग, एक accent रंग, text के लिए एक गहरा रंग, और पृष्ठभूमि के लिए एक हल्का रंग। तीन या चार रंगों वाली वेबसाइटें professional लगती हैं; पंद्रह रंगों वाली वेबसाइटें गलती जैसी लगती हैं।

आम गलतियाँ

  • HEX रंग में # भूल जाना — ff0000 CSS के लिए रंग नहीं है। यह #ff0000 होना चाहिए।
  • opacity को RGBA से उलझा देना — opacity text को भी फीका कर देती है, इसलिए फीका box पढ़ने में मुश्किल हो सकता है।
  • Alpha की value 1 से ज़्यादा लिखना — alpha 0 से 1 तक चलता है, 0 से 100 तक नहीं। rgba(255, 0, 0, 50) गलत है।
  • RGB की value 255 से ज़्यादा लिखना — हर channel 255 पर रुकता है। rgb(300, 0, 0) गलत है।
  • ऐसे रंग चुनना जो एक-दूसरे के बहुत करीब हों — सफ़ेद पृष्ठभूमि पर हल्का भूरा text पढ़ा नहीं जाता, code में चाहे कितना भी सुंदर लगे।

याद रखें

  • color text का रंग तय करता है; background-color पृष्ठभूमि का रंग तय करता है।
  • HEX है #RRGGBB — #ff0000 लाल है; छोटा रूप #f00 भी वही है।
  • RGB हर channel में 0–255 है; RGBA में alpha जुड़ता है, 0 (आर-पार) से 1 (ठोस) तक।
  • HSL रंग को hue, saturation और lightness से बताता है — हल्का या गहरा version बनाने का सबसे आसान तरीका।
  • opacity text समेत पूरे element को फीका करती है; RGBA सिर्फ रंग को फीका करता है।

उदाहरण समझिए

  • .named { background-color: tomato; } — नाम वाला रंग। Box टमाटर-लाल हो जाता है, बिना किसी number के।
  • .hex { background-color: #1e90ff; } — HEX रंग। Hash और छह digits एक चमकीला dodger नीला बताते हैं।
  • .rgb { background-color: rgb(34, 139, 34); } — RGB numbers। लाल 34, हरा 139, नीला 34 मिलकर forest green बनाते हैं।
  • .see-through { background-color: rgba(255, 165, 0, 0.5); } — RGBA। आधी ताकत वाला नारंगी, इसलिए उसके पीछे का पेज पृष्ठभूमि से झाँकता है — लेकिन text ठोस रहता है।
  • .faded { opacity: 0.5; } — पूरे box पर opacity। पृष्ठभूमि और text दोनों आधे फीके हो जाते हैं, इसीलिए text धुला हुआ लगता है।

उदाहरण

Example
<!DOCTYPE html>
<html>
<head>
<title>CSS Colors</title>
<style>
body { font-family: Verdana, sans-serif; }
.box { color: white; padding: 14px; margin: 8px; }
.named { background-color: tomato; }
.hex { background-color: #1e90ff; }
.rgb { background-color: rgb(34, 139, 34); }
.see-through { background-color: rgba(255, 165, 0, 0.5); color: black; }
.faded { background-color: purple; opacity: 0.5; }
</style>
</head>
<body>

<h1>Our Colour Palette</h1>
<div class="box named">Named colour — tomato</div>
<div class="box hex">HEX colour — #1e90ff</div>
<div class="box rgb">RGB colour — rgb(34, 139, 34)</div>
<div class="box see-through">RGBA — orange at half strength, text stays solid</div>
<div class="box faded">Opacity 0.5 — background and text both fade</div>

</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 colour is #ff0000?

HEX is #RRGGBB — #ff0000 is red 255, green 0, blue 0.

2. What is the short form of #ff0000?

When the two digits of each pair are the same, you can write one digit each — #ff0000 becomes #f00.

3. In rgba(255, 0, 0, 0.5), what does 0.5 mean?

Alpha runs from 0 (fully see-through) to 1 (fully solid) — 0.5 is half see-through.

4. What is the difference between opacity and RGBA?

opacity makes the entire element — text and all — see-through. RGBA makes only the colour see-through, so the text stays solid and readable.

5. What is the valid range for each RGB channel?

Each RGB channel runs from 0 to 255 — rgb(300, 0, 0) is invalid.

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 colour formats does CSS offer, and which one is most common?

Named colours (red, tomato — 140 built-in names), HEX (#ff0000 — red, green and blue as two hex digits each), RGB (rgb(255, 0, 0), each channel 0 to 255), RGBA (RGB plus an alpha for transparency), and HSL/HSLA (hue, saturation, lightness). HEX is the most common in real projects — designers and tools all speak HEX.

2. What is the difference between opacity and RGBA?

opacity makes the whole element see-through — text, background and everything inside it, from 0 (invisible) to 1 (fully solid). RGBA makes only the colour see-through; the text stays fully solid and easy to read. When you want a see-through background with readable text, RGBA is almost always the right choice.

3. What is wrong with the colour rgb(300, 0, 0)?

Each RGB channel runs from 0 to 255, so 300 is out of range and the value is invalid. The same applies to alpha: it runs from 0 to 1, not 0 to 100, so rgba(255, 0, 0, 50) is also invalid. And a HEX colour needs its hash — ff0000 without the # is not a colour to CSS.

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. Build a colour palette

Create a page called "Our Colour Palette" with five boxes, one for each colour format: a named colour (tomato), a HEX colour (#1e90ff), an RGB colour (rgb(34, 139, 34)), an RGBA colour (rgba(255, 165, 0, 0.5) with black text so it stays readable), and a purple box with opacity: 0.5 (background and text both fade). Give every box the class "box" with white text, 14px padding and 8px margin, and give the body a Verdana font.

Hint: Write one .box rule for the shared styles, then one rule per colour: .named, .hex, .rgb, .see-through, .faded. HEX needs the # in front. RGBA alpha is 0 to 1. Opacity fades the whole box including its text — that is the point of the last box.
Open the live editor
Show solution
<!DOCTYPE html>
<html>
<head>
<title>CSS Colors</title>
<style>
body { font-family: Verdana, sans-serif; }
.box { color: white; padding: 14px; margin: 8px; }
.named { background-color: tomato; }
.hex { background-color: #1e90ff; }
.rgb { background-color: rgb(34, 139, 34); }
.see-through { background-color: rgba(255, 165, 0, 0.5); color: black; }
.faded { background-color: purple; opacity: 0.5; }
</style>
</head>
<body>

<h1>Our Colour Palette</h1>
<div class="box named">Named colour — tomato</div>
<div class="box hex">HEX colour — #1e90ff</div>
<div class="box rgb">RGB colour — rgb(34, 139, 34)</div>
<div class="box see-through">RGBA — orange at half strength, text stays solid</div>
<div class="box faded">Opacity 0.5 — background and text both fade</div>

</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.

Advanced Cyber Security Industrial Training
₹5,000
View
Ethical Hacking & Cyber Security Workshop
View
AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
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 →