HEX & HSL Color Codes0%

HEX & HSL Color Codes

Beginner9 min readβ€’Updated: 2026-09-10
Study Materials

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?

  1. 1
    HEX codes are short, compact, and universally supported across every design software and website in the world.
  2. 2
    HSL 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 SymbolMeaning in Normal Maths
0 to 9Numbers 0 to 9
A10
B11
C12
D13
E14
F15 (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}$$

  • 00 is the lowest possible two-digit value (equal to $0$).
  • FF is 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}$$

Output
# FF 6B 00
β”‚ ── ── ──
β”‚ β”‚ β”‚ β”‚
Hashtagβ”‚ β”‚ └── Blue (00 = 0 in decimal)
β”‚ └─────── Green (6B = 107 in decimal)
└──────────── Red (FF = 255 in decimal)
  1. 1
    RR (First 2 digits): Red intensity from 00 (off) to FF (maximum).
  2. 2
    GG (Middle 2 digits): Green intensity from 00 (off) to FF (maximum).
  3. 3
    BB (Last 2 digits): Blue intensity from 00 (off) to FF (maximum).

Let's Look at Familiar HEX Colors:

ColorHEX CodeRedGreenBlueExplanation
Pure Red#FF0000FF (255)00 (0)00 (0)Red at maximum brightness; green & blue off.
Pure Green#00FF0000 (0)FF (255)00 (0)Green at maximum brightness.
Pure Blue#0000FF00 (0)00 (0)FF (255)Blue at maximum brightness.
Pitch Black#00000000 (0)00 (0)00 (0)Zero light everywhere.
Pure White#FFFFFFFF (255)FF (255)FF (255)All lights at 100% full power.
Bright Yellow#FFFF00FF (255)FF (255)00 (0)Red + Green at full power.
MSK Orange#FF6B00FF (255)6B (107)00 (0)Signature energetic brand orange.
MSK Navy#0A25400A (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!

Output
Full 6-Digit Code ───► Short 3-Digit Code
#FFFFFF ───► #FFF
#000000 ───► #000
#FF0000 ───► #F00
#00FF00 ───► #0F0
#0000FF ───► #00F
#FFAA00 ───► #FA0
Pro Tip
Rule of Thumb: You can only use the 3-digit shorthand if both digits in Red match, both in Green match, and both in Blue match! For example, #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\%)\!}$$

Output
hsl( 210 , 100% , 50% )
─── ──── ───
β”‚ β”‚ β”‚
Hue Saturation Lightness
(0Β°-360Β°) (0%-100%) (0%-100%)

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:

Output
0Β° / 360Β° (RED)
β–²
β”‚
300Β° (MAGENTA) β”‚ 60Β° (YELLOW)
\ β”‚ /
\ β”‚ /
\ β”‚ /
240Β° (BLUE) ───┼───────┼───────┼─── 120Β° (GREEN)
/ β”‚ \
/ β”‚ \
/ β”‚ \
210Β° (SKY BLUE) β”‚ 180Β° (CYAN)
β–Ό
  • 0Β° (or 360Β°): Red
  • 60Β°: Yellow
  • 120Β°: Green
  • 180Β°: Cyan (Aqua)
  • 240Β°: Blue
  • 300Β°: 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:

CSS
/* Normal Button */
background-color: hsl(210, 100%, 50%);

When a user hovers their mouse over the button, you want it to become slightly darker:

CSS
/* Darker Button on Mouse Hover */
background-color: hsl(210, 100%, 40%); /* Just dropped lightness to 40%! */

And if you want a soft pastel background version:

CSS
/* Soft Pastel Blue Background */
background-color: hsl(210, 100%, 95%); /* Lightness boosted to 95%! */

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:

HTML
<!-- Semi-transparent Emerald Green banner (Alpha = 0.8) -->
<div style="background-color: hsla(140, 75%, 45%, 0.8); color: white; padding: 15px; border-radius: 8px;">
<h3>Class Test Passed!</h3>
</div>

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:

  1. 1
    Type any color name or code (like red or #000000).
  2. 2
    Hover your mouse cursor over the small color square that appears next to it.
  3. 3
    A 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:

ColorColor NameHEX CodeRGB CodeHSL Code
Pure Redred#FF0000 or #F00rgb(255, 0, 0)hsl(0, 100%, 50%)
Pure Greenlime#00FF00 or #0F0rgb(0, 255, 0)hsl(120, 100%, 50%)
Pure Blueblue#0000FF or #00Frgb(0, 0, 255)hsl(240, 100%, 50%)
Pure Whitewhite#FFFFFF or #FFFrgb(255, 255, 255)hsl(0, 0%, 100%)
Pitch Blackblack#000000 or #000rgb(0, 0, 0)hsl(0, 0%, 0%)
Tomatotomato#FF6347rgb(255, 99, 71)hsl(9, 100%, 64%)
DodgerBluedodgerblue#1E90FFrgb(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!

HTML
<!-- ❌ WRONG: Missing the hash symbol -->
<h2 style="color: FF6B00;">Text</h2>
Β 
<!-- βœ… CORRECT: Always start with # -->
<h2 style="color: #FF6B00;">Text</h2>

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!

HTML
<!-- ❌ WRONG: 'G' and 'Z' are not valid hex digits! -->
<p style="color: #GGZZ00;">Invalid</p>
Β 
<!-- βœ… CORRECT: Only characters 0-9 and A-F -->
<p style="color: #EEFF00;">Valid</p>

3. ⚠️ Forgetting the % symbol in HSL

In hsl(), the Saturation and Lightness values must include the percent symbol (%).

HTML
<!-- ❌ WRONG: Missing % on saturation and lightness -->
<p style="color: hsl(210, 100, 50);">Invalid</p>
Β 
<!-- βœ… CORRECT: Include % symbols -->
<p style="color: hsl(210, 100%, 50%);">Valid</p>

Quick Summary

  • HEX codes use Base-16 numbers (0-9 and A-F), where A=10 and F=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., #FFFFFF becomes #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:

  1. 1
    Container Card: Give it a deep navy background using #0A2540 and rounded corners.
  2. 2
    School Header: Write your school's name in bold gold using #F59E0B.
  3. 3
    Student Name: Display your name in pure white using 3-digit shorthand #FFF.
  4. 4
    Class & Roll No Tag: Style it inside an HSL pill badge with background-color: hsl(210, 100%, 50%) and white text.
  5. 5
    Verified Status: Add a small success badge at the bottom with leafy green hsl(140, 70%, 40%).
  6. 6
    Open 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!

Next Lesson

Links & Hyperlinks

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
HTML Colors & RGBLinks & Hyperlinks

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext