Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Welcome to Chapter 3! 🎨
Colors give life, emotion, and personality to a website. A medical website might use calm blues and teals, a school sports portal might burst with energetic reds and oranges, while an eco-friendly nature blog shines with lush greens.
In CSS, you have 4 standard ways to pick and declare colors:
crimson, navy, gold)#ff0000, #2563eb)rgb(37, 99, 235), rgba(37, 99, 235, 0.5))hsl(217, 83%, 53%))Let us explore each format using a fun watercolor palette analogy!
Imagine you are sitting in your school art class with an empty white paper and a paint palette:
How data and code flow step-by-step
CSS comes with 140 built-in color names that modern browsers understand directly. You can simply write the name as the value:
crimson, coral, tomato, hotpinknavy, royalblue, deepskyblue, steelblueforestgreen, seagreen, lime, mediumseagreenwhitesmoke, slategray, darkslategray, goldWhile color names are easy to remember, you cannot fine-tune the shade. If royalblue is slightly too dark for your school logo, you cannot adjust it using color names. For exact branding, professionals use HEX or RGB.
A HEX color code is the most widely used color format on the web. It always begins with a hash symbol (#) followed by 6 characters representing three color channels:
In the hexadecimal number system, numbers count from 0 to 9, and then use letters A to F (where A = 10 and F = 15).
00 means zero intensity (completely dark / off).FF means maximum intensity (full blast: 255).If each pair of characters has matching digits, you can shorten it to 3 digits:
#ffffff can be written as #fff#000000 can be written as #000#ff0000 can be written as #f00Every computer screen and phone display produces color by shining tiny Red, Green, and Blue light sub-pixels.
rgb(red, green, blue) takes 3 values ranging from 0 to 255:
What if you want a color to be semi-transparent (like tinted sunglasses)? You add a 4th value called Alpha (a): rgba(red, green, blue, alpha)
alpha is a decimal number between 0 and 1:0 = completely invisible (100% transparent)0.5 = half transparent (50% see-through)1 = completely solid (opaque)opacity: 0.5;, the entire box AND all the text inside it becomes see-through!
If you use background-color: rgba(...);, only the background is see-through, keeping your text 100% crisp and readable!Many designers consider HSL the easiest color format to understand because it matches human intuition rather than computer sub-pixels!
hsl(hue, saturation, lightness)
+-------------------------------------------------------------------------+ | UNDERSTANDING HSL | +-------------------------------------------------------------------------+ | 1. HUE (0 to 360 degrees on the color wheel): | | 0° / 360° = Red 120° = Green 240° = Blue | | 60° = Yellow 180° = Cyan 300° = Magenta | | | | 2. SATURATION (0% to 100%): | | 0% = Dull gray (no color) 100% = Full vibrant vivid color | | | | 3. LIGHTNESS (0% to 100%): | | 0% = Completely Black 100% = Completely White | | 50% = Perfect normal color shade | +-------------------------------------------------------------------------+
Notice how easy it is to create darker or lighter shades with HSL: you only need to change the Lightness percentage without recalculating any hex numbers!
| Format | Syntax Example | When to Use | Transparency Support |
|---|---|---|---|
| Color Name | color: crimson; | Quick tests, prototypes, basic colors | ❌ No |
| HEX Code | color: #2563eb; | Most common on the web, brand style guides | ✅ Yes (#2563eb80) |
| RGB / RGBA | color: rgba(37, 99, 235, 0.5); | When creating translucent backgrounds & overlays | ✅ Yes (alpha) |
| HSL / HSLA | color: hsl(217, 83%, 53%); | Dynamic color themes, hover states (tints & shades) | ✅ Yes (hsla) |
| What Beginners Do (Don't ❌) | What You Should Do Instead (Do ✅) | Why? |
|---|---|---|
Forgetting the hash: color: 2563eb; | Always include #: color: #2563eb; | Without #, the browser does not recognize it as a HEX code. |
Using opacity: 0.5 on a card with text. | Use background-color: rgba(...). | opacity makes the text inside hard to read; rgba keeps text solid. |
Writing RGB values greater than 255: rgb(300, 0, 0). | Keep values between 0 and 255: rgb(255, 0, 0). | 255 is the maximum allowed integer for standard 8-bit color channels. |
#RRGGBB with values from 00 (darkest) to FF (brightest).0 to 255.0 to 1) to control transparency without fading text.Test your understanding with these multiple-choice questions:
#00FF00, which color channel is set to maximum intensity?rgb() function?rgba() color format?background-color: rgba(0, 0, 0, 0.5) usually preferred over opacity: 0.5 for card backgrounds?palette.html..color-hsl from 50% to 30% (dark purple) and then 80% (light pastel lavender) to see how intuitive HSL is! 🎯In the next lesson, we will continue your web styling journey with Background Color, Image, Position, and Size (Colors and Backgrounds).
👉 Continue to Next Lesson: Background Color, Image, Position, and Size →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Grouping and Combinator Selectors | Background Color, Image, Position, and Size |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.