How to Convert Hex Color Code to RGB for CSS: A Complete Guide

Oct 15, 2026
5 Min Read
How to Convert Hex Color Code to RGB for CSS: A Complete Guide

How to Convert Hex Color Code to RGB for CSS: A Complete Guide

Choosing the right colors is an important part of creating an attractive, consistent website. If you work with CSS, you have probably encountered hex color codes such as #FF5733, #3498DB, or #FFFFFF. These codes are widely used in web design, but sometimes you need their RGB equivalents to adjust transparency, work with JavaScript, or fine-tune a website's appearance.

Learning how to convert a hex color code to RGB for CSS makes these tasks easier. You can convert colors manually, use an online color converter, or write a simple JavaScript function to automate the process.

In this guide, you will learn how hexadecimal color codes work, how to convert them into RGB values, how to use RGB colors in CSS, and how to avoid common conversion mistakes. You will also discover when to use rgb(), rgba(), and modern CSS color syntax.

Whether you are a beginner learning web development or an experienced designer managing a website's color palette, this guide will help you work with CSS colors more confidently.

What Is a Hex Color Code?

A hexadecimal color code, commonly called a hex color code, is a way to represent a color using six hexadecimal digits, usually preceded by a hash symbol (#).

For example:

  • #FF0000 represents red.
  • #00FF00 represents green.
  • #0000FF represents blue.
  • #FFFFFF represents white.
  • #000000 represents black.

Hexadecimal is a base-16 numbering system that uses sixteen symbols: the numbers 0 through 9 and the letters A through F.

In a standard six-digit CSS hex color, each pair of digits represents the intensity of one of three primary light colors: red, green, and blue.

The structure looks like this:

#RRGGBB

  • RR represents the red component.
  • GG represents the green component.
  • BB represents the blue component.

Each component can have a value from 00 to FF in hexadecimal, corresponding to a decimal value from 0 to 255.

For example, #FF5733 contains these components:

Component Hex Value Decimal Value
RedFF255
Green5787
Blue3351

Therefore, the RGB equivalent of #FF5733 is rgb(255, 87, 51).

Understanding this structure is the first step toward converting hex colors accurately.

What Is RGB in CSS?

RGB stands for Red, Green, and Blue. It is a color model that creates colors by combining different intensities of red, green, and blue light.

In CSS, RGB colors can be written using the rgb() function.

.button {
  background-color: rgb(255, 87, 51);
}

This code gives the button a reddish-orange background.

Each RGB component uses a numeric value between 0 and 255 when expressed in the traditional integer format.

Here is how the values work:

  • rgb(255, 0, 0) produces red.
  • rgb(0, 255, 0) produces green.
  • rgb(0, 0, 255) produces blue.
  • rgb(255, 255, 255) produces white.
  • rgb(0, 0, 0) produces black.
  • rgb(128, 128, 128) produces a medium gray.

A value of 0 means that the corresponding color component contributes no intensity. A value of 255 represents its maximum intensity.

By combining these three components, CSS can represent more than 16 million RGB color combinations.

Hex vs. RGB: What Is the Difference?

Hex and RGB are two different ways of representing colors. Both can describe the same color, but their formats differ.

Feature Hex Color RGB Color
Example#FF5733rgb(255, 87, 51)
Number systemHexadecimalDecimal
Component formatTwo hexadecimal digits per channelThree numeric channel values
Typical range00 to FF per channel0 to 255 per channel
Transparency supportEight-digit hex can include alphargba() or modern rgb() syntax
ReadabilityCompactExplicit channel values
Common usesDesign systems and stylesheetsColor manipulation and dynamic styling

Neither format is universally better. Hex is compact and familiar to many designers, while RGB can be more convenient when you need to calculate or modify individual color components.

How to Convert Hex Color Code to RGB for CSS

The easiest way to convert a hex color code to RGB depends on how often you need to perform the conversion.

For occasional conversions, an online tool is usually the fastest option. If you want to understand the process, you can calculate the values manually.

Method 1: Use an Online Hex to RGB Converter

An online hex to RGB converter is a convenient solution when you want accurate results without doing calculations yourself.

You can use a hex-to-RGB conversion tool on SmartColorTools.com to simplify the process.

Follow these steps:

  1. Open the hex color converter.
  2. Enter your hexadecimal color code, such as #3498DB.
  3. Check that the code contains valid hexadecimal characters.
  4. View the converted RGB values.
  5. Copy the result into your CSS stylesheet.

For example, converting #3498DB produces:

color: rgb(52, 152, 219);

You can then use this value for text, backgrounds, borders, or other CSS properties that accept colors.

An online converter is particularly useful when working with a large color palette, matching colors from a design mockup, or converting values copied from graphic design software.

Always verify that the output corresponds to the original color before using it in a production project.

Method 2: Convert Hex to RGB Manually

You can convert any standard six-digit hex color into RGB by separating the color code into three pairs and converting each pair from hexadecimal to decimal.

Let's use #1ABC9C as an example.

Step 1: Remove the hash symbol.

Start with #1ABC9C. Remove the leading # to get 1ABC9C.

Step 2: Divide the code into three pairs.

The pairs represent the red, green, and blue channels.

  • Red: 1A
  • Green: BC
  • Blue: 9C

Step 3: Convert each pair to decimal.

Hexadecimal values use powers of 16. For a two-digit hexadecimal value, multiply the first digit's decimal equivalent by 16 and add the second digit's decimal equivalent.

Conversion formula:

Decimal value = (first digit × 16) + second digit

For 1A:

  • 1 equals 1.
  • A equals 10.
  • (1 × 16) + 10 = 26.

Therefore, the red value is 26.

For BC:

  • B equals 11.
  • C equals 12.
  • (11 × 16) + 12 = 188.

Therefore, the green value is 188.

For 9C:

  • 9 equals 9.
  • C equals 12.
  • (9 × 16) + 12 = 156.

Therefore, the blue value is 156.

Step 4: Write the RGB color in CSS.

Combine the three decimal values in the correct order:

color: rgb(26, 188, 156);

The original hex color #1ABC9C and the RGB value rgb(26, 188, 156) represent the same color.

Method 3: Convert Hex to RGB Using JavaScript

If you are building a website, color tool, or design application, JavaScript can automate hex-to-RGB conversion.

The following function converts a standard six-digit hexadecimal color into an RGB string.

function hexToRgb(hex) {
  const cleanHex = hex.replace(/^#/, "");

  if (!/^[0-9A-Fa-f]{6}$/.test(cleanHex)) {
    throw new Error("Enter a valid six-digit hex color.");
  }

  const red = parseInt(cleanHex.slice(0, 2), 16);
  const green = parseInt(cleanHex.slice(2, 4), 16);
  const blue = parseInt(cleanHex.slice(4, 6), 16);

  return `rgb(${red}, ${green}, ${blue})`;
}

console.log(hexToRgb("#FF5733"));
// rgb(255, 87, 51)

This function performs four main operations:

  1. Removes an optional leading hash symbol.
  2. Validates the input to ensure it contains six hexadecimal digits.
  3. Converts each two-digit pair into a decimal number using parseInt() with a base of 16.
  4. Returns the resulting RGB value in CSS-compatible syntax.

The validation step prevents invalid input from producing misleading results.

This implementation intentionally supports six-digit hex codes only. If your application needs to support three-digit or eight-digit hex codes, you can extend the validation and conversion logic.

Hex to RGB Conversion Examples for CSS

Working through several examples helps you recognize common colors and verify your conversions.

Here are some frequently used hex colors and their RGB equivalents.

Color Name Hex Code RGB Value
Red#FF0000rgb(255, 0, 0)
Green#008000rgb(0, 128, 0)
Blue#0000FFrgb(0, 0, 255)
White#FFFFFFrgb(255, 255, 255)
Black#000000rgb(0, 0, 0)
Yellow#FFFF00rgb(255, 255, 0)
Orange#FFA500rgb(255, 165, 0)
Purple#800080rgb(128, 0, 128)
Gray#808080rgb(128, 128, 128)
Teal#008080rgb(0, 128, 128)
Coral#FF7F50rgb(255, 127, 80)
Dodger Blue#1E90FFrgb(30, 144, 255)

These examples are useful when building a website's color palette or checking whether an online conversion tool returns the expected values.

Example: Convert a Hex Color for a Website Button

Suppose your website's primary button uses the hex color #2563EB.

Its RGB equivalent is rgb(37, 99, 235).

You can use it in CSS like this:

.primary-button {
  background-color: rgb(37, 99, 235);
  color: rgb(255, 255, 255);
  border: none;
  padding: 12px 20px;
  border-radius: 6px;
}

This creates a blue button with white text.

If your design system stores colors as hexadecimal values but a component needs RGB channels for calculations, converting the color gives you a consistent way to use the same base color across your application.

How to Use RGB Colors in CSS

Once you convert a hex code into RGB, you can apply the resulting value to most CSS properties that accept colors.

Set a Background Color

Use the background-color property to set an element's background.

body {
  background-color: rgb(245, 247, 250);
}

This creates a light background suitable for many websites and dashboards.

Change Text Color

The color property controls an element's text color.

h1 {
  color: rgb(37, 99, 235);
}

You can use this technique to match headings and links to your website's branding.

Set Border Colors

RGB colors also work with borders.

.card {
  border: 1px solid rgb(220, 226, 232);
}

This creates a subtle border around a card or content container.

Use RGB Colors with Transparency

One advantage of RGB syntax is that you can include an alpha value to control transparency.

.overlay {
  background-color: rgba(0, 0, 0, 0.5);
}

Here, the alpha value is 0.5, which represents 50% opacity. The background color is black, but it allows the content behind it to show through.

You can also use modern CSS syntax:

.overlay {
  background-color: rgb(0 0 0 / 50%);
}

Both examples express the same intended color and opacity.

The alpha value ranges from 0 for fully transparent to 1 for fully opaque when using the traditional decimal notation.

  • rgba(0, 0, 0, 0) is fully transparent black.
  • rgba(0, 0, 0, 0.25) is black at 25% opacity.
  • rgba(0, 0, 0, 0.75) is black at 75% opacity.
  • rgba(0, 0, 0, 1) is fully opaque black.

Transparency is especially useful for overlays, hover effects, shadows, and layered interface elements.

How to Convert Three-Digit Hex Colors to RGB

CSS also supports three-digit hexadecimal color notation.

For example, #F0A.

Each digit is duplicated to produce a six-digit color:

  • F becomes FF.
  • 0 becomes 00.
  • A becomes AA.

Therefore, #F0A becomes #FF00AA.

The RGB equivalent is rgb(255, 0, 170).

This shorthand is convenient for writing compact stylesheets, but it does not represent every possible six-digit color. It works only when each pair in the six-digit code contains two identical hexadecimal digits.

For example, #3498DB cannot be shortened to a three-digit hex code without changing its color.

When converting three-digit hex codes manually or with a custom script, expand each digit before converting the individual channels to decimal.

Can You Convert Eight-Digit Hex Codes to RGB?

Yes. CSS supports eight-digit hexadecimal color notation, which includes an alpha component.

The format is #RRGGBBAA.

The first six digits represent red, green, and blue. The final two digits represent alpha, or opacity.

For example, #FF000080 represents red with an alpha value of 80 in hexadecimal.

The RGB components are:

  • Red: 255
  • Green: 0
  • Blue: 0

The alpha component is 128 out of 255, or approximately 50.2% opacity.

Its equivalent CSS representation is:

color: rgb(255 0 0 / 50.2%);

You can also write it using rgba() with a decimal alpha value of approximately 0.502.

Remember that the final two digits in an eight-digit hex code do not represent another RGB channel. They represent transparency, so they must be handled separately when converting the color.

Common Mistakes When Converting Hex to RGB

Even though hex-to-RGB conversion is straightforward, a few mistakes can lead to incorrect colors or CSS errors.

1. Forgetting That Hexadecimal Is Base 16

Hexadecimal digits are not decimal digits. For example, FF equals 255, not 99 or 15.

Use base-16 conversion rather than interpreting the pairs as ordinary decimal numbers.

2. Mixing Up the Color Channels

The correct order is always red, green, and blue for standard hex-to-RGB conversion.

For #3498DB, the correct output is rgb(52, 152, 219).

Rearranging these values produces a different color.

3. Confusing RGB Values with Percentages

Traditional RGB integer values range from 0 to 255. CSS also supports percentage-based RGB components, but those use a different representation.

color: rgb(100% 0% 0%);

This represents full red. It is not equivalent to rgb(100, 0, 0).

4. Using Invalid Characters

Hexadecimal codes can contain the digits 0 through 9 and the letters A through F, regardless of letter case.

Characters such as G, X, or Z are not valid hexadecimal digits.

Always check the input before using the converted value in a stylesheet or application.

5. Ignoring Alpha Transparency

A six-digit hex color has no explicitly encoded alpha component. An eight-digit hex color does.

If your original color includes transparency, converting only the first six digits will lose that information.

6. Assuming RGB Conversion Improves Accessibility

Changing a color from hex to RGB does not automatically improve readability or accessibility. The two formats can represent exactly the same color.

For accessible web design, check the contrast between text and its background, and do not rely on color alone to communicate important information.

Best Practices for Managing CSS Colors

Converting color formats is only one part of creating a reliable website design system.

Use these practical recommendations to keep your styles consistent.

Choose a consistent color format. Decide whether your project will primarily use hex, RGB, HSL, or a combination. Consistency makes stylesheets easier to maintain.

Use CSS custom properties. Store frequently used colors in variables instead of repeating their values throughout your stylesheet.

:root {
  --primary-color: #2563EB;
  --text-color: #1F2937;
  --background-color: #F9FAFB;
}

.button {
  background-color: var(--primary-color);
}

body {
  color: var(--text-color);
  background-color: var(--background-color);
}

You can retain hex values in your design tokens while converting them to RGB whenever a specific component or calculation requires RGB channels.

Validate color values before publishing. When using automated conversion scripts, validate the input and test the generated CSS.

Use alpha transparency carefully. Transparent colors blend with the content behind them. The final appearance depends on the background, so test overlays against their actual surroundings.

Check color contrast. Use a contrast checker to assess whether text and background combinations meet the applicable accessibility requirements.

Document your design palette. Keep a record of your brand colors, their intended use, and any alternate formats required by your application.

These habits reduce accidental inconsistencies and make future design changes easier.

Frequently Asked Questions

1. How do I convert a hex color code to RGB in CSS?

Remove the # symbol, split the six-digit hex code into three pairs, and convert each pair from hexadecimal to decimal. Use the results in rgb(red, green, blue).

For example, #FF5733 becomes rgb(255, 87, 51).

You can also use an online hex-to-RGB converter to perform the conversion automatically.

2. What is the RGB equivalent of a hex color?

The RGB equivalent is another representation of the same color using red, green, and blue channel values.

For example, #FFFFFF is equivalent to rgb(255, 255, 255), while #000000 is equivalent to rgb(0, 0, 0).

3. Can I use RGB instead of hex in CSS?

Yes. CSS supports both hexadecimal color notation and RGB functions for properties that accept colors.

For example, these declarations represent the same blue color:

color: #3498DB;
color: rgb(52, 152, 219);

Use whichever format best fits your workflow and project requirements.

4. Is an online hex-to-RGB converter accurate?

A reliable converter should produce the mathematically equivalent RGB values for valid hexadecimal input.

For example, a converter should return rgb(255, 0, 0) for #FF0000.

If you are using the result in a production website, verify that the input format is supported and that any alpha transparency is preserved when necessary.

5. How do I convert a hex color to RGB with transparency?

First, convert the red, green, and blue components as usual. Then add an alpha value between 0 and 1, or express opacity as a percentage in modern CSS syntax.

background-color: rgb(255 0 0 / 50%);

This creates red at 50% opacity.

6. How do I convert a three-digit hex code to RGB?

Duplicate each digit to create a six-digit hex code, then convert each pair to decimal.

For example, #ABC expands to #AABBCC, which converts to rgb(170, 187, 204).

7. What is the difference between RGB and RGBA?

RGB represents red, green, and blue color components. RGBA adds an alpha component to control opacity.

For example, rgb(255, 0, 0) represents fully opaque red, while rgba(255, 0, 0, 0.5) represents red at 50% opacity.

Modern CSS also supports alpha values directly within the rgb() function.

8. Can I convert RGB back to hex?

Yes. Convert each RGB component to a two-digit hexadecimal value and combine the pairs after a hash symbol.

For example, rgb(52, 152, 219) converts to #3498DB.

When a component's hexadecimal representation contains only one digit, add a leading zero to maintain the standard six-digit format.

9. Does converting hex to RGB change the color?

No. A correct conversion preserves the same red, green, and blue channel values. Only the notation changes.

However, a conversion that omits alpha transparency, changes channel order, or rounds values incorrectly can alter the final appearance.

10. Should I use hex or RGB for website design?

Both formats are suitable for modern CSS. Hex is compact and convenient for defining fixed colors, while RGB is helpful when working with channel values, transparency, or programmatic color manipulation.

Choose the format that makes your code easiest to understand and maintain.

Conclusion

Learning how to convert a hex color code to RGB for CSS is a useful skill for web designers, developers, and anyone working with website color palettes.

The process is simple: separate the hexadecimal code into red, green, and blue pairs, convert each pair to decimal, and place the resulting values inside the CSS rgb() function.

For example, #FF5733 becomes rgb(255, 87, 51). If you need a quicker workflow, an online hex-to-RGB converter can handle the calculation automatically. JavaScript is another practical option when you need to convert colors dynamically in a web application.

Remember to validate your input, preserve alpha transparency when working with eight-digit hex codes, and check contrast when choosing colors for text and interface elements.

Ready to convert your colors? Use SmartColorTools.com to simplify your color-conversion workflow and find the RGB values you need for your next CSS project.

Share this article

System Message