HTML colors are defined using hexadecimal values, RGB values, or color names. Let me provide you with examples of each, along with their uses and complete code examples.
Learn how to use HTML colors to enhance the visual appearance of your web pages. This guide covers RGB, HEX, HSL, and RGBA color representations with code examples and explanations.
Hexadecimal colors use a combination of six characters, consisting of numbers (0-9) and letters (A-F), preceded by a hash symbol (#).
The characters represent the intensity of red, green, and blue (RGB) colors in the format #RRGGBB.
Hexadecimal colors are widely used in web design to specify precise colors for elements like backgrounds, text, borders, and more.
How to use Hexadecimal color in HTML?
For body tag:
< body bgcolor=”#FF0000”>
Or
<body style=”background-color: #FF0000;”>
Example:
<html>
<head>
<title>Hexadecimal Colors</title>
</head>
<body style="background-color: #FF0000;">
<h1 style="color: #00FF00;">Hello, World!</h1>
</body>
</html>

RGB colors specify the intensity of red, green, and blue colors individually using decimal values ranging from 0 to 255. They are defined using the rgb() or rgba() function, where “a” represents the alpha channel (opacity) value.
RGB colors are commonly used to define colors in CSS stylesheets and inline styles.
Complete code example:
<!DOCTYPE html>
<html>
<head>
<title>RGB Colors</title>
<style>
body {
background-color: rgb(255, 0, 0);
}
h1 {
color: rgba(0, 255, 0, 0.8);
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>

HTML also provides a set of predefined color names that can be used directly to specify colors.
Color names are convenient when you want to use common colors without specifying RGB or hexadecimal values explicitly.
Complete code example:
<!DOCTYPE html>
<html>
<head>
<title>Color Names</title>
</head>
<body style="background-color: blue;">
<h1 style="color: lime;">h1 has lime color</h1>
</body>
</html>

In the provided code examples, the background color of the <body> element is set using the style attribute, and the color of the <h1> heading is set similarly.
You can replace the color values with your desired ones or experiment further based on your requirements.
Here are some common uses of HTML colors with the style attribute, along with complete code examples:
You can use the style attribute to set the background color of an HTML element.
Here’s an example of setting the background color of the <body> element to yellow:
<!DOCTYPE html>
<html>
<head>
<title>Background Color</title>
</head>
<body style="background-color: yellow;">
<h1> background-color is yellow</h1>
</body>
</html>

The style attribute can be used to change the color of text within an HTML element.
In the following example, the color of the <h1> heading is set to blue:
<!DOCTYPE html>
<html>
<head>
<title>Text Color</title>
</head>
<body>
<h1 style="color: blue;">Hello, World!</h1>
</body>
</html>

You can apply inline styles to multiple HTML elements within a single tag by separating the style properties with a semicolon (;).
Here’s an example of setting the background color and text color of the <h1> and <p> elements:
<!DOCTYPE html>
<html>
<head>
<title>Inline Styles</title>
</head>
<body>
<h1 style="background-color: yellow; color: blue;">Hello, World!</h1>
<p style="background-color: cyan; color: red;">Lorem ipsum dolor sit amet.</p>
</body>
</html>

You can add a border to an HTML element and specify its color using the style attribute.
The following example adds a red border to the <div> element:
<!DOCTYPE html>
<html>
<head>
<title>Border Color</title>
</head>
<body>
<div style="border: 2px solid red;">Hello, World!</div>
</body>
</html>

HTML colors can be used to style different states of links, such as normal, hover, visited, and active.
Here’s an example that sets the colors for different link states:
<!DOCTYPE html>
<html>
<head>
<title>Link Colors</title>
<style>
a {
color: blue; /* Normal link color */
}
a:hover {
color: red; /* Link color on hover */
}
a:visited {
color: purple; /* Link color for visited links */
}
a:active {
color: green; /* Link color on active state */
}
</style>
</head>
<body>
<a href="#">Link Example</a>
</body>
</html>
These examples demonstrate various uses of HTML colors with the style attribute.
You can modify the color values or experiment with other CSS properties to achieve your desired styles.

Here are examples of using RGB and RGBA colors in HTML with complete code examples:
RGB colors specify the intensity of red, green, and blue colors individually using decimal values ranging from 0 to 255.
You can define an RGB color using the rgb() function in CSS.
<!DOCTYPE html>
<html>
<head>
<title>RGB Color</title>
</head>
<body>
<h1 style="color: rgb(255, 0, 0);">using rgb color</h1>
</body>
</html>

In the above example, the <h1> heading text color is set to pure red using the RGB value (255, 0, 0).
RGBA colors are similar to RGB colors, but they include an additional alpha channel that represents the opacity of the color. The alpha value ranges from 0 (completely transparent) to 1 (completely opaque). RGBA colors are defined using the rgba() function in CSS.
<!DOCTYPE html>
<html>
<head>
<title>RGBA Color</title>
</head>
<body>
<h1 style="color: rgba(0, 128, 0, 0.5);">using rgba color </h1>
</body>
</html>

In the above example, the <h1> heading text color is set to semi-transparent green using the RGBA value (0, 128, 0, 0.5).
You can use these examples as a starting point and modify the RGB or RGBA values to achieve your desired colors. Additionally, you can apply the style attribute to other HTML elements or combine it with other CSS properties to create various visual effects.
Here’s an example of using HEX colors in HTML with a complete code example:
<!DOCTYPE html>
<html>
<head>
<title>HEX Color</title>
</head>
<body>
<h1 style="color: #FF00ff;">using hex colors </h1>
</body>
</html>

In the above example:
You can find various HEX color charts or use color picker tools to find the desired HEX color values.
Try to experiment with different HEX color values in your HTML code to achieve the desired visual effect.
HSL (Hue, Saturation, Lightness) colors define colors based on their hue, saturation, and lightness values.
The hue represents the color itself, saturation controls the intensity or purity of the color, and lightness determines how light or dark the color appears.
Here’s an example of using HSL and HSLA colors in HTML with a complete code example:
<!DOCTYPE html>
<html>
<head>
<title>HSL Color</title>
</head>
<body>
<h1 style="color: hsl(120, 100%, 50%);"> using HSL Color !</h1>
</body>
</html>

In the above example, the <h1> heading text color is set to a fully saturated green color with a hue of 120 degrees, 100% saturation, and 50% lightness.
HSLA colors are defined using the hsla() function in CSS.
<!DOCTYPE html>
<html>
<head>
<title>HSLA Color</title>
</head>
<body>
<h1 style="color: hsla(240, 100%, 50%, 0.7);">using HSLA Color </h1>
</body>
</html>

In the above example:
a) To define the structure of a web page.
b) To specify the font style of text.
c) To define the colors of elements in a web page.
d) To create animations and transitions.
Answer: c) To define the colors of elements in a web page.
a) Hexadecimal values.
b) RGB values.
c) HSL values.
d) Decimal values.
Answer: d) Decimal values.
a) rgb(255, 0, 0)
b) #FF0000
c) red
d) rgba(255, 0, 0, 0.5)
Answer: a) rgb(255, 0, 0)
a) RGB
b) HSL
c) HEX
d) RGBA
Answer: d) RGBA
a) Hue, Saturation, Lightness
b) Red, Green, Blue
c) Hexadecimal, Saturation, Lightness
d) Hue, Shade, Luminance
Answer: a) Hue, Saturation, Lightness
a) #FF00FF
b) aquamarine
c) 128, 0, 128
d) hsla(0, 100%, 50%, 1)
Answer: b) aquamarine
a) #FFFFFF
b) #000000
c) #FF0000
d) #00FF00
Answer: a) #FFFFFF
a) background-color: rgba(0, 0, 255, 0.5);
b) background-color: hsl(240, 100%, 50%);
c) background-color: yellow;
d) background-color: rgb(255, 0, 0);
Answer: a) background-color: rgba(0, 0, 255, 0.5);
a) color
b) background-color
c) text-decoration
d) hover-color
Answer: a) color
a) Fully transparent
b) Fully opaque
c) 30% transparent
d) 70% transparent
Answer: c) 30% transparent