HEX, RGB and HSL: A Beginner’s Guide to Color Codes
You open a design file and see #1E90FF. A teammate sends you rgb(30, 144, 255). A style guide lists hsl(210, 100%, 56%). All three describe approximately the same shade of blue. Each uses a different format to represent the color.
HEX, RGB, and HSL are three common color formats used in web design and CSS. This guide explains how each format works, how to read color values, how to convert between them, and when to use each one.
Color Codes Explained
A color code is a value that tells a device or browser which color to display. Most colors you see on standard web pages are represented using the red, green, and blue (RGB) color model.
In the traditional 8-bit RGB format, each channel has a value from 0 to 255:
- Red: Controls the amount of red.
- Green: Controls the amount of green.
- Blue: Controls the amount of blue.
Combining different levels of these three channels produces a wide range of colors. For example, red, green, and blue at their maximum values produce white, while setting all three to zero produces black.
HEX and RGB represent these channel values directly. HSL describes a color using hue, saturation, and lightness, which can make certain color adjustments easier.
HTML Color Codes and CSS Color Formats
HTML and CSS let you specify colors in several ways. You can use named colors such as red and tomato, hexadecimal values, RGB functions, and HSL functions.
For example, all three of these CSS declarations represent approximately the same blue:
color: #1E90FF; color: rgb(30, 144, 255); color: hsl(210, 100%, 56%);
These formats are supported by modern browsers. The best choice depends on your project and how you plan to work with colors.
What Are HEX Color Codes?
HEX color codes are hexadecimal values used to represent colors. A standard six-digit HEX code starts with a hash sign (#) and follows this structure:
#RRGGBB
Each pair represents one RGB channel:
RRrepresents red.GGrepresents green.BBrepresents blue.
Consider this example:
#1E90FF
Its three pairs represent:
1Eis the red value.90is the green value.FFis the blue value.
HEX uses base 16 rather than the decimal system, which uses base 10. Its digits include 0–9 and A–F. The letters represent values from 10 to 15.
Two hexadecimal digits can represent 256 different values, from 00 to FF. This matches the 0–255 range used by traditional 8-bit RGB channels.
How to Read HEX Color Codes
Reading a HEX code becomes easier once you understand how its pairs work.
00means the channel has no intensity.FFmeans the channel is at full intensity.- Equal pairs produce shades of gray when all three channels match.
#000000represents black.#FFFFFFrepresents white.#FF0000represents pure red.#00FF00represents pure green.#0000FFrepresents pure blue.
You can also recognize some color characteristics by looking at the three pairs. For example, #E63946 has a strong red component, while #2A9D8F has a stronger green component.
Short and Extended HEX Codes
CSS supports more than just six-digit HEX codes.
Three-digit HEX: A three-digit code is shorthand for a six-digit code. Each digit is repeated to create its corresponding pair.
For example:
#F80 becomes #FF8800.
Four-digit HEX: This is shorthand for a code that includes an alpha channel (transparency).
For example:
#F808 becomes #FF880088.
Eight-digit HEX: An eight-digit code uses the format #RRGGBBAA. The final pair (AA) controls the alpha channel.
For example:
#1E90FF80 represents the same blue as #1E90FF, with approximately 50% opacity.
These extended formats are useful when you need transparency without using a separate RGB or HSL alpha value.
RGB Color Codes
RGB stands for red, green, and blue. It represents a color using three channel values, just like a six-digit HEX code, but uses decimal numbers instead of hexadecimal pairs.
A typical CSS RGB declaration looks like this:
color: rgb(30, 144, 255);
The values represent:
- Red: 30
- Green: 144
- Blue: 255
In the traditional 8-bit format, each channel ranges from 0 to 255. A value of 0 means no intensity, while 255 means maximum intensity.
Adding Transparency to RGB Colors
You can add an alpha value to an RGB color to control its opacity.
For example:
background: rgba(30, 144, 255, 0.5);
The final value, 0.5, represents 50% opacity.
Modern CSS also supports writing the same color with the newer space-separated syntax:
background: rgb(30 144 255 / 50%);
Both forms are valid in modern browsers. The newer syntax also lets you use an alpha value directly inside rgb().
RGB is useful when you need to work with channel values directly, calculate colors in code, or adjust transparency.
HSL Color Codes
HSL stands for hue, saturation, and lightness. Instead of listing red, green, and blue channel values, it describes a color through three properties.
A typical CSS HSL declaration looks like this:
color: hsl(210, 100%, 56%);
Here is what each part means:
- Hue: Represents the color’s position on a color wheel. It is usually measured in degrees from 0 to 360. Red is around 0°, green around 120°, and blue around 240°.
- Saturation: Controls how vivid or muted the color appears. At 0%, the result is a shade of gray. At 100%, the color has full saturation.
- Lightness: Controls how light or dark the color is. At 0%, the result is black. At 50%, it shows the fully saturated hue. At 100%, the result is white.
HSL is especially useful when creating color palettes. You can keep the hue and saturation the same while changing lightness to create lighter and darker variations.
For example:
/* Base color */ color: hsl(210, 100%, 50%); /* Lighter variation */ color: hsl(210, 100%, 70%); /* Darker variation */ color: hsl(210, 100%, 30%);
This approach makes it easier to create consistent color variations than manually adjusting individual HEX digits.
Adding Transparency to HSL Colors
HSL also supports transparency through an alpha value.
For example:
background: hsla(210, 100%, 50%, 0.5);
The final value sets the opacity to 50%.
You can also use the modern CSS syntax:
background: hsl(210 100% 50% / 50%);
HEX vs RGB vs HSL
All three formats can represent the same colors within the standard sRGB color range. Their main difference is how they express those colors and how convenient they are for different tasks.
| Format | Best for | Example |
|---|---|---|
| HEX | Copying colors from design tools and keeping CSS compact | #1E90FF |
| RGB | Working with color channels, calculations, and transparency | rgb(30, 144, 255) |
| HSL | Building palettes, themes, and lighter or darker variations | hsl(210, 100%, 56%) |
Use HEX when you need a short, familiar color value from a design file. Choose RGB when you need to work directly with red, green, and blue channel values. HSL is often more convenient when you want to adjust a color’s appearance without changing its basic hue.
There is no requirement to use only one format throughout a project. You can use different formats where they make the most sense, although following a consistent style can make your CSS easier to maintain.
How to Convert HEX to RGB
You can convert HEX to RGB manually by splitting the six-digit code into three pairs and converting each pair from base 16 to base 10.
Let’s use #1E90FF as an example.
- Split the code into three pairs:
1E,90, andFF. - Convert each pair into a decimal number.
- Use the resulting numbers as the red, green, and blue values.
The calculations are:
1E= (1 × 16) + 14 = 3090= (9 × 16) + 0 = 144FF= (15 × 16) + 15 = 255
The final RGB value is:
rgb(30, 144, 255)
How to Convert RGB to HEX
You can reverse the process to convert RGB into HEX.
Take these RGB values:
rgb(30, 144, 255)
Convert each decimal number into a two-digit hexadecimal value:
- 30 becomes
1E. - 144 becomes
90. - 255 becomes
FF.
Combine the three pairs and add the hash sign:
#1E90FF
How to Convert HEX to HSL
Converting HEX to HSL requires a few more calculations. First, convert the HEX value into RGB. Then normalize the RGB channels and use their minimum and maximum values to calculate hue, saturation, and lightness.
You do not need to perform these calculations manually for everyday design work. A color converter can calculate the HSL values instantly.
Converters and the Color Picker Tool
A HEX to RGB converter can save time when you need to change color formats. You can also use an RGB to HEX converter or a HEX to HSL converter to get the values you need.
Most online color converters work in a simple way:
- Enter or paste a color value.
- Select the format you want to convert from.
- View the converted HEX, RGB, or HSL value.
- Copy the result into your CSS or design project.
A color picker tool is another useful option. It lets you select a color from a palette or sample one from an image. The tool then displays its color values in different formats.
Browser developer tools also include color pickers. In many browsers, you can open the developer tools, find a CSS color declaration, and select its color swatch to inspect or adjust the value.
These tools are particularly useful when matching colors between a website, a design file, and an image.
Common Mistakes to Avoid
Even though HEX, RGB, and HSL are straightforward once you understand them, beginners often make a few mistakes.
- Forgetting the hash sign: A HEX color in CSS needs a
#before its value, such as#1E90FF. - Using invalid RGB values: In the traditional 8-bit RGB format, channel values range from 0 to 255. CSS also supports percentage-based RGB values.
- Leaving out HSL percentage signs: Saturation and lightness are normally written as percentages, such as
100%and56%. - Mixing up hue and lightness: Hue is an angle on the color wheel, while lightness is a percentage.
- Confusing transparency with lightness: Alpha controls opacity, while HSL lightness changes how light or dark a color appears.
- Ignoring color contrast: A color may look attractive on its own but still be difficult to read against a particular background. Check text and background contrast using accessibility guidelines.
Conclusion
HEX, RGB, and HSL describe colors in different ways. HEX uses compact hexadecimal values, RGB lists red, green, and blue channel values in decimal, and HSL describes a color through hue, saturation, and lightness. Within the standard sRGB range, all three can represent the same colors.
Use HEX when you want a short value copied from a design file, RGB when you need to work with channel values or transparency, and HSL when you want to build palettes with lighter and darker variations. Converting between formats is straightforward: HEX and RGB are two ways of writing the same three channel values, and a color converter can calculate HSL for you.
Whichever format you choose, keep your CSS consistent, double-check details such as the # sign and percentage signs, and test text and background contrast so your colors stay readable.
Frequently asked questions
What are HEX color codes?
HEX color codes are hexadecimal values used to represent colors. A standard six-digit code starts with #, and each pair represents the red, green, or blue channel.
Is HEX the same as RGB?
They can represent exactly the same color. HEX uses base-16 values, while RGB uses decimal channel values. For example, #1E90FF and rgb(30, 144, 255) represent the same sRGB color.
Why do some HEX codes have only three digits?
Three-digit HEX codes are shorthand versions of six-digit codes. Each digit is repeated to create a pair. For example, #0AF expands to #00AAFF.
Which is better for CSS, HEX or HSL?
Neither format is universally better. HEX is compact and common in design tools, while HSL makes it easier to create lighter, darker, or less saturated variations. Choose the format that suits your workflow.
How do I add transparency to a color?
You can use an alpha value in RGB or HSL, such as rgba(30, 144, 255, 0.5) or hsla(210, 100%, 50%, 0.5). You can also use an eight-digit HEX code, such as #1E90FF80.
What does the FF mean in a HEX color code?
FF is the highest two-digit hexadecimal value. It equals 255 in decimal, which represents maximum intensity for an RGB channel.
What is the difference between RGB and RGBA?
RGB represents a color using red, green, and blue values. RGBA adds a fourth value called alpha, which controls the color’s opacity.
Can I use HEX, RGB, and HSL in the same CSS file?
Yes. Modern browsers support all three formats, so you can use them together in one CSS file. However, consistent formatting can make your code easier to read and maintain.
What is the difference between HSL and HSV?
Both use hue to identify a color and a second property to describe its intensity. HSL uses lightness, while HSV uses value (brightness). They calculate and represent colors differently, so their percentages are not interchangeable.
How can I find the HEX code of a color in an image?
You can use an image color picker or an eyedropper tool. Select the color in the image, and the tool will display its HEX, RGB, or HSL value. Keep in mind that image compression and color profiles can affect the sampled result.