OnlineCBT Logo OnlineCBT
CSS Tutorial
CSS · LESSON 11

Color Models: Named, HEX, RGB, HSL, and Alpha Channels

By Ratan Updated: 15 Oct 2026

Work with modern CSS colors including RGBA and HSLA for opacity and vibrant palettes.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Represent color and transparency deliberately

CSS supports named colors and numeric color notations. Hex and rgb describe red, green and blue channels; hsl expresses hue, saturation and lightness. Alpha controls transparency. Choosing another notation does not automatically make a color more accessible.

.named { color: navy; }
.hex { color: #1d4ed8; }
.rgb { color: rgb(29 78 216); }
.hsl { color: hsl(224 76% 48%); }
.overlay { background: rgb(15 23 42 / 0.7); }

The hex and rgb examples specify the same RGB channel values. The hsl example is a nearby blue, not a promise of an exact conversion. The overlay's 0.7 alpha means its final appearance depends on the content behind it.

Alpha versus opacity

An alpha color changes that paint's transparency. The opacity property composites the entire element, including its descendants, as a transparent group. Setting opacity:0.5 on a card also fades its text; a translucent background color can leave text fully opaque.

Readable interfaces

Test text against the background it actually appears on, including hover, disabled, dark and translucent states. Small light-gray text on white can be unreadable even when it looks refined. Do not use color alone to distinguish correct/wrong answers; add labels or icons with an accessible explanation.

Current color

.badge {
  color: #166534;
  border: 1px solid currentColor;
}

currentColor uses the element's computed color value. It helps borders and icons follow a component's theme without repeating the same literal color.

Practice

  1. Build success, warning and error messages with readable foreground/background pairs and explicit text labels.
  2. Compare a card with opacity:0.5 against one using only a 50%-alpha background.
  3. Change .badge's color and confirm its border follows automatically.
  4. Place a translucent overlay over both a light and dark image area and check legibility.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat