How Do You Choose Between HEX, RGB, and HSL? Differences Between Web Color Formats
While building a website, you may encounter the same color written as HEX, RGB, or HSL. Which should you use? A useful way to decide is to think about your task: sharing an exact color, reading its numeric components, or adjusting its hue and lightness. These formats are not competing levels of quality. They are different ways to specify a color. This article compares one blue in all three forms and shows how to apply the formats to a search-results card.
Use HEX for sharing, RGB for components, and HSL for adjustments
HEX is a hexadecimal color code, using digits 0–9 and letters A–F after a # sign. Blue #0066CC is an example. HEX is convenient when copying a color from a design specification or handing a precise value to another person. RGB stands for Red, Green, and Blue. CSS can specify that same blue as rgb(0 102 204), making its components—red 0, green 102, and blue 204—explicit.
HSL stands for Hue, Saturation, and Lightness. The CSS value hsl(210 100% 40%) uses a hue angle of 210 degrees, saturation of 100%, and lightness of 40%. In this example, blue #0066CC, rgb(0 102 204), and hsl(210 100% 40%) represent exactly the same color. Merely switching among these notations does not change how that blue looks.
HSL can be convenient when you want to explain an adjustment to the hue, saturation, or lightness while exploring alternatives. However, HSL lightness is not the same measure as perceived brightness or the relative luminance used in accessibility contrast calculations. Always check the resulting color with the actual background before choosing text colors.
Example: choose formats while building a search-results card
Imagine a document-search interface. The outer page uses pale blue-gray #F8FAFC and the search-results card uses white #FFFFFF. Set headings and body text in dark blue-gray #1F2937, supporting date information in slate #475569, and the card border in gray #64748B. The main action button uses blue #0066CC with white #FFFFFF text. A filter-status label uses pale blue #99CCFF with dark text #1F2937, plus the explicit label “Filter applied” so the state does not depend on color alone.
The design specification can record the primary blue as HEX #0066CC, while the button’s CSS declares the same color using rgb(0 102 204). For the label background, increase only the lightness of hsl(210 100% 40%) to 80%, giving hsl(210 100% 80%). This pale blue corresponds to #99CCFF. The point is not to force all three formats into every project, but to demonstrate how different notations may be useful during different stages of work.
Here is a short example using HTML (HyperText Markup Language, which defines webpage content and structure) and CSS (Cascading Style Sheets, which controls its appearance). It demonstrates how the elements look; the action button does not implement any logic for changing the search filters.
<div class="result-surface">
<section class="result-card" aria-labelledby="result-title">
<h3 id="result-title">Search criteria</h3>
<p class="result-note">Date range: This week</p>
<p><span class="result-chip">Filter applied</span></p>
<button type="button">Change filters</button>
</section>
</div>
.result-surface {
padding: 24px;
background-color: rgb(248 250 252);
}
.result-card {
padding: 24px;
background-color: rgb(255 255 255);
color: rgb(31 41 55);
border: 2px solid rgb(100 116 139);
border-radius: 12px;
}
.result-card h3 { margin-top: 0; }
.result-note { color: rgb(71 85 105); }
.result-chip {
display: inline-block;
padding: 4px 10px;
background-color: hsl(210 100% 80%);
color: rgb(31 41 55);
border-radius: 6px;
}
.result-card button {
display: block;
margin-top: 16px;
padding: 12px 18px;
background-color: rgb(0 102 204);
color: rgb(255 255 255);
border: 0;
border-radius: 6px;
font: inherit;
font-weight: 700;
cursor: pointer;
}
.result-card button:focus-visible {
outline: 3px solid rgb(15 23 42);
outline-offset: 3px;
}
A contrast ratio compares the relative brightness of two colors. Against the white card, the main dark text measures approximately 14.68:1 and the supporting slate text 7.58:1. White text on the blue button has about 5.57:1 contrast, while dark text on the pale-blue label reaches about 8.69:1. The gray card border measures approximately 4.76:1 against white and 4.55:1 against the surrounding pale blue-gray. The dark navy #0F172A keyboard-focus outline has about 17.85:1 contrast against white.
Prioritize consistent color roles, opacity, and contrast checks
In team projects, you can keep HEX values in the design specification and translate them to RGB or HSL in CSS when useful. Avoid maintaining conflicting values for the same color role. CSS custom properties—named values that can be reused in several declarations—can also help keep a shared palette consistent. There is no requirement to mix all three notations in one stylesheet.
RGB and HSL can include opacity, for example rgb(0 102 204 / 30%) or hsl(210 100% 40% / 30%). Eight-digit HEX can represent opacity too. Transparent colors blend with the surface underneath, so check the resulting appearance and contrast against that surface rather than evaluating the source color in isolation.
If you raise the HSL lightness of the main blue to 80%, pale blue #99CCFF with white #FFFFFF lettering has a contrast ratio of only about 1.69:1. A larger lightness percentage does not automatically make any text color readable. Under the Web Content Accessibility Guidelines (WCAG) 2.2, normal-sized text generally requires at least 4.5:1 contrast, while non-text information necessary for identifying interface controls or states generally requires 3:1 against adjacent colors. Passing individual contrast checks does not guarantee overall accessibility.
HueRoll displays HEX, RGB, and HSL values and lets you adjust colors by entering three- or six-digit HEX codes or using its color picker. You cannot enter RGB or HSL values directly. Its contrast feature compares the selected color with white or black, not arbitrary color pairs, and it does not automatically determine WCAG conformance. Whichever notation you choose, verify the actual text, background, and control combinations in the finished interface.