HEX & HSL Color Codes
HEX & HSL Color Codes (Color Like a Pro!) π―
Welcome back! In the previous lesson, you learned how to paint HTML elements using 140 built-in color names and RGB light values.
Now, if you look at modern professional web designsβwhether it is Instagram, YouTube, Flipkart, or Figmaβyou will almost never see developers typing color: Tomato;.
Instead, you will see professional codes like this:
#FF6B00(MSK Vibrant Orange)#0A2540(Deep Navy Blue)hsl(210, 100%, 50%)(Electric Blue)
Why do software engineers and graphic designers love HEX and HSL?
- 1HEX codes are short, compact, and universally supported across every design software and website in the world.
- 2HSL values match how human artists think about colors (Rainbow Hue, Richness, and Brightness) and make it effortless to create lighter and darker shades!
Let's break them down step-by-step in easy English!
1. Demystifying Hexadecimal (HEX) Codes π’
The word Hexadecimal sounds like complicated rocket science, but it is actually very simple:
- Hexa = 6
- Deci = 10
- Together: $6 + 10 = 16$ (The Base-16 number system!)
The Counting Difference:
In school maths, you use the Decimal (Base-10) system. You have 10 single-digit numbers: $$\text{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}$$ Once you reach $9$, you run out of single digits, so you combine two digits to make $10$.
In computer science, Hexadecimal (Base-16) needs 16 single digits. Because we don't have numbers after 9, computers borrow the first six letters of the English alphabet:
| Hex Symbol | Meaning in Normal Maths |
|---|---|
0 to 9 | Numbers 0 to 9 |
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
F | 15 (The Maximum single digit!) |
So in Hexadecimal, the numbers go: $$\text{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}$$
00is the lowest possible two-digit value (equal to $0$).FFis the highest possible two-digit value (equal to $255$ in decimal)!
2. The Structure of a HEX Code: #RRGGBB
A HEX color code always begins with a hash symbol #, followed by 6 characters:
$$\text{\#RRGGBB}$$
- 1
RR(First 2 digits): Red intensity from00(off) toFF(maximum). - 2
GG(Middle 2 digits): Green intensity from00(off) toFF(maximum). - 3
BB(Last 2 digits): Blue intensity from00(off) toFF(maximum).
Let's Look at Familiar HEX Colors:
| Color | HEX Code | Red | Green | Blue | Explanation |
|---|---|---|---|---|---|
| Pure Red | #FF0000 | FF (255) | 00 (0) | 00 (0) | Red at maximum brightness; green & blue off. |
| Pure Green | #00FF00 | 00 (0) | FF (255) | 00 (0) | Green at maximum brightness. |
| Pure Blue | #0000FF | 00 (0) | 00 (0) | FF (255) | Blue at maximum brightness. |
| Pitch Black | #000000 | 00 (0) | 00 (0) | 00 (0) | Zero light everywhere. |
| Pure White | #FFFFFF | FF (255) | FF (255) | FF (255) | All lights at 100% full power. |
| Bright Yellow | #FFFF00 | FF (255) | FF (255) | 00 (0) | Red + Green at full power. |
| MSK Orange | #FF6B00 | FF (255) | 6B (107) | 00 (0) | Signature energetic brand orange. |
| MSK Navy | #0A2540 | 0A (10) | 25 (37) | 40 (64) | Deep, premium institutional blue. |
3-Digit Shorthand HEX Codes β‘
Whenever the two digits in every pair are identical, you can take a shortcut and write just 3 digits!
#FF6B00 cannot be shortened because 6 and B are different.Practical HTML Example with HEX:
3. Understanding HSL (Hue, Saturation, Lightness) π
RGB and HEX are great for computers. But how do human beings think about colors?
When an art teacher asks you: "Can you make this blue color a bit lighter, or make the orange more rich and bright?", you don't calculate base-16 math. You think about brightness and richness!
That is exactly why HSL was invented!
$$\text{hsl(Hue, Saturation\%, Lightness\%)\!}$$
Let's understand each of the three parts:
1. Hue (The 360Β° Color Wheel) π¨
Think of a circle or geometry protractor with 360 degrees. As you travel around the circle, you visit every color of the rainbow:
0Β°(or360Β°): Red60Β°: Yellow120Β°: Green180Β°: Cyan (Aqua)240Β°: Blue300Β°: Magenta (Pink/Purple)
2. Saturation (Purity & Richness of Color) π§ͺ
Saturation is expressed as a percentage from 0% to 100%:
0%: Completely gray! Like an old black-and-white television or pencil sketch (zero color).50%: A gentle, muted, pastel shade.100%: 100% pure, intense, vibrant color!
3. Lightness (How Much Light Shines On It) βοΈ
Lightness is also expressed as a percentage from 0% to 100%:
0%: Pitch Black (No light at all, like midnight).50%: Normal, True Color (The perfect balance).100%: Pure White (Blinding light, like looking into the sun).
Why Designers Love HSL: The Hover Effect Magic β¨
Imagine you created a button with this blue color:
When a user hovers their mouse over the button, you want it to become slightly darker:
And if you want a soft pastel background version:
Notice how easy that was? The color family (210) stayed the exact same; you only changed one single number!
HSLA (HSL with Transparency) πͺ
Just like RGBA, HSL has an A (Alpha) version called HSLA:
Pro Tips for School Web Developers π‘
Tip 1: VS Code's Built-in Color Picker
You never have to calculate HEX or HSL codes by hand in your head! In Visual Studio Code:
- 1Type any color name or code (like
redor#000000). - 2Hover your mouse cursor over the small color square that appears next to it.
- 3A visual color picker popup will open! You can drag your mouse to choose any shade, and click the top bar to switch between HEX, RGB, and HSL instantly!
Tip 2: The High-Contrast Accessibility Rule π
Always make sure your text is easy to read:
- Dark background? Use light or white text (
#FFFFFF). - Light background? Use dark text (
#1E293B). - Never put yellow text on a white background or dark blue text on a black backgroundβnobody can read it!
The Grand Master Color Cheat Sheet
Here is how the four main color formats look side-by-side for the same shades:
| Color | Color Name | HEX Code | RGB Code | HSL Code |
|---|---|---|---|---|
| Pure Red | red | #FF0000 or #F00 | rgb(255, 0, 0) | hsl(0, 100%, 50%) |
| Pure Green | lime | #00FF00 or #0F0 | rgb(0, 255, 0) | hsl(120, 100%, 50%) |
| Pure Blue | blue | #0000FF or #00F | rgb(0, 0, 255) | hsl(240, 100%, 50%) |
| Pure White | white | #FFFFFF or #FFF | rgb(255, 255, 255) | hsl(0, 0%, 100%) |
| Pitch Black | black | #000000 or #000 | rgb(0, 0, 0) | hsl(0, 0%, 0%) |
| Tomato | tomato | #FF6347 | rgb(255, 99, 71) | hsl(9, 100%, 64%) |
| DodgerBlue | dodgerblue | #1E90FF | rgb(30, 144, 255) | hsl(210, 100%, 56%) |
Common Beginner Mistakes β οΈ
1. β οΈ Forgetting the # symbol in HEX
A HEX code without a hash # will be completely ignored by the browser!
2. β οΈ Using invalid letters in HEX (beyond F)
Hexadecimal only goes from 0 to 9 and A to F. Letters like G, H, Z do not exist in Hex!
3. β οΈ Forgetting the % symbol in HSL
In hsl(), the Saturation and Lightness values must include the percent symbol (%).
Quick Summary
- HEX codes use Base-16 numbers (
0-9andA-F), whereA=10andF=15. - The standard HEX format is
#RRGGBB(two digits each for Red, Green, and Blue). - If every pair in a HEX code has matching digits, you can use the 3-digit shorthand (e.g.,
#FFFFFFbecomes#FFF). - HSL stands for Hue, Saturation, Lightness:
- Hue is an angle on the 360Β° color wheel ($0^\circ=\text{Red}$, $120^\circ=\text{Green}$, $240^\circ=\text{Blue}$).
- Saturation is color richness from $0\%$ (gray) to $100\%$ (vibrant).
- Lightness is brightness from $0\%$ (black) to $50\%$ (normal) to $100\%$ (white).
- In VS Code, hover your mouse over any color to use the interactive visual color picker.
- Always maintain strong contrast between your text and background for great readability.
Practice Quiz
Test your understanding with these multiple-choice questions:
1. What does the "HEX" in HEX color codes stand for?
A. Hexagon (6-sided polygon) B. Hexadecimal (Base-16 number system) C. Highly Extended D. Helium Extra Answer: B Explanation: HEX is short for Hexadecimal, which is the base-16 number system used in computer science.
2. Which letter in a HEX color code represents the highest value (15)?
A. A B. E C. F D. Z Answer: C Explanation: In hexadecimal, the digits are 0-9 followed by A(10), B(11), C(12), D(13), E(14), and F(15). F is the maximum single digit.
3. What is the 3-digit shorthand for the color #000000?
A. #00 B. #000 C. #0 D. #BBB Answer: B Explanation: When each pair of digits is identical (00, 00, 00), it can be written with 3 digits as #000.
4. On the HSL color wheel, which angle represents pure Green?
A. 0Β° B. 60Β° C. 120Β° D. 240Β° Answer: C Explanation: On the 360Β° color circle, 0Β° is Red, 120Β° is Green, and 240Β° is Blue.
5. If you set Lightness to 0% in an HSL color (hsl(120, 100%, 0%)), what color will you see?
A. Bright Green B. Pure White C. Pitch Black D. Muted Gray Answer: C Explanation: Setting Lightness to 0% means zero illumination, which turns any color completely Black.
Hands-on Practice Challenge π―
Open VS Code and create a file named student-id-card.html.
Your Challenge:
Design a modern, professional Student ID badge for your school using HEX and HSL colors:
- 1Container Card: Give it a deep navy background using
#0A2540and rounded corners. - 2School Header: Write your school's name in bold gold using
#F59E0B. - 3Student Name: Display your name in pure white using 3-digit shorthand
#FFF. - 4Class & Roll No Tag: Style it inside an HSL pill badge with
background-color: hsl(210, 100%, 50%)and white text. - 5Verified Status: Add a small success badge at the bottom with leafy green
hsl(140, 70%, 40%). - 6Open your file in the browser and test how sleek professional color codes look!
Congratulations! You have completed Chapter 5: Colors! You are now ready to tackle links, navigation, and building multi-page websites!
Links & Hyperlinks
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| HTML Colors & RGB | Links & Hyperlinks |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.